Skip to content

Commit 0d0b0bf

Browse files
committed
tests
1 parent 9040255 commit 0d0b0bf

40 files changed

+902
-1602
lines changed

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,31 @@
6666
"devDependencies": {
6767
"@4c/babel-preset": "^10.2.1",
6868
"@4c/cli": "^4.0.4",
69-
"@4c/jest-preset": "^1.8.1",
7069
"@4c/rollout": "^4.0.2",
7170
"@4c/tsconfig": "^0.4.1",
7271
"@babel/cli": "^7.26.4",
7372
"@babel/core": "^7.26.0",
7473
"@babel/preset-typescript": "^7.26.0",
7574
"@testing-library/dom": "^10.4.0",
7675
"@testing-library/react": "^16.1.0",
77-
"@types/jest": "^29.5.3",
7876
"@types/lodash": "^4.14.195",
7977
"@types/react": "^19.0.2",
80-
"babel-jest": "^29.6.1",
8178
"babel-plugin-transform-rename-import": "^2.3.0",
8279
"cherry-pick": "^0.5.0",
8380
"codecov": "^3.8.3",
8481
"concurrently": "^9.1.2",
8582
"eslint": "^8.44.0",
8683
"gh-pages": "^3.1.0",
8784
"husky": "^4.3.6",
88-
"jest": "^29.6.1",
89-
"jest-environment-jsdom": "^29.6.1",
85+
"jsdom": "^25.0.1",
9086
"lint-staged": "^13.2.3",
9187
"mq-polyfill": "^1.1.8",
9288
"prettier": "^3.0.0",
9389
"react": "^19.0.0",
9490
"react-dom": "^19.0.0",
9591
"rimraf": "^5.0.1",
96-
"typescript": "^5.1.6"
92+
"typescript": "^5.1.6",
93+
"vitest": "^2.1.8"
9794
},
9895
"dependencies": {
9996
"dequal": "^2.0.3"

test/setup.js

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import matchMediaPolyfill from 'mq-polyfill'
2+
import { cleanup } from '@testing-library/react'
3+
import { afterEach } from 'vitest'
4+
5+
afterEach(cleanup)
26

37
// https://github.com/bigslycat/mq-polyfill
48

@@ -18,35 +22,3 @@ if (typeof window !== 'undefined') {
1822
}).dispatchEvent(new this.Event('resize'))
1923
}
2024
}
21-
22-
let expectedErrors = 0
23-
let actualErrors = 0
24-
function onError(e) {
25-
if (expectedErrors) {
26-
e.preventDefault()
27-
}
28-
actualErrors += 1
29-
}
30-
31-
expect.errors = (num) => {
32-
expectedErrors = num
33-
}
34-
35-
beforeEach(() => {
36-
expectedErrors = 0
37-
actualErrors = 0
38-
if (typeof window !== 'undefined') {
39-
window.addEventListener('error', onError)
40-
}
41-
})
42-
43-
afterEach(() => {
44-
if (typeof window !== 'undefined') {
45-
window.removeEventListener('error', onError)
46-
}
47-
if (expectedErrors) {
48-
expect(actualErrors).toBe(expectedErrors)
49-
}
50-
51-
expectedErrors = 0
52-
})

test/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"extends": "..",
33
"compilerOptions": {
44
"rootDir": "..",
5-
"types": ["jest"],
5+
"noEmit": true,
66
"noImplicitAny": false
77
},
88
"include": [".", "../src"]

test/useAnimationFrame.test.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { renderHook, act } from '@testing-library/react'
22
import useAnimationFrame from '../src/useAnimationFrame.js'
3+
import { describe, it, beforeAll, afterAll, vi, expect } from 'vitest'
34

45
describe('useAnimationFrame', () => {
56
let rafSpy, rafCancelSpy
67

78
beforeAll(() => {
8-
rafSpy = jest
9+
rafSpy = vi
910
.spyOn(window, 'requestAnimationFrame')
1011
.mockImplementation((cb) => {
1112
return setTimeout(() => cb(1)) as any
1213
})
1314

14-
rafCancelSpy = jest
15+
rafCancelSpy = vi
1516
.spyOn(window, 'cancelAnimationFrame')
1617
.mockImplementation((handle) => {
1718
clearTimeout(handle)
@@ -24,48 +25,48 @@ describe('useAnimationFrame', () => {
2425
})
2526

2627
it('should requestAnimationFrame', () => {
27-
jest.useFakeTimers()
28+
vi.useFakeTimers()
2829

29-
let spy = jest.fn()
30+
let spy = vi.fn()
3031

3132
const { result } = renderHook(useAnimationFrame)
3233

3334
act(() => result.current!.request(spy))
3435

3536
expect(spy).not.toHaveBeenCalled()
3637

37-
jest.runAllTimers()
38+
vi.runAllTimers()
3839

3940
expect(spy).toHaveBeenCalledTimes(1)
4041
})
4142

4243
it('should cancel a request', () => {
43-
jest.useFakeTimers()
44+
vi.useFakeTimers()
4445

45-
let spy = jest.fn()
46+
let spy = vi.fn()
4647
const { result } = renderHook(useAnimationFrame)
4748

4849
act(() => {
4950
result.current.request(spy)
5051

5152
result.current.cancel()
5253
})
53-
jest.runAllTimers()
54+
vi.runAllTimers()
5455

5556
expect(spy).toHaveBeenCalledTimes(0)
5657
})
5758

5859
it('should cancel a request on unmount', () => {
59-
jest.useFakeTimers()
60+
vi.useFakeTimers()
6061

61-
let spy = jest.fn()
62+
let spy = vi.fn()
6263
const { result, unmount } = renderHook(useAnimationFrame)
6364

6465
act(() => result.current!.request(spy))
6566

6667
unmount()
6768

68-
jest.runAllTimers()
69+
vi.runAllTimers()
6970

7071
expect(spy).toHaveBeenCalledTimes(0)
7172
})

test/useBreakpoint.ssr.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
2-
* @jest-environment node
2+
* @vitest-environment node
33
*/
44

5-
import React from 'react'
65
import { renderToString } from 'react-dom/server'
76
import useBreakpoint from '../src/useBreakpoint.js'
7+
import { describe, it, expect } from 'vitest'
88

99
describe('useBreakpoint (ssr)', () => {
1010
it('should match immediately if possible', () => {

test/useBreakpoint.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ import useBreakpoint, {
33
createBreakpointHook,
44
} from '../src/useBreakpoint.js'
55

6+
import { describe, it, vi, expect, afterEach, beforeEach } from 'vitest'
67
import { renderHook } from '@testing-library/react'
78

89
interface Props {
910
breakpoint: DefaultBreakpointMap
1011
}
1112

1213
describe('useBreakpoint', () => {
13-
let matchMediaSpy: jest.SpyInstance<MediaQueryList, [string]>
14+
let matchMediaSpy
1415

1516
beforeEach(() => {
16-
matchMediaSpy = jest.spyOn(window, 'matchMedia')
17+
matchMediaSpy = vi.spyOn(window, 'matchMedia')
1718
window.resizeTo(1024, window.innerHeight)
1819
})
1920

test/useCallbackRef.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import React, { useEffect } from 'react'
1+
import { useEffect } from 'react'
22
import { render, act } from '@testing-library/react'
33

4+
import { describe, it, vi, expect } from 'vitest'
45
import useCallbackRef from '../src/useCallbackRef.js'
56

67
describe('useCallbackRef', () => {
78
it('should update value and be fresh in an effect', () => {
8-
const effectSpy = jest.fn()
9+
const effectSpy = vi.fn()
910

1011
function Wrapper({ toggle }) {
1112
const [ref, attachRef] = useCallbackRef<

test/useCommittedRef.test.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
import { useEffect } from 'react'
22
import { renderHook } from '@testing-library/react'
33
import useCommittedRef from '../src/useCommittedRef.js'
4+
import { describe, it, vi, expect } from 'vitest'
45

56
describe('useCommittedRef', () => {
67
it('should use fresh value', () => {
7-
function Foo({ fn }) {
8-
const fnRef = useCommittedRef(fn)
9-
10-
useEffect(() => {
11-
fnRef.current()
12-
})
13-
14-
return null
15-
}
16-
17-
const spyA = jest.fn()
18-
const spyB = jest.fn()
8+
const spyA = vi.fn()
9+
const spyB = vi.fn()
1910

2011
const { rerender } = renderHook(
2112
(fn) => {

test/useCustomEffect.test.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import { describe, it, vi, expect } from 'vitest'
12
import useCustomEffect from '../src/useCustomEffect.js'
23
import useImmediateUpdateEffect from '../src/useImmediateUpdateEffect.js'
34
import { renderHook } from './helpers.js'
45

56
describe('useCustomEffect', () => {
67
it('should run custom isEqual logic', () => {
7-
const teardown = jest.fn()
8+
const teardown = vi.fn()
89

9-
const spy = jest.fn().mockImplementation(() => teardown)
10-
const isEqual = jest.fn((next, prev) => next[0].foo === prev[0].foo)
10+
const spy = vi.fn().mockImplementation(() => teardown)
11+
const isEqual = vi.fn((next, prev) => next[0].foo === prev[0].foo)
1112

1213
const [, wrapper] = renderHook(
1314
({ value }) => {
@@ -37,8 +38,8 @@ describe('useCustomEffect', () => {
3738
})
3839

3940
it('should accept different hooks', () => {
40-
const spy = jest.fn()
41-
const hookSpy = jest.fn().mockImplementation(useImmediateUpdateEffect)
41+
const spy = vi.fn()
42+
const hookSpy = vi.fn().mockImplementation(useImmediateUpdateEffect)
4243

4344
renderHook(
4445
({ value }) => {

0 commit comments

Comments
 (0)