add test case

This commit is contained in:
Tobias Koppers 2020-08-27 11:11:40 +02:00
parent 14a0aff16c
commit 30856a6750
6 changed files with 140 additions and 91 deletions

View File

@ -212,97 +212,6 @@ const describeCases = config => {
return;
}
const globalContext = {
console: console,
expect: expect,
setTimeout: setTimeout,
clearTimeout: clearTimeout,
document: new FakeDocument(),
location: {
href: "https://test.cases/path/index.html",
origin: "https://test.cases",
toString() {
return "https://test.cases/path/index.html";
}
}
};
const requireCache = Object.create(null);
function _require(currentDirectory, options, module) {
if (Array.isArray(module) || /^\.\.?\//.test(module)) {
let content;
let p;
if (Array.isArray(module)) {
p = path.join(currentDirectory, ".array-require.js");
content = `module.exports = (${module
.map(arg => {
return `require(${JSON.stringify(`./${arg}`)})`;
})
.join(", ")});`;
} else {
p = path.join(currentDirectory, module);
content = fs.readFileSync(p, "utf-8");
}
if (p in requireCache) {
return requireCache[p].exports;
}
const m = {
exports: {}
};
requireCache[p] = m;
let runInNewContext = false;
const moduleScope = {
require: _require.bind(null, path.dirname(p), options),
importScripts: _require.bind(
null,
path.dirname(p),
options
),
module: m,
exports: m.exports,
__dirname: path.dirname(p),
__filename: p,
it: _it,
beforeEach: _beforeEach,
afterEach: _afterEach,
expect,
jest,
_globalAssign: { expect },
__STATS__: jsonStats,
nsObj: m => {
Object.defineProperty(m, Symbol.toStringTag, {
value: "Module"
});
return m;
}
};
if (
options.target === "web" ||
options.target === "webworker"
) {
moduleScope.window = globalContext;
moduleScope.self = 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 &&
module in testConfig.modules
) {
return testConfig.modules[module];
} else return require(module);
}
let filesCount = 0;
if (testConfig.noTests) return process.nextTick(done);
@ -312,6 +221,101 @@ const describeCases = config => {
const bundlePath = testConfig.findBundle(i, optionsArr[i]);
if (bundlePath) {
filesCount++;
const globalContext = {
console: console,
expect: expect,
setTimeout: setTimeout,
clearTimeout: clearTimeout,
document: new FakeDocument(),
location: {
href: "https://test.cases/path/index.html",
origin: "https://test.cases",
toString() {
return "https://test.cases/path/index.html";
}
}
};
const requireCache = Object.create(null);
// eslint-disable-next-line no-loop-func
const _require = (currentDirectory, options, module) => {
if (Array.isArray(module) || /^\.\.?\//.test(module)) {
let content;
let p;
if (Array.isArray(module)) {
p = path.join(currentDirectory, ".array-require.js");
content = `module.exports = (${module
.map(arg => {
return `require(${JSON.stringify(`./${arg}`)})`;
})
.join(", ")});`;
} else {
p = path.join(currentDirectory, module);
content = fs.readFileSync(p, "utf-8");
}
if (p in requireCache) {
return requireCache[p].exports;
}
const m = {
exports: {}
};
requireCache[p] = m;
let runInNewContext = false;
const moduleScope = {
require: _require.bind(
null,
path.dirname(p),
options
),
importScripts: url => {
_require(path.dirname(p), options, `./${url}`);
},
module: m,
exports: m.exports,
__dirname: path.dirname(p),
__filename: p,
it: _it,
beforeEach: _beforeEach,
afterEach: _afterEach,
expect,
jest,
_globalAssign: { expect },
__STATS__: jsonStats,
nsObj: m => {
Object.defineProperty(m, Symbol.toStringTag, {
value: "Module"
});
return m;
}
};
if (
options.target === "web" ||
options.target === "webworker"
) {
moduleScope.window = globalContext;
moduleScope.self = 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 &&
module in testConfig.modules
) {
return testConfig.modules[module];
} else return require(module);
};
results.push(
_require(outputDirectory, optionsArr[i], bundlePath)
);

View File

@ -0,0 +1 @@
export default 42;

View File

@ -0,0 +1,5 @@
module.exports = {
findBundle: function (i, options) {
return i === 0 ? "./web-0.js" : "./webworker-1.js";
}
};

View File

@ -0,0 +1,13 @@
it("should allow to load a shared chunk in web", async () => {
const promise = import(/* webpackChunkName: "chunk" */ "./chunk");
expect(document.head._children).toHaveLength(1);
const script = document.head._children[0];
__non_webpack_require__("./chunk-0.js");
script.onload();
expect(await promise).toEqual(
nsObj({
default: 42
})
);
});

View File

@ -0,0 +1,19 @@
const base = {
entry: {
web: "./web",
webworker: {
import: "./webworker",
chunkLoading: "import-scripts"
}
},
output: {
globalObject: "(typeof self === 'undefined' ? window : self)"
},
target: "web"
};
/** @type {import("../../../../").Configuration[]} */
module.exports = [
{ ...base, output: { ...base.output, filename: "[name]-0.js" } },
{ ...base, output: { ...base.output, filename: "[name]-1.js" } }
];

View File

@ -0,0 +1,7 @@
it("should allow to load a shared chunk in a WebWorker", async () => {
expect(await import(/* webpackChunkName: "chunk" */ "./chunk")).toEqual(
nsObj({
default: 42
})
);
});