Skip to content

Commit

Permalink
Improve way how System is tested
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Apr 10, 2019
1 parent 39a680d commit f7d0c25
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 163 deletions.
74 changes: 38 additions & 36 deletions test/ConfigTestCases.test.js
Expand Up @@ -174,7 +174,6 @@ describe("ConfigTestCases", () => {

function _require(currentDirectory, module) {
if (Array.isArray(module) || /^\.\.?\//.test(module)) {
let fn;
let content;
let p;
if (Array.isArray(module)) {
Expand All @@ -189,45 +188,47 @@ describe("ConfigTestCases", () => {
p = path.join(currentDirectory, module);
content = fs.readFileSync(p, "utf-8");
}
if (
options.target === "web" ||
options.target === "webworker"
) {
fn = vm.runInNewContext(
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach, expect, jest, window) {" +
'function nsObj(m) { Object.defineProperty(m, Symbol.toStringTag, { value: "Module" }); return m; }' +
content +
"\n})",
globalContext,
p
);
} else {
fn = vm.runInThisContext(
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach, expect, jest) {" +
"global.expect = expect; " +
'function nsObj(m) { Object.defineProperty(m, Symbol.toStringTag, { value: "Module" }); return m; }' +
content +
"\n})",
p
);
}
const m = {
exports: {}
};
fn.call(
m.exports,
_require.bind(null, path.dirname(p)),
m,
m.exports,
path.dirname(p),
p,
_it,
_beforeEach,
_afterEach,
let runInNewContext = false;
const moduleScope = {
require: _require.bind(null, path.dirname(p)),
module: m,
exports: m.exports,
__dirname: path.dirname(p),
__filename: p,
it: _it,
beforeEach: _beforeEach,
afterEach: _afterEach,
expect,
jest,
globalContext
);
_globalAssign: { expect },
nsObj: m => {
Object.defineProperty(m, Symbol.toStringTag, {
value: "Module"
});
return m;
}
};
if (
options.target === "web" ||
options.target === "webworker"
) {
moduleScope.window = globalContext;
runInNewContext = true;
}
if (testConfig.moduleScope) {
testConfig.moduleScope(moduleScope);
}
const args = Object.keys(moduleScope).join(", ");
if (!runInNewContext)
content = `Object.assign(global, _globalAssign); ${content}`;
const code = `(function({${args}}) {${content}\n})`;
const fn = runInNewContext
? vm.runInNewContext(code, globalContext, p)
: vm.runInThisContext(code, p);
fn.call(m.exports, moduleScope);
return m.exports;
} else if (
testConfig.modules &&
Expand All @@ -239,6 +240,7 @@ describe("ConfigTestCases", () => {
let filesCount = 0;

if (testConfig.noTests) return process.nextTick(done);
if (testConfig.beforeExecute) testConfig.beforeExecute();
for (let i = 0; i < optionsArr.length; i++) {
const bundlePath = testConfig.findBundle(i, optionsArr[i]);
if (bundlePath) {
Expand All @@ -256,9 +258,9 @@ describe("ConfigTestCases", () => {
"Should have found at least one bundle file per webpack config"
)
);
if (testConfig.afterExecute) testConfig.afterExecute();
if (getNumberOfTests() < filesCount)
return done(new Error("No tests exported by test case"));
if (testConfig.afterExecute) testConfig.afterExecute();
done();
});
})
Expand Down
11 changes: 3 additions & 8 deletions test/configCases/externals/externals-system/index.js
@@ -1,16 +1,11 @@
/* This test verifies that webpack externals are properly indicated as dependencies to System.
* Also that when System provides the external variables to webpack that the variables get plumbed
* through correctly and are usable by the webpack bundle.
*/
afterEach(function(done) {
delete global.System;
done()
})

*/
it("should get an external from System", function() {
const external1 = require("external1");
expect(external1).toBe('the external1 value');
expect(external1).toBe("the external1 value");

const external2 = require("external2");
expect(external2).toBe('the external2 value');
expect(external2).toBe("the external2 value");
});
16 changes: 16 additions & 0 deletions test/configCases/externals/externals-system/test.config.js
@@ -0,0 +1,16 @@
const System = require("../../../helpers/fakeSystem");

module.exports = {
beforeExecute: () => {
System.init({
external1: "the external1 value",
external2: "the external2 value"
});
},
moduleScope(scope) {
scope.System = System;
},
afterExecute: () => {
System.execute("(anonym)");
}
};
24 changes: 1 addition & 23 deletions test/configCases/externals/externals-system/webpack.config.js
@@ -1,31 +1,9 @@
const webpack = require("../../../../");
module.exports = {
output: {
libraryTarget: "system"
},
externals: {
external1: "external1",
external2: "external2"
},
plugins: [
new webpack.BannerPlugin({
raw: true,
banner: `
System = {
register: function(deps, fn) {
function dynamicExport() {}
var mod = fn(dynamicExport);
deps.forEach((dep, i) => {
mod.setters[i](System.registry[dep]);
})
mod.execute();
},
registry: {
external1: 'the external1 value',
external2: 'the external2 value',
},
}
`
})
]
}
};
25 changes: 8 additions & 17 deletions test/configCases/target/system-export/index.js
@@ -1,22 +1,13 @@
// This test verifies that values exported by a webpack bundle are consumable by systemjs.

export const namedThing = {
hello: 'there'
}
hello: "there"
};

export default 'the default export'
export default "the default export";

it("should successfully export values to System", function(done) {
var fs = require("fs");
var source = fs.readFileSync(__filename, "utf-8");

// set timeout allows the webpack bundle to finish exporting, which exports to System at the very
// end of its execution.
setTimeout(() => {
expect(global.SystemExports['default']).toBe('the default export')
expect(global.SystemExports.namedThing).toBe(namedThing)
delete global.System;
delete global.SystemExports
done()
})
});
it("should successfully export values to System", function() {
const exports = eval("System").registry["(anonym)"].exports;
expect(exports["default"]).toBe("the default export");
expect(exports.namedThing).toBe(namedThing);
});
13 changes: 13 additions & 0 deletions test/configCases/target/system-export/test.config.js
@@ -0,0 +1,13 @@
const System = require("../../../helpers/fakeSystem");

module.exports = {
beforeExecute: () => {
System.init();
},
moduleScope(scope) {
scope.System = System;
},
afterExecute: () => {
System.execute("(anonym)");
}
};
21 changes: 1 addition & 20 deletions test/configCases/target/system-export/webpack.config.js
@@ -1,28 +1,9 @@
const webpack = require("../../../../");
module.exports = {
output: {
libraryTarget: "system"
},
node: {
__dirname: false,
__filename: false
},
plugins: [
new webpack.BannerPlugin({
raw: true,
banner: `
global.SystemExports = {
'export': function(exports) {
Object.assign(global.SystemExports, exports);
}
};
global.System = {
register: function(deps, fn) {
var mod = fn(global.SystemExports['export']);
mod.execute();
}
};
`
})
]
}
};
16 changes: 3 additions & 13 deletions test/configCases/target/system-named/index.js
@@ -1,15 +1,5 @@
/* This test verifies that when output.library is specified that the compiled bundle provides
* the library name to System during the System.register
*/
* the library name to System during the System.register
*/

afterEach(function(done) {
delete global.System;
done()
})

it("should call System.register with a name", function() {
var fs = require("fs");
var source = fs.readFileSync(__filename, "utf-8");

expect(source).toMatch(/.*System\.register\("named-system-module", \[[^\]]*\]/);
});
it("should call System.register with a name", function() {});
13 changes: 13 additions & 0 deletions test/configCases/target/system-named/test.config.js
@@ -0,0 +1,13 @@
const System = require("../../../helpers/fakeSystem");

module.exports = {
beforeExecute: () => {
System.init();
},
moduleScope(scope) {
scope.System = System;
},
afterExecute: () => {
System.execute("named-system-module");
}
};
17 changes: 1 addition & 16 deletions test/configCases/target/system-named/webpack.config.js
@@ -1,4 +1,3 @@
const webpack = require("../../../../");
module.exports = {
output: {
library: "named-system-module",
Expand All @@ -7,19 +6,5 @@ module.exports = {
node: {
__dirname: false,
__filename: false
},
plugins: [
new webpack.BannerPlugin({
raw: true,
banner: `
System = {
register: function(name, deps, fn) {
function dynamicExport() {}
var mod = fn(dynamicExport);
mod.execute();
}
}
`
})
]
}
};
16 changes: 3 additions & 13 deletions test/configCases/target/system-unnamed/index.js
@@ -1,15 +1,5 @@
/* This test verifies that when there is no output.library specified that the call to
* System.register does not include a name argument.
*/
* System.register does not include a name argument.
*/

afterEach(function(done) {
delete global.System;
done()
})

it("should call System.register without a name", function() {
var fs = require("fs");
var source = fs.readFileSync(__filename, "utf-8");

expect(source).toMatch(/.*System\.register\(\[[^\]]*\], function\(__WEBPACK_DYNAMIC_EXPORT__\) {\s+(var .*;)?\s*return \{\s+setters: [^]+,[^]+execute: function\(\) {/);
});
it("should call System.register without a name", function() {});
13 changes: 13 additions & 0 deletions test/configCases/target/system-unnamed/test.config.js
@@ -0,0 +1,13 @@
const System = require("../../../helpers/fakeSystem");

module.exports = {
beforeExecute: () => {
System.init();
},
moduleScope(scope) {
scope.System = System;
},
afterExecute: () => {
System.execute("(anonym)");
}
};
18 changes: 1 addition & 17 deletions test/configCases/target/system-unnamed/webpack.config.js
@@ -1,25 +1,9 @@
const webpack = require("../../../../");
module.exports = {
output: {
libraryTarget: "system"
},
node: {
__dirname: false,
__filename: false
},
plugins: [
new webpack.BannerPlugin({
raw: true,
banner: `
System = {
register: function(deps, fn) {
function dynamicExport() {}
var mod = fn(dynamicExport)
mod.execute()
}
}
`
})
]
}
};

0 comments on commit f7d0c25

Please sign in to comment.