-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathkarma.conf.js
More file actions
145 lines (136 loc) · 4.97 KB
/
Copy pathkarma.conf.js
File metadata and controls
145 lines (136 loc) · 4.97 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
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
/**
* Karma configuration for Nuxeo Web UI unit tests (@open-wc/karma-esm + Mocha).
*
* Important design choices:
*
* 1) Single test entry (`test/load-all-tests.js` only in `files`)
* Karma+ESM can otherwise load many test modules in parallel; Mocha may call `__karma__.loaded()`
* before every suite is registered, so some tests never run. The generated barrel imports
* `setup.js` then every `*.test.js` in one static graph. Do not add separate test file globs here.
*
* 2) Coverage mode (`karma start --coverage`)
* - Serves the element source globs defined in coverageSourceFiles as modules (not executed as scripts)
* so dynamic imports from `test/setup.js` can resolve.
* - `esm.compatibility: 'always'` runs sources through Babel so Istanbul instruments them
* (native ESM in Chrome can skip instrumentation otherwise).
* - `skipFilesWithNoCoverage: false` keeps files with zero hits visible in reports.
* - Bulk import of all element modules happens in `test/setup.js` `suiteTeardown`, not here.
*
* Related: `scripts/generate-test-load-all.js`, `scripts/generate-coverage-imports.js`, `test/setup.js`.
*/
const path = require('path');
const coverage = process.argv.find((arg) => arg.includes('coverage'));
// When instrumenting, Karma must be able to fetch any element URL that setup.js imports dynamically.
const coverageSourceFiles = coverage
? [
{
pattern: 'elements/**/*.js',
type: 'module',
included: false,
watched: false,
},
{
pattern: 'addons/**/elements/**/*.js',
type: 'module',
included: false,
watched: false,
},
]
: [];
const reporters = coverage ? ['mocha', 'coverage-istanbul'] : ['mocha'];
let customLaunchers = {
ChromeHeadlessNoSandbox: {
base: 'ChromeHeadless',
flags: ['--disable-gpu', '--no-sandbox'],
},
};
module.exports = (config) => {
// Single module entry (see test/load-all-tests.js) loads every suite in one graph before
// __karma__.loaded(). Do not add separate test globs here — that reintroduces parallel races.
config.set({
hostname: '127.0.0.1',
basePath: '',
singleRun: true,
browsers: config.browsers && config.browsers.length > 0 ? config.browsers : Object.keys(customLaunchers),
browserDisconnectTimeout: 10 * 1000,
browserDisconnectTolerance: 1,
browserNoActivityTimeout: 5 * 60 * 1000,
customLaunchers,
middleware: ['static'],
static: {
path: path.join(process.cwd(), ''),
},
files: [
...coverageSourceFiles,
{
pattern: 'test/load-all-tests.js',
type: 'module',
},
],
plugins: [
// load plugin
require.resolve('@open-wc/karma-esm'),
// fallback: resolve any karma- plugins
'karma-*',
],
frameworks: ['esm', 'mocha', 'source-map-support'],
esm: {
// 'always' runs app sources through Babel so istanbul can record hits. With 'none',
// modern browsers skip transforms and some modules (e.g. elements/performance.js) stay
// effectively un-instrumented despite tests executing their logic.
compatibility: coverage ? 'always' : 'none',
// polyfills-loader hashes with crypto.createHash('md4'), which throws on Node 17+
// (OpenSSL 3) unless NODE_OPTIONS=--openssl-legacy-provider. Unit tests use modern
// Chrome only, so injecting the polyfills loader is unnecessary here.
polyfillsLoader: false,
coverage,
// if you are using 'bare module imports' you will need this option
nodeResolve: {
// Dedupe all common packages to prevent duplicate registrations
// when using symlinked packages (nuxeo-elements)
dedupe: (importee) =>
importee.startsWith('@polymer/') || importee.startsWith('@nuxeo/') || importee.startsWith('@webcomponents/'),
},
// needed for npm link or lerna support
preserveSymlinks: true,
},
reporters,
port: 9876,
colors: true,
browserConsoleLogOptions: {
// Set KARMA_VERBOSE=1 to surface karma-esm "Error loading test file" in the terminal.
level: process.env.KARMA_VERBOSE === '1' ? 'log' : 'error',
},
logLevel: config.LOG_WARN,
/** Some errors come in JSON format with a message property. */
formatError(error) {
try {
if (typeof error !== 'string') {
return error;
}
const parsed = JSON.parse(error);
if (typeof parsed !== 'object' || !parsed.message) {
return error;
}
return parsed.message;
} catch (_) {
return error;
}
},
coverageIstanbulReporter: {
reports: ['html', 'lcovonly', 'text-summary'],
dir: path.join(__dirname, 'coverage'),
combineBrowserReports: true,
skipFilesWithNoCoverage: false,
},
client: {
useCoverage: Boolean(coverage),
mocha: {
reporter: 'html',
ui: 'tdd',
timeout: 3000,
...(config.grep ? { grep: config.grep } : {}),
},
},
});
};