|
1 | 1 | const fs = require('fs');
|
2 |
| -const {spawn} = require('child_process'); |
3 |
| - |
4 |
| -const del = require('del'); |
5 |
| - |
6 | 2 | const ROOT = `${__dirname}/dev-server`;
|
7 | 3 | const WEBPACK_CONFIG_PATH = `${ROOT}/webpack.config.js`;
|
8 | 4 | const webpackConfig = require(WEBPACK_CONFIG_PATH);
|
| 5 | +const DevServer = require('webpack-dev-server'); |
| 6 | +const webpack = require('webpack'); |
9 | 7 |
|
10 | 8 | describe('Webpack Dev Server', function () {
|
11 |
| - beforeAll(deleteOutputDirectory); |
12 |
| - afterEach(deleteOutputDirectory); |
13 |
| - |
14 |
| - const timeout = 15000; |
15 |
| - jest.setTimeout(timeout); |
16 |
| - |
17 |
| - it('should save report file to the output directory', function (done) { |
18 |
| - const startedAt = Date.now(); |
19 |
| - |
20 |
| - const devServer = spawn(`${__dirname}/../node_modules/.bin/webpack-dev-server`, ['--config', WEBPACK_CONFIG_PATH], { |
21 |
| - cwd: ROOT |
| 9 | + it('should save report file to memory file system when writeToDisk is empty', async function () { |
| 10 | + expect.assertions(2); |
| 11 | + const compiler = webpack(webpackConfig); |
| 12 | + const devServer = await new Promise((resolve) => { |
| 13 | + const devServerOptions = {host: '127.0.0.1', port: 8080}; |
| 14 | + const devServer = new DevServer(compiler, devServerOptions); |
| 15 | + devServer.listen(devServerOptions.port, devServerOptions.host, () => { |
| 16 | + resolve(devServer); |
| 17 | + }); |
| 18 | + }); |
| 19 | + await new Promise((resolve) => { |
| 20 | + compiler.hooks.afterDone.tap('webpack-bundle-analyzer', resolve); |
| 21 | + }); |
| 22 | + const path = `${webpackConfig.output.path}/report.html`; |
| 23 | + expect(compiler.outputFileSystem.existsSync(path)).toBeTruthy(); |
| 24 | + expect(fs.existsSync(path)).toBeFalsy(); |
| 25 | + compiler.outputFileSystem.unlinkSync(path); |
| 26 | + await new Promise((resolve) => { |
| 27 | + devServer.close(() => { |
| 28 | + resolve(); |
| 29 | + }); |
22 | 30 | });
|
23 | 31 |
|
24 |
| - const reportCheckIntervalId = setInterval(() => { |
25 |
| - if (fs.existsSync(`${webpackConfig.output.path}/report.html`)) { |
26 |
| - finish(); |
27 |
| - } else if (Date.now() - startedAt > timeout - 1000) { |
28 |
| - finish(`report file wasn't found in "${webpackConfig.output.path}" directory`); |
29 |
| - } |
30 |
| - }, 300); |
| 32 | + }); |
31 | 33 |
|
32 |
| - function finish(errorMessage) { |
33 |
| - clearInterval(reportCheckIntervalId); |
34 |
| - devServer.kill(); |
35 |
| - done(errorMessage ? new Error(errorMessage) : null); |
36 |
| - } |
| 34 | + it('should save report file to the output directory when writeToDisk is true', async function () { |
| 35 | + expect.assertions(2); |
| 36 | + const compiler = webpack(webpackConfig); |
| 37 | + const devServer = await new Promise((resolve) => { |
| 38 | + const devServerOptions = {host: '127.0.0.1', port: 8080, writeToDisk: true}; |
| 39 | + const devServer = new DevServer(compiler, devServerOptions); |
| 40 | + devServer.listen(devServerOptions.port, devServerOptions.host, () => { |
| 41 | + resolve(devServer); |
| 42 | + }); |
| 43 | + }); |
| 44 | + await new Promise((resolve) => { |
| 45 | + compiler.hooks.afterDone.tap('webpack-bundle-analyzer', resolve); |
| 46 | + }); |
| 47 | + const path = `${webpackConfig.output.path}/report.html`; |
| 48 | + expect(compiler.outputFileSystem.existsSync(path)).toBeTruthy(); |
| 49 | + expect(fs.existsSync(path)).toBeTruthy(); |
| 50 | + compiler.outputFileSystem.unlinkSync(path); |
| 51 | + return await new Promise((resolve) => { |
| 52 | + devServer.close(() => { |
| 53 | + resolve(); |
| 54 | + }); |
| 55 | + }); |
37 | 56 | });
|
38 | 57 | });
|
39 | 58 |
|
40 |
| -function deleteOutputDirectory() { |
41 |
| - del.sync(webpackConfig.output.path); |
42 |
| -} |
|
0 commit comments