Skip to content

Commit e10ed57

Browse files
fix: use concurrently to give correct watch behaviour (#380)
Vitest has projects which are a great way to organise test config, but watchTriggerPatterns is global, not per project. If you want a single project to behave like e2e tests (i.e. run if the source changes) and another like unit tests (i.e. run if a file they're importing changes), it doesn't work. Concurrently fixes this because it handles running the projects and they handle (separately) which tests should run.
1 parent b55a7a3 commit e10ed57

5 files changed

Lines changed: 68 additions & 29 deletions

File tree

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"@types/chai": "^4.3.20",
4242
"@typescript-eslint/eslint-plugin": "^8.31.0",
4343
"@typescript-eslint/parser": "^8.31.0",
44+
"concurrently": "^9.2.0",
4445
"eslint": "9.30.1",
4546
"eslint-config-prettier": "10.1.5",
4647
"eslint-config-xo": "0.46.0",
@@ -54,7 +55,7 @@
5455
"ts-loader": "9.5.2",
5556
"typescript": "^5.8.2",
5657
"typescript-eslint": "^8.31.0",
57-
"vitest": "^3.1.4",
58+
"vitest": "^3.2.4",
5859
"webpack": "5.99.9",
5960
"webpack-cli": "5.1.4"
6061
},
@@ -93,6 +94,9 @@
9394
"lint": "eslint . --max-warnings 0 && prettier --check . && tsc --build",
9495
"format": "prettier --write .",
9596
"test": "vitest",
97+
"test:watch": "concurrently --prefix none \"pnpm test:unit --watch\" \"pnpm test:integration --watch\"",
98+
"test:unit": "vitest --config vitest.unit.config.mjs",
99+
"test:integration": "vitest --config vitest.integration.config.mjs",
96100
"prepare": "husky install",
97101
"prepublishOnly": "pnpm clean:build && pnpm build",
98102
"webpack": "webpack"

pnpm-lock.yaml

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vitest.config.mjs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,7 @@
1-
import { configDefaults, defineConfig } from "vitest/config";
2-
3-
const integrationTests = [
4-
"./packages/main/integration-tests/index.test.ts",
5-
"./packages/main/integration-tests/timeout.test.ts",
6-
];
1+
import { defineConfig } from "vitest/config";
72

83
export default defineConfig({
9-
assetsInclude: ["**/*.py"],
104
test: {
11-
environment: "puppeteer",
12-
exclude: [...configDefaults.exclude, "packages/*/build", "dist"],
13-
globals: true, // TODO: remove this OR include vitest types
14-
globalSetup: "vitest-environment-puppeteer/global-init",
15-
watchTriggerPatterns: [
16-
{
17-
pattern: /packages.*\.ts$/,
18-
testsToRun: (id) => {
19-
// If the changed file is a test, it's the only test that could be
20-
// affected.
21-
const isTestFile = id.endsWith(".test.ts");
22-
if (isTestFile) return id;
23-
24-
// Otherwise, if there is a test file, this is it:
25-
const testFile = id.slice(0, -3) + ".test.ts";
26-
// In principle any source change could impact the integration tests
27-
return [...integrationTests, testFile];
28-
},
29-
},
30-
],
5+
projects: ["./vitest.integration.config.mjs", "./vitest.unit.config.mjs"],
316
},
327
});

vitest.integration.config.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { configDefaults, defineConfig } from "vitest/config";
2+
3+
const integrationTests = [
4+
"./packages/tests/integration-tests/index.test.ts",
5+
"./packages/tests/integration-tests/timeout.test.ts",
6+
];
7+
8+
export default defineConfig({
9+
test: {
10+
watchTriggerPatterns: [
11+
{
12+
pattern: /packages\/.*\/.*\.ts$/,
13+
testsToRun: () => {
14+
// Any source change could impact the integration tests
15+
return integrationTests;
16+
},
17+
},
18+
],
19+
name: "integration",
20+
globals: true, // TODO: remove this OR include vitest types
21+
environment: "puppeteer",
22+
include: integrationTests,
23+
exclude: [...configDefaults.exclude, "packages/*/build", "dist"],
24+
globalSetup: "vitest-environment-puppeteer/global-init",
25+
},
26+
});

vitest.unit.config.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { configDefaults, defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
assetsInclude: ["**/*.py"],
5+
test: {
6+
name: "unit",
7+
globals: true, // TODO: remove this OR include vitest types
8+
include: ["**/*.test.ts", "**/*.test.tsx"],
9+
exclude: [
10+
...configDefaults.exclude,
11+
"packages/*/build",
12+
"dist",
13+
"packages/tests/integration-tests",
14+
],
15+
},
16+
});

0 commit comments

Comments
 (0)