Skip to content

Commit

Permalink
chore: Jest to vitest migration (#5097)
Browse files Browse the repository at this point in the history
* eslint-lugin-query updated

* migrating from jest to vitest

* fixing error in ci pipeline

* adding coverage

* re-introduction vi.fn generics and addressing comments

* fixes from vi.fn generics second pass

* reverting vue-query tsconfig to not use esnext

* fixing frozen lock file error

* adding coverage option on vitest command

* resetting bad merge from alpha

* bumping node version in CI
  • Loading branch information
ZackDeRose committed Mar 10, 2023
1 parent 0948c81 commit 4aaaa51
Show file tree
Hide file tree
Showing 106 changed files with 1,130 additions and 1,008 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
version: 7
- uses: actions/setup-node@v3
with:
node-version: 16.14.2
node-version: 16.19.0
registry-url: https://registry.npmjs.org/
cache: 'pnpm'
- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
version: 7
- uses: actions/setup-node@v3
with:
node-version: 16.14.2
node-version: 16.19.0
cache: 'pnpm'
- name: Install dependencies
run: pnpm --filter "./packages/**" --filter query --prefer-offline install
Expand Down
46 changes: 0 additions & 46 deletions jest-preset.js

This file was deleted.

9 changes: 0 additions & 9 deletions jest.setup.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
"typescript": "4.7.4",
"vitest": "^0.27.1",
"vue": "^3.2.47"
},
"bundlewatch": {
Expand Down
4 changes: 0 additions & 4 deletions packages/codemods/jest.config.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/codemods/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"sideEffects": false,
"scripts": {
"test:eslint": "eslint --ext .ts,.tsx ./src",
"test:lib": "jest --config ./jest.config.ts",
"test:lib": "vitest run --coverage",
"test:lib:dev": "pnpm run test:lib --watch"
},
"files": [
Expand Down
2 changes: 0 additions & 2 deletions packages/codemods/src/v4/__tests__/key-transformation.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const defineTest = require('jscodeshift/dist/testUtils').defineTest

jest.autoMockOff()

defineTest(__dirname, 'key-transformation', null, 'default-import', {
parser: 'tsx',
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const defineTest = require('jscodeshift/dist/testUtils').defineTest

jest.autoMockOff()

defineTest(
__dirname,
'replace-import-specifier',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const defineTest = require('jscodeshift/dist/testUtils').defineTest

jest.autoMockOff()

defineTest(__dirname, 'remove-overloads', null, 'default-import', {
parser: 'tsx',
})
10 changes: 10 additions & 0 deletions packages/codemods/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
name: 'codemods',
watch: false,
globals: true,
coverage: { provider: 'istanbul' },
},
})
4 changes: 0 additions & 4 deletions packages/eslint-plugin-query/jest.config.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/eslint-plugin-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"dev": "tsup --watch --sourcemap",
"test:eslint": "eslint --ext .ts,.tsx ./src",
"test:types": "tsc",
"test:lib": "jest --config ./jest.config.ts",
"test:lib": "vitest run --coverage",
"test:lib:dev": "pnpm run test:lib --watch",
"build": "tsup --minify"
},
Expand Down
10 changes: 10 additions & 0 deletions packages/eslint-plugin-query/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
name: 'eslint-plugin-query',
watch: false,
globals: true,
coverage: { provider: 'istanbul' },
},
})
4 changes: 0 additions & 4 deletions packages/query-async-storage-persister/jest.config.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/query-async-storage-persister/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"clean": "rimraf ./build",
"test:eslint": "eslint --ext .ts,.tsx ./src",
"test:types": "tsc",
"test:lib": "jest --config ./jest.config.ts",
"test:lib": "vitest run --coverage",
"test:lib:dev": "pnpm run test:lib --watch",
"build:types": "tsc --build"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { asyncThrottle } from '../asyncThrottle'
import { sleep as delay } from './utils'
import { vi } from 'vitest'

describe('asyncThrottle', () => {
test('basic', async () => {
const interval = 10
const execTimeStamps: number[] = []
const mockFunc = jest.fn(
const mockFunc = vi.fn(
async (id: number, complete?: (value?: unknown) => void) => {
await delay(1)
execTimeStamps.push(Date.now())
Expand Down Expand Up @@ -33,7 +34,7 @@ describe('asyncThrottle', () => {
test('Bug #3331 case 1: Special timing', async () => {
const interval = 1000
const execTimeStamps: number[] = []
const mockFunc = jest.fn(
const mockFunc = vi.fn(
async (id: number, complete?: (value?: unknown) => void) => {
await delay(30)
execTimeStamps.push(Date.now())
Expand Down Expand Up @@ -62,7 +63,7 @@ describe('asyncThrottle', () => {
test('Bug #3331 case 2: "func" execution time is greater than the interval.', async () => {
const interval = 1000
const execTimeStamps: number[] = []
const mockFunc = jest.fn(
const mockFunc = vi.fn(
async (id: number, complete?: (value?: unknown) => void) => {
await delay(interval + 10)
execTimeStamps.push(Date.now())
Expand All @@ -86,7 +87,7 @@ describe('asyncThrottle', () => {
})

test('"func" throw error not break next invoke', async () => {
const mockFunc = jest.fn(
const mockFunc = vi.fn(
async (id: number, complete?: (value?: unknown) => void) => {
if (id === 1) throw new Error('error')
await delay(1)
Expand All @@ -105,11 +106,10 @@ describe('asyncThrottle', () => {
expect(mockFunc.mock.calls[1]?.[0]).toBe(2)
})

test('"onError" should be called when "func" throw error', (done) => {
test('"onError" should be called when "func" throw error', () => {
const err = new Error('error')
const handleError = (e: unknown) => {
expect(e).toBe(err)
done()
}

const testFunc = asyncThrottle(
Expand Down
10 changes: 10 additions & 0 deletions packages/query-async-storage-persister/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
name: 'query-async-storage-persister',
watch: false,
globals: true,
coverage: { provider: 'istanbul' },
},
})
4 changes: 0 additions & 4 deletions packages/query-core/jest.config.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/query-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"clean": "rimraf ./build",
"test:eslint": "eslint --ext .ts,.tsx ./src",
"test:types": "tsc",
"test:lib": "jest --config ./jest.config.ts",
"test:lib": "vitest run --coverage",
"test:lib:dev": "pnpm run test:lib --watch",
"build:types": "tsc --build"
}
Expand Down
26 changes: 12 additions & 14 deletions packages/query-core/src/tests/focusManager.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { sleep } from '../utils'
import { FocusManager } from '../focusManager'
import { setIsServer } from './utils'
import { vi } from 'vitest'

describe('focusManager', () => {
let focusManager: FocusManager
beforeEach(() => {
jest.resetModules()
vi.resetModules()
focusManager = new FocusManager()
})

it('should call previous remove handler when replacing an event listener', () => {
const remove1Spy = jest.fn()
const remove2Spy = jest.fn()
const remove1Spy = vi.fn()
const remove2Spy = vi.fn()

focusManager.setEventListener(() => remove1Spy)
focusManager.setEventListener(() => remove2Spy)
Expand Down Expand Up @@ -39,7 +40,7 @@ describe('focusManager', () => {
})

it('should not notify listeners on focus if already focused', async () => {
const subscriptionSpy = jest.fn()
const subscriptionSpy = vi.fn()
const unsubscribe = focusManager.subscribe(subscriptionSpy)

focusManager.setFocused(true)
Expand All @@ -66,7 +67,7 @@ describe('focusManager', () => {
test('cleanup (removeEventListener) should not be called if window is not defined', async () => {
const restoreIsServer = setIsServer(true)

const removeEventListenerSpy = jest.spyOn(globalThis, 'removeEventListener')
const removeEventListenerSpy = vi.spyOn(globalThis, 'removeEventListener')

const unsubscribe = focusManager.subscribe(() => undefined)

Expand All @@ -83,7 +84,7 @@ describe('focusManager', () => {
// @ts-expect-error
globalThis.window.addEventListener = undefined

const removeEventListenerSpy = jest.spyOn(globalThis, 'removeEventListener')
const removeEventListenerSpy = vi.spyOn(globalThis, 'removeEventListener')

const unsubscribe = focusManager.subscribe(() => undefined)

Expand All @@ -95,8 +96,8 @@ describe('focusManager', () => {
})

it('should replace default window listener when a new event listener is set', async () => {
const unsubscribeSpy = jest.fn().mockImplementation(() => undefined)
const handlerSpy = jest.fn().mockImplementation(() => unsubscribeSpy)
const unsubscribeSpy = vi.fn().mockImplementation(() => undefined)
const handlerSpy = vi.fn().mockImplementation(() => unsubscribeSpy)

focusManager.setEventListener(() => handlerSpy())

Expand All @@ -115,12 +116,9 @@ describe('focusManager', () => {
})

test('should call removeEventListener when last listener unsubscribes', () => {
const addEventListenerSpy = jest.spyOn(
globalThis.window,
'addEventListener',
)
const addEventListenerSpy = vi.spyOn(globalThis.window, 'addEventListener')

const removeEventListenerSpy = jest.spyOn(
const removeEventListenerSpy = vi.spyOn(
globalThis.window,
'removeEventListener',
)
Expand All @@ -136,7 +134,7 @@ describe('focusManager', () => {
})

test('should keep setup function even if last listener unsubscribes', () => {
const setupSpy = jest.fn().mockImplementation(() => () => undefined)
const setupSpy = vi.fn().mockImplementation(() => () => undefined)

focusManager.setEventListener(setupSpy)

Expand Down

0 comments on commit 4aaaa51

Please sign in to comment.