-
Notifications
You must be signed in to change notification settings - Fork 6
/
start.js
127 lines (101 loc) · 2.89 KB
/
start.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* @license Copyright (c) 2020-2022, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
'use strict';
const sinon = require( 'sinon' );
const expect = require( 'chai' ).expect;
const mockery = require( 'mockery' );
describe( 'lib/tasks/start', () => {
let startTask, stubs;
beforeEach( () => {
mockery.enable( {
useCleanCache: true,
warnOnReplace: false,
warnOnUnregistered: false
} );
stubs = {
webpackDevServerArguments: null,
webpack: sinon.stub(),
webpackConfig: sinon.stub(),
server: {
start: sinon.stub()
}
};
mockery.registerMock( 'webpack', stubs.webpack );
mockery.registerMock( 'webpack-dev-server', function( ...args ) {
stubs.webpackDevServerArguments = args;
return stubs.server;
} );
mockery.registerMock( '../utils/get-webpack-config-server', stubs.webpackConfig );
startTask = require( '../../lib/tasks/start' );
} );
afterEach( () => {
sinon.restore();
mockery.disable();
} );
it( 'should be a function', () => {
expect( startTask ).to.be.a( 'function' );
} );
it( 'should run webpack and enable server with the live-reloading mechanism', () => {
const taskOptions = {
cwd: '/cwd',
open: true
};
// An example object returned by webpack marked as "compiler".
const compiler = {
type: 'compiler'
};
const webpackConfig = {
mode: 'development',
entry: 'file.js',
devServer: {
port: 9000
}
};
// Mock reading the configuration.
stubs.webpackConfig.returns( webpackConfig );
// Webpack returns a compiler instance if callback is not specified.
stubs.webpack.returns( compiler );
// Execute the task.
startTask( taskOptions );
// Verify arguments passed to webpack-dev-server.
expect( stubs.webpackDevServerArguments ).to.be.an( 'array' );
expect( stubs.webpackDevServerArguments[ 0 ] ).to.deep.equal( {
port: 9000,
open: true
} );
expect( stubs.webpackDevServerArguments[ 1 ] ).to.equal( compiler );
// Verify whether the server was started.
expect( stubs.server.start.calledOnce ).to.equal( true );
} );
it( 'should not open the browser if the open option is set to false', () => {
const taskOptions = {
cwd: '/cwd',
open: false
};
// An example object returned by webpack marked as "compiler".
const compiler = {
type: 'compiler'
};
const webpackConfig = {
mode: 'development',
entry: 'file.js',
devServer: {
port: 9000
}
};
// Mock reading the configuration.
stubs.webpackConfig.returns( webpackConfig );
// Webpack returns a compiler instance if callback is not specified.
stubs.webpack.returns( compiler );
// Execute the task.
startTask( taskOptions );
// Verify arguments passed to webpack-dev-server.
expect( stubs.webpackDevServerArguments ).to.be.an( 'array' );
expect( stubs.webpackDevServerArguments[ 0 ] ).to.deep.equal( {
port: 9000,
open: false
} );
} );
} );