Skip to content

Commit ae1f435

Browse files
committedJul 10, 2018
feat: Switch from jasmine to jest
1 parent 82b34a1 commit ae1f435

File tree

22 files changed

+241
-207
lines changed

22 files changed

+241
-207
lines changed
 

‎README.md

+15-9
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,19 @@ plugins: [
287287

288288
### `Events`
289289

290-
To allow other [plugins](https://github.com/webpack/docs/wiki/plugins) to alter the HTML this plugin executes the following events:
290+
To allow other [plugins](https://github.com/webpack/docs/wiki/plugins) to alter the HTML this plugin executes
291+
[tapable](https://github.com/webpack/tapable/tree/master) hooks.
291292

292-
#### `AsyncSeriesWaterfallHook`
293+
The [lib/hooks.js](https://github.com/jantimon/html-webpack-plugin/blob/master/lib/hooks.js) contains all information
294+
about which values are passed.
293295

294-
* `htmlWebpackPluginBeforeHtmlGeneration`
295-
* `htmlWebpackPluginBeforeHtmlProcessing`
296-
* `htmlWebpackPluginAlterAssetTags`
297-
* `htmlWebpackPluginAfterHtmlProcessing`
298-
* `htmlWebpackPluginAfterEmit`
296+
You can tap into the following async hooks:
297+
298+
* `beforeHtmlGeneration`
299+
* `beforeHtmlProcessing`
300+
* `alterAssetTags`
301+
* `afterHtmlProcessing`
302+
* `afterEmit`
299303

300304
Example implementation: [html-webpack-harddisk-plugin](https://github.com/jantimon/html-webpack-harddisk-plugin)
301305

@@ -308,8 +312,10 @@ function MyPlugin(options) {
308312
MyPlugin.prototype.apply = function (compiler) {
309313
compiler.hooks.compilation.tap('MyPlugin', (compilation) => {
310314
console.log('The compiler is starting a new compilation...');
311-
HtmlWebpackPlugin.getHooks(compilation).htmlWebpackPluginAfterHtmlProcessing.tapAsync(
312-
'MyPlugin',
315+
316+
// | HOOK NAME |
317+
HtmlWebpackPlugin.getHooks(compilation).afterHtmlProcessing.tapAsync(
318+
'MyPlugin', // <-- Set a meaningful name here for stacktraces
313319
(data, cb) => {
314320
data.html += 'The Magic Footer'
315321

‎examples/appcache/dist/webpack-4/manifest.appcache

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CACHE MANIFEST
2-
# 95a89258cd32fe3f01e3
2+
# a351a7eb0665a7fa27b3
33

44
0714810ae3fb211173e2964249507195.png
55
bundle.js

‎examples/appcache/webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ module.exports = {
1313
filename: 'bundle.js'
1414
},
1515
module: {
16-
loaders: [
17-
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') },
16+
rules: [
17+
{ test: /\.css$/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' }) },
1818
{ test: /\.png$/, loader: 'file-loader' },
1919
{ test: /\.html$/, loader: 'html-loader?-removeOptionalTags' }
2020
]

‎examples/build-examples.js

-15
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@ var fs = require('fs');
1010
var path = require('path');
1111
var rimraf = require('rimraf');
1212
var webpack = require('webpack');
13-
var ExtractTextPlugin = require('extract-text-webpack-plugin');
14-
15-
if (Number(webpackMajorVersion) > 1) {
16-
var extractOriginal = ExtractTextPlugin.extract;
17-
ExtractTextPlugin.extract = function (fallback, use) {
18-
return extractOriginal({
19-
fallback: fallback,
20-
use: use
21-
});
22-
};
23-
}
2413

2514
var examples = fs.readdirSync(__dirname).filter(function (file) {
2615
return fs.statSync(path.join(__dirname, file)).isDirectory();
@@ -39,10 +28,6 @@ examples.forEach(function (exampleName) {
3928
}));
4029
config.mode = 'production';
4130
config.optimization = { minimizer: [] };
42-
if (config.module && config.module.loaders) {
43-
config.module.rules = config.module.loaders;
44-
delete config.module.loaders;
45-
}
4631
}
4732

4833
rimraf.sync(path.join(examplePath, 'dist', 'webpack-' + webpackMajorVersion));

‎examples/custom-template/webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ module.exports = {
1212
filename: 'bundle.js'
1313
},
1414
module: {
15-
loaders: [
16-
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') },
15+
rules: [
16+
{ test: /\.css$/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' }) },
1717
{ test: /\.png$/, loader: 'file-loader' }
1818
]
1919
},

‎examples/default/webpack.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
filename: 'bundle.js'
1111
},
1212
module: {
13-
loaders: [
13+
rules: [
1414
{ test: /\.css$/, loader: 'style-loader!css-loader' },
1515
{ test: /\.png$/, loader: 'file-loader' }
1616
]

‎examples/favicon/webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ module.exports = {
1111
filename: 'bundle.js'
1212
},
1313
module: {
14-
loaders: [
15-
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') },
14+
rules: [
15+
{ test: /\.css$/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' }) },
1616
{ test: /\.png$/, loader: 'file-loader' }
1717
]
1818
},

‎examples/html-loader/webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ module.exports = {
1111
filename: 'bundle.js'
1212
},
1313
module: {
14-
loaders: [
15-
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') },
14+
rules: [
15+
{ test: /\.css$/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' }) },
1616
{ test: /\.png$/, loader: 'file-loader' },
1717
{ test: /\.html$/, loader: 'html-loader' }
1818
]

‎examples/inline/webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ module.exports = {
1212
filename: 'bundle.js'
1313
},
1414
module: {
15-
loaders: [
16-
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') },
15+
rules: [
16+
{ test: /\.css$/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' }) },
1717
{ test: /\.jade$/, loader: 'jade-loader' }
1818
]
1919
},
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!DOCTYPE html><html><head><title>Jade demo</title><link rel="shortcut icon" href="favicon.ico"><link href="styles.css" rel="stylesheet"></head><body><div id="main"><!-- this partial is used for frontend and backend--><div class="time"> <b>Current time</b><p>1999-01-01T05:00:00.000Z</p></div><img src="0714810ae3fb211173e2964249507195.png"></div><script src="bundle.js"></script></body></html>
1+
<!DOCTYPE html><html><head><title>Jade demo</title><link rel="shortcut icon" href="favicon.ico"><link href="styles.css" rel="stylesheet"></head><body><div id="main"><!-- this partial is used for frontend and backend--><div class="time"> <b>Current time</b><p>1998-12-31T23:00:00.000Z</p></div><img src="0714810ae3fb211173e2964249507195.png"></div><script src="bundle.js"></script></body></html>

‎examples/jade-loader/webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ module.exports = {
1111
filename: 'bundle.js'
1212
},
1313
module: {
14-
loaders: [
15-
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') },
14+
rules: [
15+
{ test: /\.css$/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' }) },
1616
{ test: /\.png$/, loader: 'file-loader' },
1717
{ test: /\.jade$/, loader: 'jade-loader' }
1818
]

‎examples/javascript-advanced/webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ module.exports = {
1111
filename: 'bundle.js'
1212
},
1313
module: {
14-
loaders: [
15-
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') },
14+
rules: [
15+
{ test: /\.css$/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' }) },
1616
{ test: /\.png$/, loader: 'file-loader' },
1717
{ test: /\.html$/, loader: 'html-loader' }
1818
]
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<head><link href="styles.css" rel="stylesheet"></head>Hello World from backend2018-02-14T04:00:28.077Z<h2>Partial</h2>
1+
<head><link href="styles.css" rel="stylesheet"></head>Hello World from backend2018-07-09T17:14:39.242Z<h2>Partial</h2>
22
<img src="0714810ae3fb211173e2964249507195.png"><script src="bundle.js"></script>

‎examples/javascript/webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ module.exports = {
1111
filename: 'bundle.js'
1212
},
1313
module: {
14-
loaders: [
15-
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') },
14+
rules: [
15+
{ test: /\.css$/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' }) },
1616
{ test: /\.png$/, loader: 'file-loader' },
1717
{ test: /\.html$/, loader: 'html-loader' }
1818
]

‎examples/sort-manually/webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ module.exports = {
1717
filename: '[name].js'
1818
},
1919
module: {
20-
loaders: [
21-
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') },
20+
rules: [
21+
{ test: /\.css$/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' }) },
2222
{ test: /\.png$/, loader: 'file-loader' },
2323
{ test: /\.html$/, loader: 'html-loader' }
2424
]

‎index.js

+20-20
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ class HtmlWebpackPlugin {
111111
});
112112

113113
compiler.hooks.emit.tapAsync('HtmlWebpackPlugin',
114-
/**
115-
* Hook into the webpack emit phase
116-
* @param {WebpackCompilation} compilation
117-
* @param {() => void} callback
118-
*/
114+
/**
115+
* Hook into the webpack emit phase
116+
* @param {WebpackCompilation} compilation
117+
* @param {() => void} callback
118+
*/
119119
(compilation, callback) => {
120120
// Clear the childCompilerCache
121121
childCompiler.clearCache(compiler);
@@ -156,7 +156,7 @@ class HtmlWebpackPlugin {
156156
});
157157
}
158158
})
159-
// Wait for the compilation to finish
159+
// Wait for the compilation to finish
160160
.then(() => compilationPromise)
161161
.then(compiledTemplate => {
162162
// Allow to use a custom function / string instead
@@ -167,22 +167,22 @@ class HtmlWebpackPlugin {
167167
// and replace it with its content
168168
return self.evaluateCompilationResult(compilation, compiledTemplate);
169169
})
170-
// Allow plugins to make changes to the assets before invoking the template
171-
// This only makes sense to use if `inject` is `false`
172-
.then(compilationResult => getHtmlWebpackPluginHooks(compilation).htmlWebpackPluginBeforeHtmlGeneration.promise({
170+
// Allow plugins to make changes to the assets before invoking the template
171+
// This only makes sense to use if `inject` is `false`
172+
.then(compilationResult => getHtmlWebpackPluginHooks(compilation).beforeHtmlGeneration.promise({
173173
assets: assets,
174174
outputName: self.childCompilationOutputName,
175175
plugin: self
176176
})
177177
.then(() => compilationResult))
178-
// Execute the template
178+
// Execute the template
179179
.then(compilationResult => typeof compilationResult !== 'function'
180180
? compilationResult
181181
: self.executeTemplate(compilationResult, assets, compilation))
182-
// Allow plugins to change the html before assets are injected
182+
// Allow plugins to change the html before assets are injected
183183
.then(html => {
184184
const pluginArgs = {html: html, assets: assets, plugin: self, outputName: self.childCompilationOutputName};
185-
return getHtmlWebpackPluginHooks(compilation).htmlWebpackPluginBeforeHtmlProcessing.promise(pluginArgs);
185+
return getHtmlWebpackPluginHooks(compilation).beforeHtmlProcessing.promise(pluginArgs);
186186
})
187187
.then(result => {
188188
const html = result.html;
@@ -191,42 +191,42 @@ class HtmlWebpackPlugin {
191191
const assetTags = self.generateHtmlTagObjects(assets);
192192
const pluginArgs = {head: assetTags.head, body: assetTags.body, plugin: self, outputName: self.childCompilationOutputName};
193193
// Allow plugins to change the assetTag definitions
194-
return getHtmlWebpackPluginHooks(compilation).htmlWebpackPluginAlterAssetTags.promise(pluginArgs)
194+
return getHtmlWebpackPluginHooks(compilation).alterAssetTags.promise(pluginArgs)
195195
.then(result => self.postProcessHtml(html, assets, { body: result.body, head: result.head })
196196
.then(html => _.extend(result, {html: html, assets: assets})));
197197
})
198-
// Allow plugins to change the html after assets are injected
198+
// Allow plugins to change the html after assets are injected
199199
.then(result => {
200200
const html = result.html;
201201
const assets = result.assets;
202202
const pluginArgs = {html: html, assets: assets, plugin: self, outputName: self.childCompilationOutputName};
203-
return getHtmlWebpackPluginHooks(compilation).htmlWebpackPluginAfterHtmlProcessing.promise(pluginArgs)
203+
return getHtmlWebpackPluginHooks(compilation).afterHtmlProcessing.promise(pluginArgs)
204204
.then(result => result.html);
205205
})
206206
.catch(err => {
207-
// In case anything went wrong the promise is resolved
208-
// with the error message and an error is logged
207+
// In case anything went wrong the promise is resolved
208+
// with the error message and an error is logged
209209
compilation.errors.push(prettyError(err, compiler.context).toString());
210210
// Prevent caching
211211
self.hash = null;
212212
return self.options.showErrors ? prettyError(err, compiler.context).toHtml() : 'ERROR';
213213
})
214214
.then(html => {
215-
// Replace the compilation result with the evaluated html code
215+
// Replace the compilation result with the evaluated html code
216216
compilation.assets[self.childCompilationOutputName] = {
217217
source: () => html,
218218
size: () => html.length
219219
};
220220
})
221-
.then(() => getHtmlWebpackPluginHooks(compilation).htmlWebpackPluginAfterEmit.promise({
221+
.then(() => getHtmlWebpackPluginHooks(compilation).afterEmit.promise({
222222
html: compilation.assets[self.childCompilationOutputName],
223223
outputName: self.childCompilationOutputName,
224224
plugin: self
225225
}).catch(err => {
226226
console.error(err);
227227
return null;
228228
}).then(() => null))
229-
// Let webpack continue with it
229+
// Let webpack continue with it
230230
.then(() => {
231231
callback();
232232
});

‎lib/chunksorter.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
* @type {{[sortmode: string] : (entryPointNames: Array<string>, compilation, htmlWebpackPluginOptions) => Array<string> }}
77
* This file contains different sort methods for the entry chunks names
88
*/
9-
const sortFunctions = {};
10-
module.exports = sortFunctions;
9+
module.exports = {};
1110

1211
/**
1312
* Performs identity mapping (no-sort).
1413
* @param {Array} chunks the chunks to sort
1514
* @return {Array} The sorted chunks
1615
*/
17-
sortFunctions.none = chunks => chunks;
16+
module.exports.none = chunks => chunks;
1817

1918
/**
2019
* Sort manually by the chunks
@@ -23,7 +22,7 @@ sortFunctions.none = chunks => chunks;
2322
* @param htmlWebpackPluginOptions the plugin options
2423
* @return {string[]} The sorted chunks
2524
*/
26-
sortFunctions.manual = (entryPointNames, compilation, htmlWebpackPluginOptions) => {
25+
module.exports.manual = (entryPointNames, compilation, htmlWebpackPluginOptions) => {
2726
const chunks = htmlWebpackPluginOptions.chunks;
2827
if (!Array.isArray(chunks)) {
2928
return entryPointNames;
@@ -38,4 +37,4 @@ sortFunctions.manual = (entryPointNames, compilation, htmlWebpackPluginOptions)
3837
/**
3938
* Defines the default sorter.
4039
*/
41-
sortFunctions.auto = module.exports.none;
40+
module.exports.auto = module.exports.none;

‎lib/hooks.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
1515
// The following typedef holds the API definition for all available hooks
1616
// to allow easier access when using ts-check or typescript inside plugins
1717
/** @typedef {{
18-
htmlWebpackPluginBeforeHtmlGeneration:
18+
19+
beforeHtmlGeneration:
1920
AsyncSeriesWaterfallHook<{
2021
assets: {
2122
publicPath: string,
@@ -25,7 +26,8 @@ const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
2526
outputName: string,
2627
plugin: HtmlWebpackPlugin
2728
}>,
28-
htmlWebpackPluginBeforeHtmlProcessing:
29+
30+
beforeHtmlProcessing:
2931
AsyncSeriesWaterfallHook<{
3032
html: string,
3133
assets: {
@@ -36,7 +38,8 @@ const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
3638
outputName: string,
3739
plugin: HtmlWebpackPlugin,
3840
}>,
39-
htmlWebpackPluginAfterHtmlProcessing:
41+
42+
afterHtmlProcessing:
4043
AsyncSeriesWaterfallHook<{
4144
html: string,
4245
assets: {
@@ -47,19 +50,22 @@ const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
4750
outputName: string,
4851
plugin: HtmlWebpackPlugin,
4952
}>,
50-
htmlWebpackPluginAlterAssetTags:
53+
54+
alterAssetTags:
5155
AsyncSeriesWaterfallHook<{
5256
head: Array<HtmlTagObject>,
5357
body: Array<HtmlTagObject>,
5458
outputName: string,
5559
plugin: HtmlWebpackPlugin
5660
}>,
57-
htmlWebpackPluginAfterEmit:
61+
62+
afterEmit:
5863
AsyncSeriesWaterfallHook<{
5964
html: string,
6065
outputName: string,
6166
plugin: HtmlWebpackPlugin
6267
}>,
68+
6369
}} HtmlWebpackPluginHooks
6470
*/
6571

@@ -81,13 +87,7 @@ function getHtmlWebpackPluginHooks (compilation) {
8187
hooks = createHtmlWebpackPluginHooks();
8288
htmlWebpackPluginHooksMap.set(compilation, hooks);
8389
}
84-
return {
85-
htmlWebpackPluginBeforeHtmlGeneration: hooks.htmlWebpackPluginBeforeHtmlGeneration,
86-
htmlWebpackPluginBeforeHtmlProcessing: hooks.htmlWebpackPluginBeforeHtmlProcessing,
87-
htmlWebpackPluginAlterAssetTags: hooks.htmlWebpackPluginAlterAssetTags,
88-
htmlWebpackPluginAfterHtmlProcessing: hooks.htmlWebpackPluginAfterHtmlProcessing,
89-
htmlWebpackPluginAfterEmit: hooks.htmlWebpackPluginAfterEmit
90-
};
90+
return hooks;
9191
}
9292

9393
/**
@@ -98,11 +98,11 @@ function getHtmlWebpackPluginHooks (compilation) {
9898
*/
9999
function createHtmlWebpackPluginHooks () {
100100
return {
101-
htmlWebpackPluginBeforeHtmlGeneration: new AsyncSeriesWaterfallHook(['pluginArgs']),
102-
htmlWebpackPluginBeforeHtmlProcessing: new AsyncSeriesWaterfallHook(['pluginArgs']),
103-
htmlWebpackPluginAlterAssetTags: new AsyncSeriesWaterfallHook(['pluginArgs']),
104-
htmlWebpackPluginAfterHtmlProcessing: new AsyncSeriesWaterfallHook(['pluginArgs']),
105-
htmlWebpackPluginAfterEmit: new AsyncSeriesWaterfallHook(['pluginArgs'])
101+
beforeHtmlGeneration: new AsyncSeriesWaterfallHook(['pluginArgs']),
102+
beforeHtmlProcessing: new AsyncSeriesWaterfallHook(['pluginArgs']),
103+
alterAssetTags: new AsyncSeriesWaterfallHook(['pluginArgs']),
104+
afterHtmlProcessing: new AsyncSeriesWaterfallHook(['pluginArgs']),
105+
afterEmit: new AsyncSeriesWaterfallHook(['pluginArgs'])
106106
};
107107
}
108108

‎package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"posttest": "tsc",
1616
"commit": "git-cz",
1717
"build-examples": "node examples/build-examples.js",
18-
"test": "jasmine",
18+
"test": "jest --runInBand",
19+
"test-watch": "jest --runInBand --watch",
1920
"release": "standard-version"
2021
},
2122
"semistandard": {
@@ -35,8 +36,7 @@
3536
"html-loader": "^0.4.4",
3637
"jade": "^1.11.0",
3738
"jade-loader": "^0.8.0",
38-
"jasmine": "^2.5.2",
39-
"jasmine-diff-matchers": "^2.0.0",
39+
"jest": "23.3.0",
4040
"rimraf": "^2.5.4",
4141
"semistandard": "8.0.0",
4242
"standard-version": "^4.3.0",
@@ -76,5 +76,9 @@
7676
"commitizen": {
7777
"path": "./node_modules/cz-conventional-changelog"
7878
}
79+
},
80+
"jest": {
81+
"watchPathIgnorePatterns": ["<rootDir>/dist"],
82+
"testEnvironment": "node"
7983
}
8084
}

‎spec/BasicSpec.js ‎spec/basic.spec.js

+136-94
Large diffs are not rendered by default.

‎spec/CachingSpec.js ‎spec/caching.spec.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Integration tests for caching
33
*/
44

5-
/* eslint-env jasmine */
5+
/* eslint-env jest */
66
'use strict';
77

88
var path = require('path');
@@ -12,12 +12,13 @@ var WebpackRecompilationSimulator = require('webpack-recompilation-simulator');
1212
var HtmlWebpackPlugin = require('../index.js');
1313
var webpackMajorVersion = require('webpack/package.json').version.split('.')[0];
1414

15-
var OUTPUT_DIR = path.join(__dirname, '../dist');
15+
var OUTPUT_DIR = path.join(__dirname, '../dist/caching-spec');
1616

17-
jasmine.getEnv().defaultTimeoutInterval = 30000;
17+
jest.setTimeout(30000);
18+
process.on('unhandledRejection', r => console.log(r));
1819

1920
function setUpCompiler (htmlWebpackPlugin) {
20-
spyOn(htmlWebpackPlugin, 'evaluateCompilationResult').and.callThrough();
21+
jest.spyOn(htmlWebpackPlugin, 'evaluateCompilationResult');
2122
var webpackConfig = {
2223
entry: path.join(__dirname, 'fixtures/index.js'),
2324
output: {
@@ -64,7 +65,7 @@ describe('HtmlWebpackPluginCaching', function () {
6465
expect(getCompiledModuleCount(stats.toJson()))
6566
.toBe(0);
6667
// Verify that the html was processed only during the inital build
67-
expect(htmlWebpackPlugin.evaluateCompilationResult.calls.count())
68+
expect(htmlWebpackPlugin.evaluateCompilationResult.mock.calls.length)
6869
.toBe(1);
6970
// Verify that the child compilation was executed twice
7071
expect(htmlWebpackPlugin.childCompilerHash)
@@ -89,7 +90,7 @@ describe('HtmlWebpackPluginCaching', function () {
8990
expect(getCompiledModuleCount(stats.toJson()))
9091
.toBe(1);
9192
// Verify that the html was processed only during the inital build
92-
expect(htmlWebpackPlugin.evaluateCompilationResult.calls.count())
93+
expect(htmlWebpackPlugin.evaluateCompilationResult.mock.calls.length)
9394
.toBe(1);
9495
// Verify that the child compilation was executed only once
9596
expect(htmlWebpackPlugin.childCompilerHash)
@@ -116,7 +117,7 @@ describe('HtmlWebpackPluginCaching', function () {
116117
expect(getCompiledModuleCount(stats.toJson()))
117118
.toBe(1);
118119
// Verify that the html was processed on every run
119-
expect(htmlWebpackPlugin.evaluateCompilationResult.calls.count())
120+
expect(htmlWebpackPlugin.evaluateCompilationResult.mock.calls.length)
120121
.toBe(2);
121122
// Verify that the child compilation was executed only once
122123
expect(htmlWebpackPlugin.childCompilerHash)
@@ -144,7 +145,7 @@ describe('HtmlWebpackPluginCaching', function () {
144145
expect(getCompiledModuleCount(stats.toJson()))
145146
.toBe(1);
146147
// Verify that the html was processed twice
147-
expect(htmlWebpackPlugin.evaluateCompilationResult.calls.count())
148+
expect(htmlWebpackPlugin.evaluateCompilationResult.mock.calls.length)
148149
.toBe(2);
149150
// Verify that the child compilation was executed twice
150151
expect(htmlWebpackPlugin.childCompilerHash)

‎spec/ExampleSpec.js ‎spec/example.spec.js

+11-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* and matches them against their dist folder
44
*/
55

6-
/* eslint-env jasmine */
6+
/* eslint-env jest */
77
'use strict';
88

99
var path = require('path');
@@ -12,19 +12,19 @@ var rimraf = require('rimraf');
1212
var fs = require('fs');
1313
var webpackMajorVersion = require('webpack/package.json').version.split('.')[0];
1414

15-
var OUTPUT_DIR = path.join(__dirname, '../dist');
15+
var OUTPUT_DIR = path.resolve(__dirname, '../dist');
1616

17-
jasmine.getEnv().defaultTimeoutInterval = 30000;
17+
jest.setTimeout(30000);
1818

1919
function runExample (exampleName, done) {
2020
var examplePath = path.resolve(__dirname, '..', 'examples', exampleName);
2121
var exampleOutput = path.join(OUTPUT_DIR, exampleName);
22-
var fixturePath = path.join(examplePath, 'dist', 'webpack-' + webpackMajorVersion);
22+
var fixturePath = path.resolve(examplePath, 'dist', 'webpack-' + webpackMajorVersion);
2323
// Clear old results
2424
rimraf(exampleOutput, function () {
2525
var options = require(path.join(examplePath, 'webpack.config.js'));
2626
options.context = examplePath;
27-
options.output.path = exampleOutput;
27+
options.output.path = exampleOutput + path.sep;
2828
if (Number(webpackMajorVersion) >= 4) {
2929
options.plugins.unshift(new webpack.LoaderOptionsPlugin({
3030
options: {
@@ -39,7 +39,10 @@ function runExample (exampleName, done) {
3939
options.optimization = { minimizer: [] };
4040
}
4141

42-
webpack(options, function (err) {
42+
webpack(options, function (err, stats) {
43+
expect(err).toBeFalsy();
44+
expect(stats.compilation.errors).toEqual([]);
45+
4346
var dircompare = require('dir-compare');
4447
var res = dircompare.compareSync(fixturePath, exampleOutput, {compareSize: true});
4548

@@ -48,22 +51,16 @@ function runExample (exampleName, done) {
4851
}).forEach(function (diff) {
4952
var file1Contents = fs.readFileSync(path.join(diff.path1, diff.name1)).toString();
5053
var file2Contents = fs.readFileSync(path.join(diff.path2, diff.name2)).toString();
51-
expect(file1Contents).diffPatch(file2Contents);
52-
expect(file1Contents).toBe(file2Contents);
54+
expect(file1Contents).toEqual(file2Contents);
5355
});
5456

55-
expect(err).toBeFalsy();
5657
expect(res.same).toBe(true);
57-
done();
58+
rimraf(exampleOutput, done);
5859
});
5960
});
6061
}
6162

6263
describe('HtmlWebpackPlugin Examples', function () {
63-
beforeEach(function () {
64-
jasmine.addMatchers(require('jasmine-diff-matchers').diffPatch);
65-
});
66-
6764
it('appcache example', function (done) {
6865
runExample('appcache', done);
6966
});

0 commit comments

Comments
 (0)
Please sign in to comment.