Skip to content

Commit

Permalink
test: split tests for webpack 5 (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
smelukov authored and evilebottnawi committed Dec 10, 2019
1 parent b33ff1a commit 47361d6
Show file tree
Hide file tree
Showing 39 changed files with 8,926 additions and 132 deletions.
9 changes: 3 additions & 6 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ jobs:
node-10-canary:
node_version: ^10.13.0
webpack_version: next
continue_on_error: true
steps:
- task: NodeTool@0
inputs:
Expand All @@ -82,7 +81,7 @@ jobs:
displayName: 'Install dependencies'
- script: npm i webpack@$(webpack_version)
displayName: 'Install "webpack@$(webpack_version)"'
- script: npm run test:coverage -- --ci --reporters="default" --reporters="jest-junit" || $(continue_on_error)
- script: npm run test:coverage -- --ci --snapshot-resolver="./test/helpers/snapshotResolver.js" --reporters="default" --reporters="jest-junit" || $(continue_on_error)
displayName: 'Run tests with coverage'
- task: PublishTestResults@2
inputs:
Expand Down Expand Up @@ -115,7 +114,6 @@ jobs:
node-10-canary:
node_version: ^10.13.0
webpack_version: next
continue_on_error: true
steps:
- task: NodeTool@0
inputs:
Expand All @@ -139,7 +137,7 @@ jobs:
displayName: 'Install dependencies'
- script: npm i webpack@$(webpack_version)
displayName: 'Install "webpack@$(webpack_version)"'
- script: npm run test:coverage -- --ci --reporters="default" --reporters="jest-junit" || $(continue_on_error)
- script: npm run test:coverage -- --ci --snapshot-resolver="./test/helpers/snapshotResolver.js" --reporters="default" --reporters="jest-junit" || $(continue_on_error)
displayName: 'Run tests with coverage'
- task: PublishTestResults@2
inputs:
Expand Down Expand Up @@ -172,7 +170,6 @@ jobs:
node-10-canary:
node_version: ^10.13.0
webpack_version: next
continue_on_error: true
steps:
- script: 'git config --global core.autocrlf input'
displayName: 'Config git core.autocrlf'
Expand All @@ -199,7 +196,7 @@ jobs:
displayName: 'Install dependencies'
- script: npm i webpack@$(webpack_version)
displayName: 'Install "webpack@$(webpack_version)"'
- script: npm run test:coverage -- --ci --reporters="default" --reporters="jest-junit" || $(continue_on_error)
- script: npm run test:coverage -- --ci --snapshot-resolver="./test/helpers/snapshotResolver.js" --reporters="default" --reporters="jest-junit" || $(continue_on_error)
displayName: 'Run tests with coverage'
- task: PublishTestResults@2
inputs:
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
testEnvironment: 'node',
snapshotResolver: './test/helpers/snapshotResolver.js',
};
116 changes: 78 additions & 38 deletions test/TerserPlugin.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import RequestShortener from 'webpack/lib/RequestShortener';
import { javascript } from 'webpack';
import MainTemplate from 'webpack/lib/MainTemplate';
import ChunkTemplate from 'webpack/lib/ChunkTemplate';

Expand Down Expand Up @@ -51,7 +52,7 @@ describe('TerserPlugin', () => {
{
mode: 'production',
bail: true,
cache: false,
cache: getCompiler.isWebpack4() ? false : { type: 'memory' },
entry: `${__dirname}/fixtures/entry.js`,
output: {
path: `${__dirname}/dist`,
Expand All @@ -65,7 +66,7 @@ describe('TerserPlugin', () => {
{
mode: 'production',
bail: true,
cache: false,
cache: getCompiler.isWebpack4() ? false : { type: 'memory' },
entry: `${__dirname}/fixtures/entry.js`,
output: {
path: `${__dirname}/dist`,
Expand All @@ -80,7 +81,7 @@ describe('TerserPlugin', () => {
{
mode: 'production',
bail: true,
cache: false,
cache: getCompiler.isWebpack4() ? false : { type: 'memory' },
entry: `${__dirname}/fixtures/import-export/entry.js`,
output: {
path: `${__dirname}/dist-MultiCompiler`,
Expand Down Expand Up @@ -186,50 +187,89 @@ describe('TerserPlugin', () => {
});

it('should regenerate hash', async () => {
const originalMainTemplateUpdateHashForChunk =
MainTemplate.prototype.updateHashForChunk;
const originalChunkTemplateUpdateHashForChunk =
ChunkTemplate.prototype.updateHashForChunk;
const mockMainTemplateUpdateHashForChunk = jest.fn();
const mockChunkTemplateUpdateHashFocChunk = jest.fn();
if (getCompiler.isWebpack4()) {
const originalMainTemplateUpdateHashForChunk =
MainTemplate.prototype.updateHashForChunk;
const originalChunkTemplateUpdateHashForChunk =
ChunkTemplate.prototype.updateHashForChunk;
const mockMainTemplateUpdateHashForChunk = jest.fn();
const mockChunkTemplateUpdateHashFocChunk = jest.fn();

MainTemplate.prototype.updateHashForChunk = mockMainTemplateUpdateHashForChunk;
ChunkTemplate.prototype.updateHashForChunk = mockChunkTemplateUpdateHashFocChunk;

const compiler = getCompiler({
entry: {
js: `${__dirname}/fixtures/entry.js`,
mjs: `${__dirname}/fixtures/entry.mjs`,
importExport: `${__dirname}/fixtures/import-export/entry.js`,
AsyncImportExport: `${__dirname}/fixtures/async-import-export/entry.js`,
},
output: {
path: `${__dirname}/dist`,
filename: '[name].[contenthash].js',
chunkFilename: '[id].[name].[contenthash].js',
},
});

MainTemplate.prototype.updateHashForChunk = mockMainTemplateUpdateHashForChunk;
ChunkTemplate.prototype.updateHashForChunk = mockChunkTemplateUpdateHashFocChunk;
new TerserPlugin().apply(compiler);

const compiler = getCompiler({
entry: {
js: `${__dirname}/fixtures/entry.js`,
mjs: `${__dirname}/fixtures/entry.mjs`,
importExport: `${__dirname}/fixtures/import-export/entry.js`,
AsyncImportExport: `${__dirname}/fixtures/async-import-export/entry.js`,
},
output: {
path: `${__dirname}/dist`,
filename: '[name].[contenthash].js',
chunkFilename: '[id].[name].[contenthash].js',
},
});
const stats = await compile(compiler);

new TerserPlugin().apply(compiler);
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');

const stats = await compile(compiler);
// On each chunk we have 2 calls (we have 1 async chunk and 4 initial).
// First call do `webpack`.
// Second call do `TerserPlugin`.

// We have 1 async chunk (1 * 2 = 2 calls for ChunkTemplate)
expect(mockMainTemplateUpdateHashForChunk).toHaveBeenCalledTimes(8);
// We have 4 initial chunks (4 * 2 = 8 calls for MainTemplate)
expect(mockChunkTemplateUpdateHashFocChunk).toHaveBeenCalledTimes(2);

expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');

MainTemplate.prototype.updateHashForChunk = originalMainTemplateUpdateHashForChunk;
ChunkTemplate.prototype.updateHashForChunk = originalChunkTemplateUpdateHashForChunk;
} else {
const mockUpdateHashForChunk = jest.fn();
const compiler = getCompiler({
entry: {
js: `${__dirname}/fixtures/entry.js`,
mjs: `${__dirname}/fixtures/entry.mjs`,
importExport: `${__dirname}/fixtures/import-export/entry.js`,
AsyncImportExport: `${__dirname}/fixtures/async-import-export/entry.js`,
},
output: {
path: `${__dirname}/dist`,
filename: '[name].[contenthash].js',
chunkFilename: '[id].[name].[contenthash].js',
},
});

expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
compiler.hooks.thisCompilation.tap('TerserPlugin', (compilation) => {
javascript.JavascriptModulesPlugin.getCompilationHooks(
compilation
).chunkHash.tap('TerserPlugin', mockUpdateHashForChunk);
});

// On each chunk we have 2 calls (we have 1 async chunk and 4 initial).
// First call do `webpack`.
// Second call do `TerserPlugin`.
new TerserPlugin().apply(compiler);

// We have 1 async chunk (1 * 2 = 2 calls for ChunkTemplate)
expect(mockMainTemplateUpdateHashForChunk).toHaveBeenCalledTimes(8);
// We have 4 initial chunks (4 * 2 = 8 calls for MainTemplate)
expect(mockChunkTemplateUpdateHashFocChunk).toHaveBeenCalledTimes(2);
const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');

// On each chunk we have 2 calls (we have 1 async chunk and 4 initial).
// First call do `webpack`.
// Second call do `TerserPlugin`.

// We have 1 async chunk (1 * 2 = 2 calls) and 4 initial chunks (4 * 2 = 8 calls)
expect(mockUpdateHashForChunk).toHaveBeenCalledTimes(10);

MainTemplate.prototype.updateHashForChunk = originalMainTemplateUpdateHashForChunk;
ChunkTemplate.prototype.updateHashForChunk = originalChunkTemplateUpdateHashForChunk;
expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
}
});

it('isSourceMap method', () => {
Expand Down
Loading

0 comments on commit 47361d6

Please sign in to comment.