-
Notifications
You must be signed in to change notification settings - Fork 6
/
getkarmaconfig.js
201 lines (160 loc) · 3.96 KB
/
getkarmaconfig.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md.
*/
'use strict';
/* eslint-env node */
const path = require( 'path' );
const options = parseArguments( process.argv.slice( 2 ) );
module.exports = function getKarmaConfig() {
const basePath = process.cwd();
const coverageDir = path.join( basePath, 'coverage' );
const webpackConfig = require( '../../webpack.config.js' )( {}, {
mode: 'development'
} );
delete webpackConfig.entry;
delete webpackConfig.output;
const karmaConfig = {
basePath,
frameworks: [ 'mocha', 'sinon', 'webpack' ],
files: [
'tests/index.js'
],
preprocessors: {
'tests/index.js': [ 'webpack' ]
},
webpack: webpackConfig,
webpackMiddleware: {
noInfo: true,
stats: 'minimal'
},
reporters: [ options.reporter ],
port: 9876,
colors: true,
logLevel: 'INFO',
browsers: getBrowsers( options.browsers ),
customLaunchers: {
CHROME_TRAVIS_CI: {
base: 'Chrome',
flags: [ '--no-sandbox', '--disable-background-timer-throttling' ]
},
CHROME_LOCAL: {
base: 'Chrome',
flags: [ '--disable-background-timer-throttling' ]
}
},
singleRun: true,
concurrency: Infinity,
browserNoActivityTimeout: 0,
mochaReporter: {
showDiff: true
}
};
if ( options.watch ) {
karmaConfig.autoWatch = true;
karmaConfig.singleRun = false;
}
if ( options.coverage ) {
karmaConfig.reporters.push( 'coverage' );
karmaConfig.coverageReporter = {
reporters: [
// Prints a table after tests result.
{
type: 'text-summary'
},
// Generates HTML tables with the results.
{
type: 'html',
dir: coverageDir,
subdir: '.'
},
// Generates "lcov.info" file. It's used by external code coverage services.
{
type: 'lcovonly',
dir: coverageDir,
subdir: '.'
}
]
};
const jsRule = webpackConfig.module.rules.find( ( { test } ) => test.test( '.js' ) );
jsRule.options.plugins.push( [
'babel-plugin-istanbul', {
include: [
'src'
],
exclude: [
'node_modules'
]
}
] );
}
if ( options.sourceMap ) {
karmaConfig.preprocessors[ 'tests/**/*.js' ].push( 'sourcemap' );
karmaConfig.preprocessors[ 'tests/**/*.jsx' ].push( 'sourcemap' );
webpackConfig.devtool = 'inline-source-map';
}
return karmaConfig;
};
/**
* Returns the value of Karma's browser option.
*
* @param {Array.<String>} browsers
* @returns {Array.<String>|null}
*/
function getBrowsers( browsers ) {
if ( !browsers ) {
return null;
}
return browsers.map( browser => {
if ( browser !== 'Chrome' ) {
return browser;
}
return process.env.TRAVIS ? 'CHROME_TRAVIS_CI' : 'CHROME_LOCAL';
} );
}
/**
* @param {Array.<String>} args CLI arguments and options.
* @returns {Object} options
* @returns {Array.<String>} options.browsers Browsers that will be used to run tests.
* @returns {String} options.reporter A reporter that will presents tests results.
* @returns {Boolean} options.watch Whether to watch the files.
* @returns {Boolean} options.coverage Whether to generate code coverage.
* @returns {Boolean} options.sourceMap Whether to add source maps.
*/
function parseArguments( args ) {
const minimist = require( 'minimist' );
const config = {
string: [
'browsers',
'reporter'
],
boolean: [
'watch',
'coverage',
'source-map'
],
alias: {
b: 'browsers',
c: 'coverage',
r: 'reporter',
s: 'source-map',
w: 'watch'
},
default: {
browsers: 'Chrome',
reporter: 'mocha',
watch: false,
coverage: false,
'source-map': false
}
};
const options = minimist( args, config );
options.sourceMap = options[ 'source-map' ];
options.browsers = options.browsers.split( ',' );
// Delete all aliases because we don't want to use them in the code.
// They are useful when calling command but useless after that.
for ( const alias of Object.keys( config.alias ) ) {
delete options[ alias ];
}
return options;
}