Repository navigation
Expand file tree
/
Copy pathrunner.test.js
More file actions
96 lines (78 loc) · 2.58 KB
/
Copy pathrunner.test.js
File metadata and controls
96 lines (78 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* External dependencies
*/
const webpack = require( 'webpack' );
/**
* Internal dependencies
*/
const { runWebpackConfigs } = require( './runner' );
jest.mock( 'webpack', () => {
const mockWebpack = jest.fn();
return mockWebpack;
} );
describe( 'runWebpackConfigs', () => {
beforeEach( () => {
jest.clearAllMocks();
} );
it( 'should return an empty array for empty input', async () => {
await expect( runWebpackConfigs( [] ) ).resolves.toEqual( [] );
expect( webpack ).not.toHaveBeenCalled();
} );
it( 'should throw TypeError when configs is not an array', async () => {
await expect( runWebpackConfigs( null ) ).rejects.toThrow( TypeError );
await expect( runWebpackConfigs( {} ) ).rejects.toThrow( TypeError );
} );
it( 'should throw TypeError for invalid concurrency', async () => {
await expect(
runWebpackConfigs( [ { entry: './a.js' } ], { concurrency: 0 } )
).rejects.toThrow( TypeError );
await expect(
runWebpackConfigs( [ { entry: './a.js' } ], {
concurrency: Number.NaN,
} )
).rejects.toThrow( TypeError );
} );
it( 'should run all configs and return stats in order', async () => {
const statsA = { hasErrors: () => false };
const statsB = { hasErrors: () => false };
let callIndex = 0;
webpack.mockImplementation( ( config, callback ) => {
const stats = callIndex === 0 ? statsA : statsB;
callIndex += 1;
callback( null, stats );
} );
const configs = [ { entry: './a.js' }, { entry: './b.js' } ];
const results = await runWebpackConfigs( configs );
expect( webpack ).toHaveBeenCalledTimes( 2 );
expect( results ).toEqual( [ statsA, statsB ] );
} );
it( 'should reject when a compilation reports errors', async () => {
webpack.mockImplementation( ( config, callback ) => {
callback( null, {
hasErrors: () => true,
toString: () => 'compilation failed',
} );
} );
await expect(
runWebpackConfigs( [ { entry: './a.js' } ] )
).rejects.toThrow( 'compilation failed' );
} );
it( 'should limit parallel compilations when concurrency is set', async () => {
let inFlight = 0;
let maxInFlight = 0;
webpack.mockImplementation( ( config, callback ) => {
inFlight += 1;
maxInFlight = Math.max( maxInFlight, inFlight );
setTimeout( () => {
inFlight -= 1;
callback( null, { hasErrors: () => false } );
}, 10 );
} );
const configs = Array.from( { length: 4 }, ( _, index ) => ( {
entry: `./file-${ index }.js`,
} ) );
await runWebpackConfigs( configs, { concurrency: 2 } );
expect( maxInFlight ).toBeLessThanOrEqual( 2 );
expect( webpack ).toHaveBeenCalledTimes( 4 );
} );
} );