Skip to content

Commit

Permalink
πŸ— πŸ”₯ remove Jest, use Vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
astoilkov committed Sep 13, 2023
1 parent f3f4597 commit b5e0c56
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 27 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/test
*.d.ts
vitest.config.ts
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"build": "tsc",
"size": "yarn run build && size-limit",
"lint": "eslint --cache --format=pretty --ext=.ts ./",
"test": "yarn run build && yarn run lint && if [[ -z $CI ]]; then jest --coverage --coverageReporters=text; else jest --coverage; fi",
"test": "yarn run build && yarn run lint && if [[ -z $CI ]]; then vitest --run --coverage.enabled --coverage.reporter=text --coverage.reporter=html --coverage.provider=istanbul; else vitest --run --coverage.enabled --coverage.provider=istanbul; fi",
"release": "yarn run build && np",
"prettier": "prettier --write --config .prettierrc.yaml {*.ts,*.json}"
},
Expand All @@ -51,12 +51,12 @@
},
"devDependencies": {
"@size-limit/preset-small-lib": "^8.0.0",
"@testing-library/react": "^13.3.0",
"@types/jest": "^28.1.6",
"@testing-library/react": "^14.0.0",
"@types/react": "^18.0.17",
"@types/react-dom": "^18.0.6",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"@vitest/coverage-istanbul": "^0.34.4",
"confusing-browser-globals": "^1.0.11",
"eslint": "^8.21.0",
"eslint-config-strictest": "^0.8.1",
Expand All @@ -65,17 +65,16 @@
"eslint-plugin-react": "^7.29.4",
"eslint-plugin-react-hooks": "^4.5.0",
"eslint-plugin-unicorn": "^43.0.2",
"jest": "^29.4.1",
"jest-environment-jsdom": "^29.4.1",
"jsdom": "^22.1.0",
"np": "^7.6.3",
"prettier": "^2.6.2",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-test-renderer": "^18.1.0",
"size-limit": "^8.0.0",
"superjson": "^1.9.1",
"ts-jest": "^28.0.7",
"typescript": "^5.1.6"
"typescript": "^5.1.6",
"vitest": "^0.34.4"
},
"size-limit": [
{
Expand Down
25 changes: 15 additions & 10 deletions test/browser.tsx β†’ test/browser.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import util from 'util'
/**
* @vitest-environment jsdom
*/

import util from 'node:util'
import superjson from 'superjson'
import { act, render, renderHook } from '@testing-library/react'
import React, { useEffect, useLayoutEffect, useMemo } from 'react'
import useLocalStorageState, { inMemoryData } from '../src/useLocalStorageState'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import useLocalStorageState, { inMemoryData } from '../src/useLocalStorageState.js'

beforeEach(() => {
// Throw an error when `console.error()` is called. This is especially useful in a React tests
Expand All @@ -19,7 +24,7 @@ beforeEach(() => {
// - "Warning: Cannot update a component (`Component`) while rendering a different component
// (`Component`). To locate the bad setState() call inside `Component`, follow the stack trace
// as described in https://reactjs.org/link/setstate-in-render"
jest.spyOn(console, 'error').mockImplementation((format: string, ...args: any[]) => {
vi.spyOn(console, 'error').mockImplementation((format: string, ...args: any[]) => {
throw new Error(util.format(format, ...args))
})
})
Expand Down Expand Up @@ -130,7 +135,7 @@ describe('useLocalStorageState()', () => {
})

test('handles errors thrown by localStorage', () => {
jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new Error()
})

Expand All @@ -150,7 +155,7 @@ describe('useLocalStorageState()', () => {
test('simulate blocking all the cookies in Safari', () => {
// in Safari, even just accessing `localStorage` throws "SecurityError: The operation is
// insecure."
jest.spyOn(window, 'localStorage', 'get').mockImplementation(() => {
vi.spyOn(window, 'localStorage', 'get').mockImplementation(() => {
throw new Error()
})

Expand Down Expand Up @@ -235,7 +240,7 @@ describe('useLocalStorageState()', () => {
})

test('returns the same update function when the value is saved', () => {
const functionMock = jest.fn()
const functionMock = vi.fn()
const { rerender } = renderHook(() => {
const [, setTodos] = useLocalStorageState('todos', {
defaultValue: ['first', 'second'],
Expand Down Expand Up @@ -550,7 +555,7 @@ describe('useLocalStorageState()', () => {

describe('in memory fallback', () => {
test('can retrieve data from in memory storage', () => {
jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new Error()
})

Expand Down Expand Up @@ -580,7 +585,7 @@ describe('useLocalStorageState()', () => {
})

test('isPersistent returns true when localStorage.setItem() throws an error but the value is the default value', () => {
jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new Error()
})

Expand All @@ -593,7 +598,7 @@ describe('useLocalStorageState()', () => {
})

test('isPersistent returns false when localStorage.setItem() throws an error', () => {
jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new Error()
})

Expand All @@ -616,7 +621,7 @@ describe('useLocalStorageState()', () => {
useLocalStorageState('todos', { defaultValue: ['first', 'second'] }),
)

jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new Error()
})

Expand Down
11 changes: 4 additions & 7 deletions test/server.tsx β†’ test/server.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
/**
* @jest-environment node
*/

import util from 'util'
import util from 'node:util'
import ReactDOM from 'react-dom/server'
import React, { MutableRefObject } from 'react'
import useLocalStorageState from '../src/useLocalStorageState'
import useLocalStorageState from '../src/useLocalStorageState.js'
import { beforeEach, describe, expect, test, vi } from 'vitest'

function renderHookOnServer<T>(useHook: () => T): { result: MutableRefObject<T> } {
const result: MutableRefObject<T> = {
Expand Down Expand Up @@ -37,7 +34,7 @@ beforeEach(() => {
// - "Warning: Cannot update a component (`Component`) while rendering a different component
// (`Component`). To locate the bad setState() call inside `Component`, follow the stack trace
// as described in https://reactjs.org/link/setstate-in-render"
jest.spyOn(console, 'error').mockImplementation((format: string, ...args: any[]) => {
vi.spyOn(console, 'error').mockImplementation((format: string, ...args: any[]) => {
throw new Error(util.format(format, ...args))
})
})
Expand Down
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"compilerOptions": {
"target": "ES6",
"module": "ES2015",
"module": "NodeNext",
"jsx": "react",
"esModuleInterop": true,
"moduleResolution": "Node16",
"declaration": true,
"moduleResolution": "nodenext",

// strict options ensuring more stable code
"strict": true,
Expand All @@ -16,7 +16,7 @@
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,

"types": ["node", "jest"],
"types": ["node"],

// - ℹ️ https://www.typescriptlang.org/tsconfig#isolatedModules
// - πŸ˜ƒ Deno requires "isolatedModules" to be set to `true`
Expand Down
9 changes: 9 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
restoreMocks: true,
// otherwise @testing-library/react can't cleanup after tests
globals: true,
},
})

0 comments on commit b5e0c56

Please sign in to comment.