Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Commit

Permalink
refactor: test
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jul 10, 2020
1 parent f729e34 commit 800b074
Show file tree
Hide file tree
Showing 64 changed files with 3,283 additions and 3,670 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/dist
/node_modules
/test/fixtures
/test/__expected__
/test/outputs
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ npm-debug.log*
/local
/reports
/node_modules
/test/__expected__
/test/outputs
.DS_Store
Thumbs.db
.idea
Expand Down
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
/dist
/node_modules
/test/fixtures
/test/__expected__
/test/outputs
CHANGELOG.md
10 changes: 10 additions & 0 deletions globalSetupTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import path from 'path';

// eslint-disable-next-line import/no-extraneous-dependencies
import del from 'del';

async function setup() {
await del(path.resolve(__dirname, `./test/outputs`));
}

module.exports = setup;
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
testEnvironment: 'node',
globalSetup: '<rootDir>/globalSetupTest.js',
setupFilesAfterEnv: ['<rootDir>/setupTest.js'],
};
5,991 changes: 2,658 additions & 3,333 deletions package-lock.json

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,31 @@
"loader-utils": "^2.0.0"
},
"devDependencies": {
"@babel/cli": "^7.10.1",
"@babel/core": "^7.10.2",
"@babel/preset-env": "^7.10.2",
"@commitlint/cli": "^7.0.0",
"@commitlint/config-conventional": "^7.0.1",
"@babel/cli": "^7.10.4",
"@babel/core": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"@commitlint/cli": "^9.0.1",
"@commitlint/config-conventional": "^9.0.1",
"@webpack-contrib/defaults": "^6.3.0",
"@webpack-contrib/eslint-config-webpack": "^2.0.4",
"babel-jest": "^26.0.1",
"commitlint-azure-pipelines-cli": "^1.0.3",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^26.1.0",
"cross-env": "^7.0.2",
"del": "^5.1.0",
"del-cli": "^3.0.1",
"eslint": "^7.2.0",
"eslint": "^7.4.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-import": "^2.22.0",
"express": "^4.17.1",
"get-port": "^5.1.1",
"html-webpack-plugin": "^4.3.0",
"husky": "^0.14.3",
"jest": "^26.0.1",
"jest-junit": "^10.0.0",
"lint-staged": "^10.2.10",
"husky": "^4.2.5",
"jest": "^26.1.0",
"lint-staged": "^10.2.11",
"memfs": "^3.2.0",
"nanoid": "^3.1.10",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"puppeteer": "^4.0.1",
"puppeteer": "^5.0.0",
"standard-version": "^8.0.0",
"webpack": "^4.43.0"
},
Expand Down
1 change: 1 addition & 0 deletions setupTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jest.setTimeout(60000);
4 changes: 0 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export function pitch(request) {
baseDataPath: 'options',
});

validateOptions({ name: 'Worker Loader', schema, target: options });

this.cacheable(false);

const cb = this.async();
Expand All @@ -47,8 +45,6 @@ export function pitch(request) {
worker.options
);

// Tapable.apply is deprecated in tapable@1.0.0-x.
// The plugins should now call apply themselves.
new WebWorkerTemplatePlugin(worker.options).apply(worker.compiler);

if (this.target !== 'webworker' && this.target !== 'web') {
Expand Down
12 changes: 7 additions & 5 deletions src/supportWebpack4.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import getWorker from './workers';

export default function runAsChild(worker, request, options, cb) {
export default function runAsChild(worker, request, options, callback) {
const subCache = `subcache ${__dirname} ${request}`;

// eslint-disable-next-line no-param-reassign
Expand All @@ -17,8 +17,10 @@ export default function runAsChild(worker, request, options, cb) {
};

worker.compiler.hooks.compilation.tap('WorkerLoader', worker.compilation);
worker.compiler.runAsChild((err, entries, compilation) => {
if (err) return cb(err);
worker.compiler.runAsChild((error, entries, compilation) => {
if (error) {
return callback(error);
}

if (entries[0]) {
// eslint-disable-next-line no-param-reassign, prefer-destructuring
Expand All @@ -35,12 +37,12 @@ export default function runAsChild(worker, request, options, cb) {
delete this._compilation.assets[worker.file];
}

return cb(
return callback(
null,
`module.exports = function() {\n return ${worker.factory};\n};`
);
}

return cb(null, null);
return callback(null, null);
});
}
18 changes: 9 additions & 9 deletions src/supportWebpack5.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import getWorker from './workers';

export default function runAsChild(worker, options, cb) {
export default function runAsChild(worker, options, callback) {
// eslint-disable-next-line import/no-unresolved, global-require
const getLazyHashedEtag = require('webpack/lib/cache/getLazyHashedEtag');

worker.compiler.runAsChild((err, entries, compilation) => {
if (err) {
return cb(err);
worker.compiler.runAsChild((error, entries, compilation) => {
if (error) {
return callback(error);
}

if (entries[0]) {
Expand All @@ -21,15 +21,15 @@ export default function runAsChild(worker, options, cb) {
cacheETag,
(getCacheError, content) => {
if (getCacheError) {
return cb(getCacheError);
return callback(getCacheError);
}

if (options.fallback === false) {
delete this._compilation.assets[worker.file];
}

if (content) {
return cb(null, content);
return callback(null, content);
}

// eslint-disable-next-line no-param-reassign
Expand All @@ -47,16 +47,16 @@ export default function runAsChild(worker, options, cb) {
newContent,
(storeCacheError) => {
if (storeCacheError) {
return cb(storeCacheError);
return callback(storeCacheError);
}

return cb(null, newContent);
return callback(null, newContent);
}
);
}
);
}

return cb(null, null);
return callback(null, null);
});
}
10 changes: 3 additions & 7 deletions src/workers/InlineWorker.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// http://stackoverflow.com/questions/10343913/how-to-create-a-web-worker-from-a-string

/* eslint-env browser */
/* eslint-disable no-var, vars-on-top, prefer-template */

var URL = window.URL || window.webkitURL;
/* eslint-disable no-var, vars-on-top, prefer-template, no-undef, no-use-before-define */
var URL = URL || webkitURL;

function CreateWorker(url, workerType) {
switch (workerType) {
Expand All @@ -24,10 +23,7 @@ module.exports = function inlineWorker(content, url, workerType) {
try {
// BlobBuilder = Deprecated, but widely implemented
var BlobBuilder =
window.BlobBuilder ||
window.WebKitBlobBuilder ||
window.MozBlobBuilder ||
window.MSBlobBuilder;
BlobBuilder || WebKitBlobBuilder || MozBlobBuilder || MSBlobBuilder;

blob = new BlobBuilder();

Expand Down
19 changes: 19 additions & 0 deletions test/__snapshots__/fallback-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"fallback" option should should work with "false" value: errors 1`] = `Array []`;

exports[`"fallback" option should should work with "false" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"fallback" option should should work with "false" value: warnings 1`] = `Array []`;

exports[`"fallback" option should should work with "true" value: errors 1`] = `Array []`;

exports[`"fallback" option should should work with "true" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"fallback" option should should work with "true" value: warnings 1`] = `Array []`;

exports[`"fallback" option should work by default: errors 1`] = `Array []`;

exports[`"fallback" option should work by default: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"fallback" option should work by default: warnings 1`] = `Array []`;
31 changes: 31 additions & 0 deletions test/__snapshots__/inline-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"inline" option should not work by default: errors 1`] = `Array []`;

exports[`"inline" option should not work by default: module 1`] = `
"module.exports = function() {
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
};"
`;

exports[`"inline" option should not work by default: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"inline" option should not work by default: warnings 1`] = `Array []`;

exports[`"inline" option should not work with "false" value: errors 1`] = `Array []`;

exports[`"inline" option should not work with "false" value: module 1`] = `
"module.exports = function() {
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
};"
`;

exports[`"inline" option should not work with "false" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"inline" option should not work with "false" value: warnings 1`] = `Array []`;

exports[`"inline" option should work with "true" value: errors 1`] = `Array []`;

exports[`"inline" option should work with "true" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"inline" option should work with "true" value: warnings 1`] = `Array []`;
24 changes: 23 additions & 1 deletion test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`worker-loader should work worker through puppeteer: result 1`] = `"{\\"a\\":1,\\"worker\\":\\"passed\\"}"`;
exports[`worker-loader should work with inline syntax: errors 1`] = `Array []`;

exports[`worker-loader should work with inline syntax: module 1`] = `
"module.exports = function() {
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
};"
`;

exports[`worker-loader should work with inline syntax: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`worker-loader should work with inline syntax: warnings 1`] = `Array []`;

exports[`worker-loader should work: errors 1`] = `Array []`;

exports[`worker-loader should work: module 1`] = `
"module.exports = function() {
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
};"
`;

exports[`worker-loader should work: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`worker-loader should work: warnings 1`] = `Array []`;
13 changes: 13 additions & 0 deletions test/__snapshots__/name-options.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"name" option should work: errors 1`] = `Array []`;

exports[`"name" option should work: module 1`] = `
"module.exports = function() {
return new Worker(__webpack_public_path__ + \\"my-custom-name.js\\");
};"
`;

exports[`"name" option should work: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"name" option should work: warnings 1`] = `Array []`;
13 changes: 13 additions & 0 deletions test/__snapshots__/publicPath.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"publicPath" option should use "__webpack_public_path__" by default: errors 1`] = `Array []`;

exports[`"publicPath" option should use "__webpack_public_path__" by default: module 1`] = `
"module.exports = function() {
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
};"
`;

exports[`"publicPath" option should use "__webpack_public_path__" by default: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"publicPath" option should use "__webpack_public_path__" by default: warnings 1`] = `Array []`;
13 changes: 13 additions & 0 deletions test/__snapshots__/workerType-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"workerType" option should use "Worker" by default: errors 1`] = `Array []`;

exports[`"workerType" option should use "Worker" by default: module 1`] = `
"module.exports = function() {
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
};"
`;

exports[`"workerType" option should use "Worker" by default: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;

exports[`"workerType" option should use "Worker" by default: warnings 1`] = `Array []`;
8 changes: 8 additions & 0 deletions test/cjs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import src from '../src';
import cjs from '../src/cjs';

describe('cjs', () => {
it('should exported', () => {
expect(cjs).toEqual(src);
});
});

0 comments on commit 800b074

Please sign in to comment.