Skip to content

Commit b6bfd12

Browse files
Add test:fast for faster unit test runs (#3179)
* refactor: optimize karma configuration for improved performance * feat: add fast karma configuration and update package.json for debugging * refactor: streamline karma plugin declaration and update test scripts for clarity * refactor: update test commands to use fast karma configuration
1 parent b1994b6 commit b6bfd12

File tree

2 files changed

+142
-2
lines changed

2 files changed

+142
-2
lines changed

karma.fast.conf.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
const argv = require('minimist')(process.argv.slice(2));
2+
const components = argv.components !== true && argv.components;
3+
const runCoverage = typeof argv.coverage !== 'undefined';
4+
const runFirefox = typeof argv.firefox !== 'undefined';
5+
const runChrome = typeof argv.chrome !== 'undefined';
6+
7+
const rootPath = __dirname;
8+
9+
module.exports = function (config) {
10+
const plugins = ['karma-webpack', 'karma-jasmine', 'karma-sourcemap-loader'];
11+
const launcher = [];
12+
13+
if (runCoverage) {
14+
plugins.push('karma-coverage-istanbul-reporter');
15+
}
16+
17+
if (runChrome) {
18+
plugins.push('karma-chrome-launcher');
19+
launcher.push('Chrome');
20+
}
21+
22+
if (runFirefox) {
23+
plugins.push('karma-firefox-launcher');
24+
launcher.push('Firefox');
25+
}
26+
27+
const tsConfig = {
28+
compilerOptions: {
29+
rootDir: rootPath,
30+
declaration: false,
31+
strict: false,
32+
downlevelIteration: true,
33+
transpileOnly: true, // Faster compilation - skip type checking
34+
paths: {
35+
'*': ['*', rootPath + '/packages/*'],
36+
},
37+
},
38+
transpileOnly: true, // Enable faster transpilation
39+
};
40+
41+
const rules = runCoverage
42+
? [
43+
{
44+
test: /lib(\\|\/).*\.ts$/,
45+
use: [
46+
{ loader: '@jsdevtools/coverage-istanbul-loader' },
47+
{
48+
loader: 'ts-loader',
49+
options: tsConfig,
50+
},
51+
],
52+
},
53+
{
54+
test: /test(\\|\/).*\.ts$/,
55+
loader: 'ts-loader',
56+
options: tsConfig,
57+
},
58+
]
59+
: [
60+
{
61+
test: /\.ts$/,
62+
loader: 'ts-loader',
63+
options: tsConfig,
64+
},
65+
];
66+
67+
const settings = {
68+
basePath: '.',
69+
plugins,
70+
client: {
71+
components: components,
72+
clearContext: false,
73+
captureConsole: true,
74+
},
75+
browsers: launcher,
76+
files: ['tools/karma.test.all.js'],
77+
frameworks: ['jasmine'],
78+
preprocessors: {
79+
'tools/karma.test.all.js': ['webpack', 'sourcemap'],
80+
},
81+
port: 9876,
82+
colors: true,
83+
logLevel: config.LOG_INFO,
84+
autoWatch: true,
85+
autoWatchBatchDelay: 300, // Batch file changes for better performance
86+
87+
// to avoid DISCONNECTED messages
88+
browserDisconnectTimeout: 10000, // default 2000
89+
browserDisconnectTolerance: 1, // default 0
90+
browserNoActivityTimeout: 60000, //default 10000
91+
browserConsoleLogOptions: {
92+
level: 'log',
93+
format: '%b %T: %m',
94+
terminal: true,
95+
},
96+
97+
singleRun: true,
98+
captureTimeout: 60000,
99+
100+
webpack: {
101+
mode: 'development',
102+
devtool: 'eval-source-map', // Faster than inline-source-map
103+
module: {
104+
rules,
105+
},
106+
resolve: {
107+
extensions: ['.ts', '.tsx', '.js'],
108+
modules: ['./packages', './node_modules'],
109+
},
110+
// Workaround karma-webpack issue https://github.com/ryanclark/karma-webpack/issues/493
111+
// Got this solution from https://github.com/ryanclark/karma-webpack/issues/493#issuecomment-780411348
112+
optimization: {
113+
splitChunks: false,
114+
removeAvailableModules: false, // Performance optimization
115+
removeEmptyChunks: false, // Performance optimization
116+
},
117+
// Add filesystem caching for better performance
118+
cache: {
119+
type: 'filesystem',
120+
cacheDirectory: require('path').join(rootPath, 'node_modules/.cache/webpack'),
121+
},
122+
stats: 'errors-warnings', // Reduce console output
123+
},
124+
125+
// Concurrency level
126+
// how many browser should be started simultaneous
127+
concurrency: Infinity,
128+
};
129+
130+
if (runCoverage) {
131+
settings.reporters = ['coverage-istanbul'];
132+
settings.coverageIstanbulReporter = {
133+
reports: ['html', 'lcovonly', 'text-summary'],
134+
dir: './dist/deploy/coverage',
135+
};
136+
}
137+
138+
config.set(settings);
139+
};

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
"test": "node tools/build.js normalize & karma start --chrome",
2828
"test:chrome": "node tools/build.js normalize & karma start --chrome",
2929
"test:firefox": "node tools/build.js normalize & karma start --firefox",
30-
"test:debug": "node tools/build.js normalize & karma start --no-single-run --chrome",
31-
"test:debug-firefox": "node tools/build.js normalize & karma start --no-single-run --firefox",
30+
"test:debug": "node tools/build.js normalize & karma start karma.fast.conf.js --no-single-run --chrome",
31+
"test:fast": "node tools/build.js normalize & karma start karma.fast.conf.js --chrome",
32+
"test:debug-firefox": "node tools/build.js normalize & karma start karma.fast.conf.js --no-single-run --firefox",
3233
"test:coverage": "node tools/build.js normalize & karma start --coverage --firefox --chrome",
3334
"publish": "node tools/build.js clean normalize buildcommonjs buildamd buildmjs dts pack packprod builddemo builddoc publish"
3435
},

0 commit comments

Comments
 (0)