Skip to content

Commit 9039a5f

Browse files
committed
Switch from Jest to Vitest
1 parent e2e8c78 commit 9039a5f

13 files changed

+2095
-3605
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
/**/*.js
2+
build/**
3+
tmp/**
4+
coverage/**

.eslintrc.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
"sourceType": "module",
1111
"ecmaVersion": 2020
1212
},
13-
"plugins": ["@typescript-eslint", "jest"],
13+
"plugins": ["@typescript-eslint", "vitest"],
1414
"extends": [
1515
"eslint:recommended",
1616
"plugin:@typescript-eslint/recommended",
17-
"plugin:jest/recommended",
17+
"plugin:vitest/recommended",
1818
"prettier"
1919
],
2020
"rules": {

.github/workflows/nodejs.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ jobs:
77
runs-on: ubuntu-latest
88

99
steps:
10-
- uses: actions/checkout@v2
11-
- uses: volta-cli/action@v1
10+
- uses: actions/checkout@v4
11+
- uses: volta-cli/action@v4
1212
- run: npm ci --no-audit
1313
- run: npm run lint --if-present
14+
- run: npm run prettier:check --if-present
1415
- run: npm test
1516
- run: npm run build --if-present
16-
env:
17-
CI: true

.prettierrc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
{
2+
"parser": "typescript",
3+
"semi": true,
24
"singleQuote": true,
3-
"trailingComma": "all",
4-
"overrides": [
5-
{
6-
"files": ["*.ts", "*.mts"],
7-
"options": {
8-
"parser": "typescript"
9-
}
10-
}
11-
]
5+
"trailingComma": "all"
126
}

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
- [TypeScript][typescript] [5.4][typescript-5-4]
1414
- [ESM][esm]
1515
- [ESLint][eslint] with some initial rules recommendation
16-
- [Jest][jest] for fast unit testing and code coverage
17-
- Type definitions for Node.js and Jest
16+
- [Vitest][vitest] for fast unit testing and code coverage
17+
- Type definitions for Node.js
1818
- [Prettier][prettier] to enforce consistent code style
1919
- NPM [scripts](#available-scripts) for common operations
2020
- [EditorConfig][editorconfig] for consistent coding style
@@ -53,22 +53,31 @@ unzip node-typescript-boilerplate.zip && rm node-typescript-boilerplate.zip
5353

5454
## Available Scripts
5555

56-
- `clean` - remove coverage data, Jest cache and transpiled files,
56+
- `clean` - remove coverage data, cache and transpiled files,
5757
- `prebuild` - lint source files and tests before building,
5858
- `build` - transpile TypeScript to ES6,
5959
- `build:watch` - interactive watch mode to automatically transpile source files,
6060
- `lint` - lint source files and tests,
6161
- `prettier` - reformat files,
6262
- `test` - run tests,
6363
- `test:watch` - interactive watch mode to automatically re-run tests
64+
- `test:coverage` - run test and print out test coverage
6465

6566
## Additional Information
6667

6768
### Why include Volta
6869

70+
I recommend to [install][volta-getting-started] Volta and use it to manage your project's toolchain.
71+
6972
[Volta][volta]’s toolchain always keeps track of where you are, it makes sure the tools you use always respect the settings of the project you’re working on. This means you don’t have to worry about changing the state of your installed software when switching between projects. For example, it's [used by engineers at LinkedIn][volta-tomdale] to standardize tools and have reproducible development environments.
7073

71-
I recommend to [install][volta-getting-started] Volta and use it to manage your project's toolchain.
74+
### Why Vitest instead of Jest
75+
76+
I recommend using [Vitest][vitest] for unit and integration testing of your TypeScript code.
77+
78+
In 2023, my team and I gradually switched from Jest to [Vitest][vitest] in all the projects. We've found out that generally, Vitest is faster than Jest, especially for large test suits. Furthermore, Vitest has native support for ES modules, is easier to configure, and has a much nicer developer experience when used with TypeScript. For example, when working with mocks, spies and types.
79+
80+
Nevertheless, the choice of specific tooling always depends on the specific requirements and characteristics of the project.
7281

7382
### ES Modules
7483

@@ -97,9 +106,7 @@ Licensed under the APLv2. See the [LICENSE](https://github.com/jsynowiec/node-ty
97106
[license]: https://github.com/jsynowiec/node-typescript-boilerplate/blob/main/LICENSE
98107
[sponsor-badge]: https://img.shields.io/badge/♥-Sponsor-fc0fb5.svg
99108
[sponsor]: https://github.com/sponsors/jsynowiec
100-
[jest]: https://facebook.github.io/jest/
101109
[eslint]: https://github.com/eslint/eslint
102-
[wiki-js-tests]: https://github.com/jsynowiec/node-typescript-boilerplate/wiki/Unit-tests-in-plain-JavaScript
103110
[prettier]: https://prettier.io
104111
[volta]: https://volta.sh
105112
[volta-getting-started]: https://docs.volta.sh/guide/getting-started
@@ -111,3 +118,4 @@ Licensed under the APLv2. See the [LICENSE](https://github.com/jsynowiec/node-ty
111118
[nodejs-esm]: https://nodejs.org/docs/latest-v16.x/api/esm.html
112119
[ts47-esm]: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#esm-nodejs
113120
[editorconfig]: https://editorconfig.org
121+
[vitest]: https://vitest.dev

__tests__/main.test.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

__tests__/unit/main.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { describe, it, afterEach, beforeEach, vi, expect } from 'vitest';
2+
import { Delays, greeter } from '../../src/main.js';
3+
4+
describe('greeter function', () => {
5+
const name = 'John';
6+
7+
beforeEach(() => {
8+
// Read more about fake timers
9+
// https://vitest.dev/api/vi.html#vi-usefaketimers
10+
vi.useFakeTimers();
11+
});
12+
13+
afterEach(() => {
14+
vi.useRealTimers();
15+
vi.restoreAllMocks();
16+
});
17+
18+
// Assert if setTimeout was called properly
19+
it('delays the greeting by 2 seconds', async () => {
20+
vi.spyOn(global, 'setTimeout');
21+
const p = greeter(name);
22+
23+
await vi.runAllTimersAsync();
24+
await p;
25+
26+
expect(setTimeout).toHaveBeenCalledTimes(1);
27+
expect(setTimeout).toHaveBeenLastCalledWith(
28+
expect.any(Function),
29+
Delays.Long,
30+
);
31+
});
32+
33+
// Assert greeter result
34+
it('greets a user with `Hello, {name}` message', async () => {
35+
const p = greeter(name);
36+
await vi.runAllTimersAsync();
37+
38+
expect(await p).toBe(`Hello, ${name}`);
39+
});
40+
});

jest.config.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)