Skip to content

Commit 21046ef

Browse files
authored
Migrate from Jest to Vitest (#86)
* Migrate from Jest to Vitest * Migrate from Jest to Vitest * Remove Jest shield
1 parent 7e659d5 commit 21046ef

File tree

9 files changed

+766
-3089
lines changed

9 files changed

+766
-3089
lines changed

.babelrc

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

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,4 @@ jobs:
110110
HUSKY: 0
111111

112112
- name: Run tests
113-
run: yarn jest
113+
run: yarn unit

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![npm](https://img.shields.io/npm/v/@wojtekmaj/react-async-button.svg)](https://www.npmjs.com/package/@wojtekmaj/react-async-button) ![downloads](https://img.shields.io/npm/dt/@wojtekmaj/react-async-button.svg) [![CI](https://github.com/wojtekmaj/react-async-button/workflows/CI/badge.svg)](https://github.com/wojtekmaj/react-async-button/actions) [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest)
1+
[![npm](https://img.shields.io/npm/v/@wojtekmaj/react-async-button.svg)](https://www.npmjs.com/package/@wojtekmaj/react-async-button) ![downloads](https://img.shields.io/npm/dt/@wojtekmaj/react-async-button.svg) [![CI](https://github.com/wojtekmaj/react-async-button/workflows/CI/badge.svg)](https://github.com/wojtekmaj/react-async-button/actions)
22

33
# React-Async-Button
44

jest.config.json

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

package.json

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
"build-esm": "tsc --project tsconfig.build.json --outDir dist/esm --module esnext",
1212
"build-cjs": "tsc --project tsconfig.build.json --outDir dist/cjs --module commonjs",
1313
"clean": "rimraf dist",
14-
"jest": "jest",
1514
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
1615
"postinstall": "husky install",
1716
"prepack": "yarn clean && yarn build",
1817
"prettier": "prettier --check . --cache",
19-
"test": "yarn lint && yarn tsc && yarn prettier && yarn jest",
20-
"tsc": "tsc --noEmit"
18+
"test": "yarn lint && yarn tsc && yarn prettier && yarn unit",
19+
"tsc": "tsc --noEmit",
20+
"unit": "vitest run"
2121
},
2222
"keywords": [
2323
"react",
@@ -35,28 +35,23 @@
3535
"prop-types": "^15.6.0"
3636
},
3737
"devDependencies": {
38-
"@babel/core": "^7.15.0",
39-
"@babel/preset-env": "^7.15.0",
40-
"@babel/preset-react": "^7.14.0",
41-
"@babel/preset-typescript": "^7.18.6",
4238
"@testing-library/dom": "^8.11.0",
4339
"@testing-library/jest-dom": "^5.15.0",
4440
"@testing-library/react": "^13.4.0",
4541
"@testing-library/user-event": "^14.4.0",
46-
"@types/jest": "^29.0.0",
4742
"@typescript-eslint/eslint-plugin": "^5.41.0",
4843
"@typescript-eslint/parser": "^5.44.0",
4944
"eslint": "^8.26.0",
5045
"eslint-config-wojtekmaj": "^0.7.1",
5146
"husky": "^8.0.0",
52-
"jest": "^29.0.0",
53-
"jest-environment-jsdom": "^29.0.0",
47+
"jsdom": "^21.1.0",
5448
"prettier": "^2.7.0",
5549
"pretty-quick": "^3.1.0",
5650
"react": "^18.2.0",
5751
"react-dom": "^18.2.0",
5852
"rimraf": "^3.0.0",
59-
"typescript": "^4.9.4"
53+
"typescript": "^4.9.4",
54+
"vitest": "^0.29.2"
6055
},
6156
"peerDependencies": {
6257
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"

src/AsyncButton.spec.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
12
import React, { createRef } from 'react';
23
import { act, render, screen } from '@testing-library/react';
34
import userEvent from '@testing-library/user-event';
45

56
import AsyncButton from './index';
67

7-
jest.useFakeTimers();
8+
vi.useFakeTimers();
89

910
const pendingConfig = {
1011
children: 'Loading…',
@@ -29,7 +30,7 @@ describe('<AsyncButton /> component', () => {
2930
let user: ReturnType<typeof userEvent.setup>;
3031
beforeEach(() => {
3132
user = userEvent.setup({
32-
advanceTimers: jest.advanceTimersByTime,
33+
advanceTimers: vi.advanceTimersByTime.bind(vi),
3334
});
3435
});
3536

@@ -50,7 +51,7 @@ describe('<AsyncButton /> component', () => {
5051
});
5152

5253
it('calls onClick properly', async () => {
53-
const onClick = jest.fn();
54+
const onClick = vi.fn();
5455

5556
render(<AsyncButton {...defaultProps} onClick={onClick} />);
5657

@@ -92,14 +93,14 @@ describe('<AsyncButton /> component', () => {
9293
expect(button2).toHaveTextContent('Success!');
9394

9495
act(() => {
95-
jest.advanceTimersByTime(1000);
96+
vi.advanceTimersByTime(1000);
9697
});
9798

9899
const button3 = screen.getByRole('button');
99100
expect(button3).toHaveTextContent('Success!');
100101

101102
act(() => {
102-
jest.advanceTimersByTime(1000);
103+
vi.advanceTimersByTime(1000);
103104
});
104105

105106
const button4 = screen.getByRole('button');
@@ -175,14 +176,14 @@ describe('<AsyncButton /> component', () => {
175176
expect(button3).toHaveTextContent('Success!');
176177

177178
act(() => {
178-
jest.advanceTimersByTime(1000);
179+
vi.advanceTimersByTime(1000);
179180
});
180181

181182
const button4 = screen.getByRole('button');
182183
expect(button4).toHaveTextContent('Success!');
183184

184185
act(() => {
185-
jest.advanceTimersByTime(1000);
186+
vi.advanceTimersByTime(1000);
186187
});
187188

188189
const button5 = screen.getByRole('button');

vitest.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// eslint-disable-next-line import/no-unresolved
2+
import { defineConfig } from 'vitest/config';
3+
4+
export default defineConfig({
5+
test: {
6+
environment: 'jsdom',
7+
setupFiles: 'vitest.setup.ts',
8+
},
9+
});

vitest.setup.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { expect, afterEach } from 'vitest';
2+
import { cleanup } from '@testing-library/react';
3+
import matchers from '@testing-library/jest-dom/matchers';
4+
5+
expect.extend(matchers);
6+
7+
afterEach(() => {
8+
cleanup();
9+
});

0 commit comments

Comments
 (0)