Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(tests): migrate to vitest #1753

Merged
merged 9 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"prettier",
"react-hooks",
"import",
"jest"
"vitest"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
Expand Down Expand Up @@ -48,10 +48,12 @@
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-explicit-any": "off",
"jest/consistent-test-it": [
"vitest/consistent-test-it": [
"error",
{ "fn": "it", "withinDescribe": "it" }
],
"import/namespace": "off",
"import/named": "off",
"import/order": [
"error",
{
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/test-multiple-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,13 @@ jobs:
if: ${{ matrix.build == 'umd' && matrix.env == 'development' }}
run: |
sed -i~ "s/<rootDir>\/src\(.*\)\.ts/<rootDir>\/dist\/umd\1.development.js/" package.json
sed -i~ 's/"test:ci":.*,$/"test:ci": "jest",/' package.json
sed -i~ 's/= await import(/= require(/' tests/devtools.test.tsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should remove L80 too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! Done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize that one.
I meant this: sed -i~ 's/= await import(/= require(/' tests/devtools.test.tsx.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! I'm surprised that it did not cause the tests to fail this time 🤔 Fixed it in this commit

sed -i~ "s/it('\[${DEVTOOLS_SKIP}\]/it.skip('/" tests/devtools.test.tsx
env:
DEVTOOLS_SKIP: ${{ matrix.devtools-skip }}
- name: Patch for UMD (PRD)
if: ${{ matrix.build == 'umd' && matrix.env == 'production' }}
run: |
sed -i~ "s/<rootDir>\/src\(.*\)\.ts/<rootDir>\/dist\/umd\1.production.js/" package.json
sed -i~ 's/"test:ci":.*,$/"test:ci": "jest",/' package.json
sed -i~ 's/= await import(/= require(/' tests/devtools.test.tsx
sed -i~ "s/it('\[${DEVTOOLS_SKIP}\]/it.skip('/" tests/devtools.test.tsx
env:
DEVTOOLS_SKIP: ${{ matrix.devtools-skip }}
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/test-multiple-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ jobs:
sed -i~ '1s/^/import React from "react";/' tests/*.tsx
sed -i~ 's/"jsx": "react-jsx"/"jsx": "react"/' tsconfig.json
sed -i~ 's/import\.meta\.env[?]\.MODE/"DEVELOPMENT".toLowerCase()/' src/*.ts src/*/*.ts
sed -i~ 's/"test:ci":.*,$/"test:ci": "jest",/' package.json
sed -i~ 's/= await import(/= require(/' tests/devtools.test.tsx
sed -i~ "s/it('\[${DEVTOOLS_SKIP}\]/it.skip('/" tests/devtools.test.tsx
env:
DEVTOOLS_SKIP: ${{ matrix.devtools-skip }}
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/test-old-typescript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,4 @@ jobs:
- name: Test ${{ matrix.typescript }}
run: |
yarn add -D typescript@${{ matrix.typescript }}
rm -r node_modules/parse5
yarn tsc --noEmit
56 changes: 9 additions & 47 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,10 @@
"eslint": "eslint --no-eslintrc --c .eslintrc.json --fix '*.{js,json}' '{src,tests}/**/*.{ts,tsx}'",
"eslint:ci": "eslint --no-eslintrc --c .eslintrc.json '*.{js,json}' '{src,tests}/**/*.{ts,tsx}'",
"pretest": "tsc --noEmit",
"test": "yarn node --experimental-vm-modules $(yarn bin jest)",
"test:ci": "yarn node --experimental-vm-modules $(yarn bin jest)",
"test:dev": "yarn node --experimental-vm-modules $(yarn bin jest) --watch --no-coverage",
"test:coverage:watch": "yarn node --experimental-vm-modules $(yarn bin jest) --watch",
"test": "vitest --ui --coverage",
"test:ci": "vitest",
"patch-d-ts": "node -e \"var {entries}=require('./rollup.config.js');require('shelljs').find('dist/**/*.d.ts').forEach(f=>{entries.forEach(({find,replacement})=>require('shelljs').sed('-i',new RegExp(' from \\''+find.source.slice(0,-1)+'\\';$'),' from \\''+replacement+'\\';',f));require('shelljs').sed('-i',/ from '(\\.[^']+)\\.ts';$/,' from \\'\\$1\\';',f)})\"",
"copy": "shx cp -r dist/src/* dist/esm && shx cp -r dist/src/* dist && shx rm -rf dist/src && shx rm -rf dist/{src,tests} && downlevel-dts dist dist/ts3.4 && shx cp package.json readme.md LICENSE dist && json -I -f dist/package.json -e \"this.private=false; this.devDependencies=undefined; this.optionalDependencies=undefined; this.scripts=undefined; this.prettier=undefined; this.jest=undefined;\"",
"copy": "shx cp -r dist/src/* dist/esm && shx cp -r dist/src/* dist && shx rm -rf dist/src && shx rm -rf dist/{src,tests} && downlevel-dts dist dist/ts3.4 && shx cp package.json readme.md LICENSE dist && json -I -f dist/package.json -e \"this.private=false; this.devDependencies=undefined; this.optionalDependencies=undefined; this.scripts=undefined; this.prettier=undefined;\"",
"patch-esm-ts": "node -e \"require('shelljs').find('dist/esm/**/*.d.ts').forEach(f=>{var f2=f.replace(/\\.ts$/,'.mts');require('fs').copyFileSync(f,f2);require('shelljs').sed('-i',/ from '(\\.[^']+)';$/,' from \\'\\$1.mjs\\';',f2);require('shelljs').sed('-i',/^declare module '(\\.[^']+)'/,'declare module \\'\\$1.mjs\\'',f2)})\""
},
"engines": {
Expand Down Expand Up @@ -132,44 +130,9 @@
"url": "https://github.com/pmndrs/zustand/issues"
},
"homepage": "https://github.com/pmndrs/zustand",
"jest": {
"rootDir": ".",
"testEnvironment": "jsdom",
"preset": "ts-jest/presets/default-esm",
"transform": {
"^.+\\.(t|j)sx?$": [
"ts-jest",
{
"useESM": true
}
]
},
"extensionsToTreatAsEsm": [
".ts",
".tsx"
],
"moduleNameMapper": {
"^zustand$": "<rootDir>/src/index.ts",
"^zustand/(.*)$": "<rootDir>/src/$1.ts"
},
"modulePathIgnorePatterns": [
"dist"
],
"testRegex": "test.(js|ts|tsx)$",
"coverageDirectory": "./coverage/",
"collectCoverage": true,
"coverageReporters": [
"json",
"html",
"text",
"text-summary"
],
"collectCoverageFrom": [
"src/**/*.{js,ts,tsx}",
"tests/**/*.{js,ts,tsx}"
]
},
"dependencies": {
"@vitest/ui": "^0.30.1",
"eslint-plugin-vitest": "^0.1.4",
Comment on lines +134 to +135
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't notice when reviewing, but these shouldn't be in dependencies.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"use-sync-external-store": "1.2.0"
},
"devDependencies": {
Expand All @@ -192,20 +155,19 @@
"@types/use-sync-external-store": "^0.0.3",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.57.0",
"@vitest/coverage-c8": "^0.30.1",
"concurrently": "^8.0.1",
"downlevel-dts": "^0.11.0",
"esbuild": "^0.17.14",
"eslint": "^8.37.0",
"eslint-config-prettier": "^8.8.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"immer": "^9.0.21",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"jsdom": "^21.1.1",
"json": "^11.0.0",
"prettier": "^2.8.7",
"react": "^18.2.0",
Expand All @@ -214,8 +176,8 @@
"rollup": "^3.20.2",
"rollup-plugin-esbuild": "^5.0.0",
"shx": "^0.3.4",
"ts-jest": "^29.0.5",
"typescript": "^5.0.3"
"typescript": "^5.0.3",
"vitest": "^0.30.1"
},
"peerDependencies": {
"immer": ">=9.0",
Expand Down
8 changes: 4 additions & 4 deletions tests/basic.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
useLayoutEffect,
useState,
} from 'react'
import { afterEach, expect, it, jest } from '@jest/globals'
import { act, fireEvent, render } from '@testing-library/react'
import ReactDOM from 'react-dom'
import { afterEach, expect, it, vi } from 'vitest'
import { create } from 'zustand'
import type { StoreApi } from 'zustand'

Expand Down Expand Up @@ -306,7 +306,7 @@ it('can call useBoundStore with progressively more arguments', async () => {
})

it('can throw an error in selector', async () => {
console.error = jest.fn()
console.error = vi.fn()
type State = { value: string | number }

const initialState: State = { value: 'foo' }
Expand Down Expand Up @@ -353,7 +353,7 @@ it('can throw an error in selector', async () => {
})

it('can throw an error in equality checker', async () => {
console.error = jest.fn()
console.error = vi.fn()
type State = { value: string | number }

const initialState: State = { value: 'foo' }
Expand Down Expand Up @@ -442,7 +442,7 @@ it('can set the store', () => {
it('both NaN should not update', () => {
const { setState, subscribe } = create<number>(() => NaN)

const fn = jest.fn()
const fn = vi.fn()
subscribe(fn)

setState(NaN)
Expand Down
4 changes: 2 additions & 2 deletions tests/context.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
useEffect,
useState,
} from 'react'
import { afterEach, it, jest } from '@jest/globals'
import { render } from '@testing-library/react'
import { afterEach, it, vi } from 'vitest'
import { create } from 'zustand'
import type { StoreApi } from 'zustand'
import createContext from 'zustand/context'
Expand Down Expand Up @@ -123,7 +123,7 @@ it('uses context store api', async () => {
})

it('throws error when not using provider', async () => {
console.error = jest.fn()
console.error = vi.fn()

class ErrorBoundary extends ClassComponent<
{ children?: ReactNode | undefined },
Expand Down
Loading