Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Vitest #260

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .depcheckrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
"@metamask/eslint-config",
"@metamask/eslint-config-*",
"@types/*",
"@vitest/coverage-istanbul",
"@vitest/eslint-plugin",
"@yarnpkg/types",
"eslint-config-*",
"eslint-import-resolver-typescript",
"eslint-plugin-*",
"prettier-plugin-packagejson",
"ts-node",
"typedoc",
"typescript-eslint"
"typescript-eslint",
"vite"
]
}
4 changes: 2 additions & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base, { createConfig } from '@metamask/eslint-config';
import jest from '@metamask/eslint-config-jest';
import nodejs from '@metamask/eslint-config-nodejs';
import typescript from '@metamask/eslint-config-typescript';
import vitest from '@metamask/eslint-config-vitest';

const config = createConfig([
{
Expand Down Expand Up @@ -40,7 +40,7 @@ const config = createConfig([

{
files: ['**/*.test.ts', '**/*.test.js'],
extends: [jest, nodejs],
extends: [vitest, nodejs],
},
]);

Expand Down
204 changes: 0 additions & 204 deletions jest.config.js

This file was deleted.

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,40 @@
"lint:fix": "yarn lint:eslint --fix && yarn lint:constraints --fix && yarn lint:misc --write && yarn lint:dependencies && yarn lint:changelog",
"lint:misc": "prettier '**/*.json' '**/*.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern",
"prepack": "./scripts/prepack.sh",
"test": "jest && jest-it-up && attw --pack",
"test:watch": "jest --watch"
"test": "vitest && attw --pack",
"test:watch": "vitest --watch"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.15.3",
"@lavamoat/allow-scripts": "^3.0.4",
"@lavamoat/preinstall-always-fail": "^2.0.0",
"@metamask/auto-changelog": "^4.0.0",
"@metamask/eslint-config": "^14.0.0",
"@metamask/eslint-config-jest": "^14.0.0",
"@metamask/eslint-config-nodejs": "^14.0.0",
"@metamask/eslint-config-typescript": "^14.0.0",
"@metamask/eslint-config-vitest": "^1.0.0",
"@ts-bridge/cli": "^0.6.3",
"@types/jest": "^28.1.6",
"@types/node": "^18.18",
"@vitest/coverage-istanbul": "^3.0.7",
"@vitest/eslint-plugin": "^1.1.4",
"@yarnpkg/types": "^4.0.0-rc.52",
"depcheck": "^1.4.3",
"eslint": "^9.11.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.3",
"eslint-plugin-import-x": "^4.3.0",
"eslint-plugin-jest": "^28.8.3",
"eslint-plugin-jsdoc": "^50.2.4",
"eslint-plugin-n": "^17.10.3",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-promise": "^7.1.0",
"jest": "^28.1.3",
"jest-it-up": "^2.0.2",
"prettier": "^3.3.3",
"prettier-plugin-packagejson": "^2.3.0",
"ts-jest": "^28.0.7",
"prettier-plugin-packagejson": "^2.5.8",
"ts-node": "^10.7.0",
"typedoc": "^0.26.11",
"typescript": "~5.4.5",
"typescript-eslint": "^8.6.0"
"typescript": "~5.7.3",
"typescript-eslint": "^8.6.0",
"vite": "^6.2.0",
"vitest": "^3.0.7"
},
"packageManager": "yarn@4.1.1",
"engines": {
Expand All @@ -87,7 +86,8 @@
},
"lavamoat": {
"allowScripts": {
"@lavamoat/preinstall-always-fail": false
"@lavamoat/preinstall-always-fail": false,
"vite>esbuild": false
}
}
}
9 changes: 9 additions & 0 deletions src/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, it, expectTypeOf } from 'vitest';

import greeter from '.';

describe('greeter', () => {
it('returns a string', () => {
expectTypeOf(greeter('Huey')).toEqualTypeOf<string>();
});
});
4 changes: 3 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { describe, it, expect } from 'vitest';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vitest doesn't inject globals like describe, it, expect by default. We could enable a setting for this, but I think importing is preferred over using globals.


import greeter from '.';

describe('Test', () => {
describe('greeter', () => {
it('greets', () => {
const name = 'Huey';
const result = greeter(name);
Expand Down
11 changes: 11 additions & 0 deletions tsconfig.test.json
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Vite uses esbuild under the hood, tests aren't type checked by default. Using a separate tsconfig seemed to be the most reliable way to get type checking and tests working.

For example, if a test in a test-d.ts file fails, Vitest outputs the following error:

 FAIL  src/index.test-d.ts > greeter > returns a string
TypeCheckError: Type 'number' does not satisfy the constraint '"Expected number, Actual string"'.
 ❯ src/index.test-d.ts:7:49
      5| describe('greeter', () => {
      6|   it('returns a string', () => {
      7|     expectTypeOf(greeter('Huey')).toEqualTypeOf<number>();
       |                                                 ^
      8|   });
      9| });

And type errors in regular test files are detected as well, as unhandled source errors:

TypeCheckError: Type 'string' is not assignable to type 'number'.
 ❯ src/index.test.ts:7:11
      5| describe('greeter', () => {
      6|   it('greets', () => {
      7|     const name: number = 'Huey';
       |           ^
      8|     const result = greeter(name);
      9|     expect(result).toBe('Hello, Huey!');

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ES2022",
"moduleResolution": "Bundler",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use bundler here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vitest uses Vite to transpile TypeScript. Bundler is the most appropriate setting in this case and recommended by Vite.

"skipLibCheck": true,
"skipDefaultLibCheck": true
},
"include": ["./src/**/*.test.ts", "./src/**/*.test-d.ts"],
"exclude": ["./dist", "**/node_modules"]
}
54 changes: 54 additions & 0 deletions vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
// Vitest enables watch mode by default. We disable it here, so it can be
// explicitly enabled with `yarn test:watch`.
watch: false,

// The files to include in the test run.
include: ['src/**/*.test.ts'],

coverage: {
enabled: true,

// Configure the coverage provider. We use `istanbul` here, because it
// is more stable than `v8`.
provider: 'istanbul',

// The files to include in the coverage report.
include: [
'src/**/*.ts',
'src/**/*.tsx',
'src/**/*.js',
'src/**/*.jsx',
'src/**/*.mjs',
],

// The files to exclude from the coverage report. Vitest excludes test
// files by default, but not `test-d.ts` files.
exclude: ['src/**/*.test-d.ts'],

// Coverage thresholds. If the coverage is below these thresholds, the
// test will fail.
thresholds: {
// Auto-update the coverage thresholds. When this is enabled, the
// thresholds will be updated automatically when the coverage is
// above the current thresholds.
autoUpdate: true,

branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},

typecheck: {
enabled: true,

// The path to the tsconfig file to use for type checking.
tsconfig: './tsconfig.test.json',
},
},
});
Loading
Loading