Skip to content

Commit 3df4ebe

Browse files
committed
Set-up to run tests with vitest browser mode
1 parent e74fcd2 commit 3df4ebe

File tree

8 files changed

+1124
-2934
lines changed

8 files changed

+1124
-2934
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ jobs:
1616
path: node_modules
1717
key: ${{ runner.os }}-node_modules-${{ hashFiles('yarn.lock') }}
1818
- name: Install
19-
run: yarn install --immutable
19+
run: yarn install --immutable && yarn playwright install
2020
- name: Format
2121
run: yarn checkformatting
2222
- name: Lint
2323
run: yarn lint
2424
- name: Typecheck
2525
run: yarn typecheck
2626
- name: Test
27-
run: yarn test
27+
run: yarn test:vitest
2828
- name: Upload coverage to Codecov
2929
uses: codecov/codecov-action@v4
3030
with:

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,5 @@ build: node_modules/.uptodate
6565

6666
node_modules/.uptodate: package.json yarn.lock
6767
yarn install
68+
yarn playwright install
6869
@touch $@

gulpfile.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,17 @@ gulp.task(
6060
}),
6161
),
6262
);
63+
64+
// Some (eg. a11y) tests rely on CSS bundles. We assume that JS will always take
65+
// longer to build than CSS, so build in parallel.
66+
gulp.task(
67+
'test:vitest',
68+
gulp.parallel('build-test-css', () =>
69+
runTests({
70+
bootstrapFile: 'test/bootstrap.js',
71+
rollupConfig: 'rollup-tests.config.js',
72+
testsPattern: 'src/**/*-test.js',
73+
vitestConfig: './vitest.config.js',
74+
}),
75+
),
76+
);

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"@babel/preset-env": "^7.1.6",
1111
"@babel/preset-react": "^7.0.0",
1212
"@babel/preset-typescript": "^7.18.6",
13-
"@hypothesis/frontend-build": "^3.0.0",
13+
"@hypothesis/frontend-build": "^3.2.1",
1414
"@hypothesis/frontend-testing": "^1.4.0",
1515
"@rollup/plugin-babel": "^6.0.0",
1616
"@rollup/plugin-commonjs": "^28.0.0",
@@ -19,6 +19,8 @@
1919
"@rollup/plugin-terser": "^0.4.4",
2020
"@rollup/plugin-virtual": "^3.0.0",
2121
"@trivago/prettier-plugin-sort-imports": "^5.2.0",
22+
"@vitest/browser": "^3.1.1",
23+
"@vitest/coverage-istanbul": "^3.1.1",
2224
"autoprefixer": "^10.3.7",
2325
"axe-core": "^4.0.0",
2426
"babel-plugin-istanbul": "^7.0.0",
@@ -46,6 +48,7 @@
4648
"karma-source-map-support": "^1.4.0",
4749
"mocha": "11.1.0",
4850
"mustache": "^4.2.0",
51+
"playwright": "^1.51.1",
4952
"postcss": "^8.3.9",
5053
"preact": "^10.25.1",
5154
"prettier": "^3.0.0",
@@ -57,6 +60,7 @@
5760
"tailwindcss": "3.4.17",
5861
"typescript": "^5.0.2",
5962
"typescript-eslint": "^8.10.0",
63+
"vitest": "^3.1.1",
6064
"yalc": "^1.0.0-pre.50"
6165
},
6266
"peerDependencies": {
@@ -71,6 +75,7 @@
7175
"checkformatting": "prettier --cache --check '**/*.{js,scss,ts,tsx,md}'",
7276
"format": "prettier --cache --list-different --write '**/*.{js,scss,ts,tsx,md}'",
7377
"test": "gulp test",
78+
"test:vitest": "gulp test:vitest",
7479
"push": "yarn build && yalc push"
7580
},
7681
"prettier": {

rollup-tests.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,23 @@ export default {
6666
[
6767
'babel-plugin-istanbul',
6868
{
69+
// This needs to match the value set in coverage.exclude in vitest.config.js
6970
exclude: [
71+
'**/node_modules/**',
7072
'**/test/**/*.js',
7173
'**/test-util/**',
7274
'src/components/icons/**/*.ts*',
7375
'src/pattern-library/**/*.js',
7476
'src/pattern-library/**/*.ts*',
7577
],
78+
79+
// These two configuration options needed to match the values that
80+
// vitest sets internally, so that they pick the coverage
81+
// instrumentation generated by babel-plugin-istanbul instead of
82+
// trying to generate their own.
83+
// See https://github.com/vitest-dev/vitest/discussions/7841#discussioncomment-12855608
84+
coverageGlobalScope: 'globalThis',
85+
coverageVariable: '__VITEST_COVERAGE__',
7686
},
7787
],
7888
],

test/bootstrap.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,10 @@ sinon.assert.expose(assert, { prefix: null });
1212
// karma-sinon plugins
1313
globalThis.assert = assert;
1414
globalThis.sinon = sinon;
15+
globalThis.context = globalThis.context ?? globalThis.describe;
1516

1617
// Configure Enzyme for UI tests.
1718
configure({ adapter: new Adapter() });
1819
afterEach(() => {
1920
unmountAll();
2021
});
21-
22-
// Ensure that uncaught exceptions between tests result in the tests failing.
23-
// This works around an issue with mocha / karma-mocha, see
24-
// https://github.com/hypothesis/client/issues/2249.
25-
let pendingError = null;
26-
let pendingErrorNotice = null;
27-
28-
window.addEventListener('error', event => {
29-
pendingError = event.error;
30-
pendingErrorNotice = 'An uncaught exception was thrown between tests';
31-
});
32-
window.addEventListener('unhandledrejection', event => {
33-
pendingError = event.reason;
34-
pendingErrorNotice = 'An uncaught promise rejection occurred between tests';
35-
});
36-
37-
afterEach(() => {
38-
if (pendingError) {
39-
console.error(pendingErrorNotice);
40-
throw pendingError;
41-
}
42-
});

vitest.config.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { defineConfig } from 'vitest/config';
2+
3+
export default defineConfig({
4+
test: {
5+
browser: {
6+
provider: 'playwright',
7+
enabled: true,
8+
headless: true,
9+
screenshotFailures: false,
10+
instances: [{ browser: 'chromium' }],
11+
},
12+
globals: true,
13+
14+
// CSS bundle relied upon by accessibility tests (eg. for color-contrast
15+
// checks).
16+
setupFiles: './build/styles/test.css',
17+
include: [
18+
// Test bundle
19+
'./build/scripts/tests.bundle.js',
20+
],
21+
22+
coverage: {
23+
enabled: true,
24+
provider: 'istanbul',
25+
reportsDirectory: './coverage',
26+
reporter: ['json', 'clover', 'html'],
27+
include: ['src/**/*.{ts,tsx}'],
28+
exclude: [
29+
'**/node_modules/**',
30+
'**/test/**/*.js',
31+
'**/test-util/**',
32+
'src/components/icons/**/*.ts*',
33+
'src/pattern-library/**/*.js',
34+
'src/pattern-library/**/*.ts*',
35+
],
36+
},
37+
},
38+
});

0 commit comments

Comments
 (0)