Skip to content

Commit

Permalink
🚨 add some e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Michał Nykiel committed Oct 19, 2016
1 parent 31995e3 commit 3b6dd43
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

dist/
# Created by https://www.gitignore.io/api/node

### Node ###
Expand Down
88 changes: 88 additions & 0 deletions e2e/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import test from 'ava';
import path from 'path';
import fs from 'fs-extra';
import webpack from 'webpack';
import ModuleMappingPlugin from '../';

const outputPath = path.resolve('../dist');

async function build(config) {
return new Promise((resolve, reject) => webpack(config, (err, stats) => {
if (err || stats.hasErrors()) {
reject(err || stats.toJson('errors-only')
.errors);
return;
}
resolve(`${config.output.path}/${config.output.filename}`);
}));
}

function createConfig(entry, pluginConfig) {
return {
entry,
output: {
path: outputPath,
filename: `bundle_${Date.now()}.js`,
library: 'test',
libraryTarget: 'commonjs2'
},
resolve: {
root: [path.resolve('./modules')]
},
module: {
loaders: [
{ test: /\.html$/, loader: 'raw' }
]
},
plugins: [
ModuleMappingPlugin(pluginConfig)
]
};
}

function testPathType(pathType) {

test(`should not map anything when using ${pathType} paths`, async t => {
const bundlePath = await build(createConfig(`./modules/index-${pathType}.js`, {}));
const testModule = require(bundlePath);
t.is(testModule.fn(), 'foo');
});


test(`should map foo.js to bar.js when using ${pathType} paths`, async t => {
const bundlePath = await build(createConfig(`./modules/index-${pathType}.js`, {
'./modules/foo.js': './modules/bar.js'
}));
const testModule = require(bundlePath);
t.is(testModule.fn(), 'bar');
t.is(testModule.html.trim(), '<div>foo</div>');
});


test(`should map foo.html to bar.html when using ${pathType} paths`, async t => {
const bundlePath = await build(createConfig(`./modules/index-${pathType}.js`, {
'./modules/foo.html': './modules/bar.html'
}));
const testModule = require(bundlePath);
t.is(testModule.fn(), 'foo');
t.is(testModule.html.trim(), '<div>bar</div>');
});


test(`should map both foo.js and foo.html when using ${pathType} paths`, async t => {
const bundlePath = await build(createConfig(`./modules/index-${pathType}.js`, {
'./modules/foo.js': './modules/bar.js',
'./modules/foo.html': './modules/bar.html'
}));
const testModule = require(bundlePath);
t.is(testModule.fn(), 'bar');
t.is(testModule.html.trim(), '<div>bar</div>');
});

}

testPathType('relative');
testPathType('absolute');
testPathType('module');

test.after.always('cleanup dist folder', t => fs.remove(outputPath));
1 change: 1 addition & 0 deletions e2e/modules/bar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>bar</div>
1 change: 1 addition & 0 deletions e2e/modules/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => 'bar';
1 change: 1 addition & 0 deletions e2e/modules/foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>foo</div>
1 change: 1 addition & 0 deletions e2e/modules/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => 'foo';
2 changes: 2 additions & 0 deletions e2e/modules/index-absolute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports.fn = require(__dirname + '/foo');
module.exports.html = require(__dirname + '/foo.html');
2 changes: 2 additions & 0 deletions e2e/modules/index-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports.fn = require('foo');
module.exports.html = require('foo.html');
2 changes: 2 additions & 0 deletions e2e/modules/index-relative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports.fn = require('./foo');
module.exports.html = require('./foo.html');
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@
"devDependencies": {
"ava": "^0.16.0",
"babel-eslint": "^7.0.0",
"eslint": "^3.8.1"
"eslint": "^3.8.1",
"fs-extra": "^0.30.0",
"raw-loader": "^0.5.1",
"webpack": "^1.13.2"
},
"publishConfig": {
"registry": "https://npm.spartez.com/repository/npm-private"
},
"ava": {
"files": [
"test/index.js",
"e2e/index.js"
],
"verbose": true
}
}
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function handleRequest(fn, request) {
}));
}

test.cb(t => {
test.cb('should map foo.js to bar.js', t => {
const plugin = ModuleMappingPlugin({
'./foo.js': './bar.js'
});
Expand Down

0 comments on commit 3b6dd43

Please sign in to comment.