Skip to content

Commit 5b062e5

Browse files
committed
Add support to run tests with vitest
1 parent 14cc0f2 commit 5b062e5

File tree

3 files changed

+755
-30
lines changed

3 files changed

+755
-30
lines changed

lib/tests.js

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import { buildJS, watchJS } from './rollup.js';
1414
* @param {string} options.bootstrapFile - Entry point for the test bundle that initializes the environment
1515
* @param {string} options.rollupConfig - Rollup config that generates the test bundle using
1616
* `${outputDir}/test-inputs.js` as an entry point
17-
* @param {string} options.karmaConfig - Karma config file
17+
* @param {string} [options.karmaConfig] - Karma config file. Will use karma to run tests
18+
* @param {string} [options.vitestConfig] - Vitest config file. Will use vitest to run tests
1819
* @param {string} options.outputDir - Directory in which to generate test bundle. Defaults to
1920
* `build/scripts`
2021
* @param {string} options.testsPattern - Minimatch pattern that specifies which test files to
@@ -26,6 +27,7 @@ export async function runTests({
2627
rollupConfig,
2728
outputDir = 'build/scripts',
2829
karmaConfig,
30+
vitestConfig,
2931
testsPattern,
3032
}) {
3133
// Parse command-line options for test execution.
@@ -64,30 +66,45 @@ export async function runTests({
6466
await watchJS(rollupConfig);
6567
}
6668

67-
// Run the tests.
68-
log('Starting Karma...');
69-
const { default: karma } = await import('karma');
70-
const parsedConfig = await karma.config.parseConfig(
71-
path.resolve(karmaConfig),
72-
{ singleRun },
73-
);
69+
// Run the tests with vitest. Karma takes precedence if both are defined
70+
if (vitestConfig && !karmaConfig) {
71+
log('Starting vitest...');
72+
const [{ startVitest }, vitestOptions] = await Promise.all([
73+
import('vitest/node'),
74+
import(path.resolve(vitestConfig)),
75+
]);
76+
await startVitest('test', [], {
77+
watch: live,
78+
...vitestOptions,
79+
});
80+
}
7481

75-
return new Promise((resolve, reject) => {
76-
new karma.Server(parsedConfig, exitCode => {
77-
if (exitCode === 0) {
78-
resolve();
79-
} else {
80-
reject(new Error(`Karma run failed with status ${exitCode}`));
81-
}
82-
}).start();
82+
// Run the tests with karma.
83+
if (karmaConfig) {
84+
log('Starting Karma...');
85+
const { default: karma } = await import('karma');
86+
const parsedConfig = await karma.config.parseConfig(
87+
path.resolve(karmaConfig),
88+
{ singleRun },
89+
);
8390

84-
process.on('SIGINT', () => {
85-
// Give Karma a chance to handle SIGINT and cleanup, but forcibly
86-
// exit if it takes too long.
87-
setTimeout(() => {
88-
resolve();
89-
process.exit(1);
90-
}, 5000);
91+
return new Promise((resolve, reject) => {
92+
new karma.Server(parsedConfig, exitCode => {
93+
if (exitCode === 0) {
94+
resolve();
95+
} else {
96+
reject(new Error(`Karma run failed with status ${exitCode}`));
97+
}
98+
}).start();
99+
100+
process.on('SIGINT', () => {
101+
// Give Karma a chance to handle SIGINT and cleanup, but forcibly
102+
// exit if it takes too long.
103+
setTimeout(() => {
104+
resolve();
105+
process.exit(1);
106+
}, 5000);
107+
});
91108
});
92-
});
109+
}
93110
}

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"rollup": "^4.0.2",
2727
"sass": "^1.43.2",
2828
"tailwindcss": "^3.0.11",
29-
"typescript": "^5.0.2"
29+
"typescript": "^5.0.2",
30+
"vitest": "^3.1.1"
3031
},
3132
"dependencies": {
3233
"commander": "^13.0.0",
@@ -39,11 +40,18 @@
3940
"postcss": "^8.3.9",
4041
"rollup": "^4.0.2",
4142
"sass": "^1.43.2",
42-
"tailwindcss": "^3.0.11"
43+
"tailwindcss": "^3.0.11",
44+
"vitest": "^3.1.1"
4345
},
4446
"peerDependenciesMeta": {
47+
"karma": {
48+
"optional": true
49+
},
4550
"tailwindcss": {
4651
"optional": true
52+
},
53+
"vitest": {
54+
"optional": true
4755
}
4856
},
4957
"prettier": {

0 commit comments

Comments
 (0)