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,6 +212,15 @@ const describeCases = config => {
return;
}
let filesCount = 0;
if (testConfig.noTests) return process.nextTick(done);
if (testConfig.beforeExecute) testConfig.beforeExecute();
const results = [];
for (let i = 0; i < optionsArr.length; i++) {
const bundlePath = testConfig.findBundle(i, optionsArr[i]);
if (bundlePath) {
filesCount++;
const globalContext = {
console: console,
expect: expect,
@ -228,7 +237,8 @@ const describeCases = config => {
};
const requireCache = Object.create(null);
function _require(currentDirectory, options, module) {
// eslint-disable-next-line no-loop-func
const _require = (currentDirectory, options, module) => {
if (Array.isArray(module) || /^\.\.?\//.test(module)) {
let content;
let p;
@ -252,12 +262,14 @@ const describeCases = config => {
requireCache[p] = m;
let runInNewContext = false;
const moduleScope = {
require: _require.bind(null, path.dirname(p), options),
importScripts: _require.bind(
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),
@ -302,16 +314,8 @@ const describeCases = config => {
) {
return testConfig.modules[module];
} else return require(module);
}
let filesCount = 0;
};
if (testConfig.noTests) return process.nextTick(done);
if (testConfig.beforeExecute) testConfig.beforeExecute();
const results = [];
for (let i = 0; i < optionsArr.length; i++) {
const bundlePath = testConfig.findBundle(i, optionsArr[i]);
if (bundlePath) {
filesCount++;
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
})
);
});