forked from Refinitiv/refinitiv-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkarma.config.js
202 lines (188 loc) · 5.2 KB
/
karma.config.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
202
#!/usr/bin/env node
const path = require('path');
const { ROOT, PACKAGES } = require('./scripts/helpers');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const { injectLitPolyfill } = require('./scripts/karma/plugins/inject-lit-polyfill')
const browsersConfig = require('./browsers.config');
const argv = yargs(hideBin(process.argv))
.option('include-snapshots', {
type: 'boolean',
default: true,
description: 'Include snapshot testing'
})
.option('include-coverage', {
type: 'boolean',
default: true,
description: 'Include coverage testing'
})
.option('snapshots', {
type: 'boolean',
default: false,
description: 'Update and prune snapshots'
})
.option('package', {
type: 'string',
alias: 'p',
description: 'Package name'
})
.option('watch', {
type: 'boolean',
default: false,
description: 'Run test and watch file change'
})
.option('browsers', {
type: 'array',
alias: 'b',
default: browsersConfig.defaultBrowsers,
choices: browsersConfig.availableBrowsers,
description: 'Specific browser(s) to run units test'
})
.argv
const packageName = argv.package || path.basename(process.cwd()); // if no package provided, try to guess
const packagePath = path.join(ROOT, PACKAGES, packageName);
// Files to include for testing
const files = [{
pattern: path.join(packagePath, '__test__/**/*.test.js'),
type: 'module'
}];
// Parsers
const frameworks = [
'esm',
'mocha',
'source-map-support',
];
// Preprocessors
const preprocessors = {};
// Plugins
const plugins = [
// resolve plugins relative to this config so that they don't always need to exist
// at the top level
require.resolve('./scripts/karma/karma-esm'),
require.resolve('karma-mocha'),
require.resolve('karma-mocha-reporter'),
require.resolve('karma-source-map-support'),
require.resolve('karma-chrome-launcher'),
require.resolve('karma-firefox-launcher'),
require.resolve('karma-ie-launcher'),
// fallback: resolve any karma- plugins
'karma-*'
];
// Reporters
const reporters = ['mocha'];
const baseConfig = {
autoWatch: argv.watch,
singleRun: !argv.watch,
basePath: ROOT, // must be in the root in order for node_modules to be resolved correctly
concurrency: 1, // Set the value to `1`, When Karma has a problem to connect a test browser on Windows.
// IE 11 must add extra time to loading all scripts for testing concurrently.
browserNoActivityTimeout: 60000 * 2,
browserDisconnectTimeout: 60000 * 2,
files,
esm: {
coverage: argv.includeCoverage,
coverageExclude: ['**/__test__/**', '**/__snapshots__/**', '**/__demo__/**'],
nodeResolve: true,
compatibility: 'auto',
preserveSymlinks: true,
// prevent compiling es5 libs
babelExclude: [
'**/node_modules/mocha/**/*',
'**/node_modules/chai/**/*',
'**/node_modules/sinon-chai/**/*',
'**/node_modules/chai-dom/**/*',
'**/node_modules/core-js-bundle/**/*'
],
// sinon is not completely es5...
babelModernExclude: ['**/node_modules/sinon/**/*'],
// prevent compiling non-module libs
babelModuleExclude: [
'**/node_modules/mocha/**/*',
'**/node_modules/core-js-bundle/**/*'
],
// exclude files served via Karma internally
karmaExclude: [
'**/__snapshots__/**'
],
plugins: [
injectLitPolyfill()
]
},
plugins,
frameworks,
preprocessors,
reporters,
mochaReporter: {
showDiff: true
},
restartOnFileChange: false,
client: {
mocha: {
reporter: 'html'
}
},
colors: true
};
// Do not run headless browsers in watch mode, it significantly slow down debugging
if (!argv.watch) {
baseConfig.browsers = argv.browsers;
baseConfig.customLaunchers = {
firefox: {
base: 'Firefox',
flags: ['-headless']
},
chrome: {
base: 'ChromeHeadless',
flags: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-translate',
'--disable-extensions'
]
},
ie: {
base: 'IE',
flags: ['-extoff']
}
};
}
// Snapshots needs specific configuration
if (argv.includeSnapshots) {
files.push({
pattern: path.join(packagePath, '__snapshots__/**/*.md'),
type: 'js'
});
frameworks.unshift('snapshot', 'mocha-snapshot');
preprocessors['**/__snapshots__/**/*.md'] = ['snapshot'];
baseConfig.snapshot = {
update: argv.snapshots,
prune: argv.snapshots,
pathResolver: (basePath, suiteName) => path.join(basePath, PACKAGES, packageName, `__snapshots__/${suiteName}.md`)
};
plugins.push(
require.resolve('karma-snapshot'),
require.resolve('karma-mocha-snapshot')
);
}
// Coverage configurations
if (argv.includeCoverage) {
baseConfig.coverageIstanbulReporter = {
reports: ['html', 'lcovonly', 'text-summary'],
dir: 'coverage',
combineBrowserReports: true,
skipFilesWithNoCoverage: false,
thresholds: {
global: {
statements: 80,
branches: 80,
functions: 80,
lines: 80
}
}
};
reporters.push('coverage-istanbul');
plugins.push(require.resolve('karma-coverage-istanbul-reporter'));
}
module.exports = async function (config) {
config.set(baseConfig);
};