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

Migrate type tests to Vitest #2129

Merged
merged 74 commits into from May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
50c72d5
Rename `connect-mapstate-mapdispatch.tsx` to `connect-mapstate-mapdis…
aryaemami59 Jan 29, 2024
beaaca5
Rename `connect-options-and-issues.tsx` to `connect-options-and-issue…
aryaemami59 Jan 29, 2024
3630e05
Rename `hooks.tsx` to `hooks.test-d.tsx`
aryaemami59 Jan 29, 2024
5e76256
Rename `provider.tsx` to `provider.test-d.tsx`
aryaemami59 Jan 29, 2024
0b2c461
Rename `react-redux-types.typetest.tsx` to `react-redux-types.test-d.…
aryaemami59 Jan 29, 2024
fc46a79
MIgrate type tests for `connect-options-and-issues.test-d.tsx` to Vitest
aryaemami59 Jan 29, 2024
8fe27c3
Fix minor issues in some test files
aryaemami59 Jan 29, 2024
e2777b7
Extend from the root `tsconfig.json`
aryaemami59 Jan 29, 2024
2c79718
Fix `exclude` in `tsconfig.test.json`
aryaemami59 Jan 29, 2024
2957c5f
Convert functions to `test` blocks in `connect-mapstate-mapdispatch.t…
aryaemami59 Jan 29, 2024
1fc684b
MIgrate type tests for `react-redux-types.test-d.tsx` to Vitest
aryaemami59 Feb 8, 2024
8e4e87d
MIgrate type tests for `hooks.test-d.tsx` to Vitest
aryaemami59 Feb 8, 2024
81c536e
Add `plugin:react/jsx-runtime` to ESLint extends
aryaemami59 Feb 8, 2024
4bf1c8e
Add `"jsx": "react-jsx"` to `tsconfig.test.json`
aryaemami59 Feb 8, 2024
31e1002
MIgrate type tests for `hooks.withTypes.test-d.tsx` to Vitest
aryaemami59 Feb 8, 2024
e813464
MIgrate type tests for `provider.test-d.tsx` to Vitest
aryaemami59 Feb 8, 2024
32a83c7
Fix some of type tests in `react-redux-types.test-d.tsx`
aryaemami59 Feb 8, 2024
a74b7b4
Remove `expectType` and `expectExactType`
aryaemami59 Feb 8, 2024
185903c
Bump typescript and vitest
aryaemami59 Feb 8, 2024
656d657
Remove wrong alias from `vitest.config.mts`
aryaemami59 Feb 8, 2024
0e138ca
Fix `CustomContext` in `connect-mapstate-mapdispatch.test-d.tsx`
aryaemami59 Feb 8, 2024
7db40df
Fix `React` imports in `connect-mapstate-mapdispatch.test-d.tsx`
aryaemami59 Feb 8, 2024
9b6df99
Fix `CustomContext` in `connect-options-and-issues.test-d.tsx`
aryaemami59 Feb 8, 2024
f379646
Merge branch 'master' of https://github.com/reduxjs/react-redux into …
aryaemami59 Feb 8, 2024
bcc5864
Merge branch 'master' of https://github.com/reduxjs/react-redux into …
aryaemami59 Feb 9, 2024
dd89f15
Fix `tsup.config.ts`
aryaemami59 Feb 9, 2024
e2484ef
Move `tsconfig.test.json` from the `test` folder to the root folder
aryaemami59 Feb 9, 2024
70481af
Revamp type tests config
aryaemami59 Feb 9, 2024
3b199ba
Fix `type-tests` command
aryaemami59 Feb 9, 2024
d887a9c
Provide the correct `tsconfig` to tsup
aryaemami59 Feb 9, 2024
656f731
Insert `any` assertion to silence TS errors
aryaemami59 Feb 9, 2024
325c8fd
Fix `tsconfig.test.json`
aryaemami59 Feb 9, 2024
68aa808
Fix ESLint issue
aryaemami59 Feb 9, 2024
8ead8e2
Remove `expectType`
aryaemami59 Feb 9, 2024
69d35c3
Fix titles of type tests
aryaemami59 Feb 9, 2024
3dbf659
Fix tsconfigs to match RTK's
aryaemami59 Feb 21, 2024
e7c4005
Add `tsconfig.vitest-temp.json` to `.gitignore` file
aryaemami59 Feb 21, 2024
f1bba5e
Remove `typecheck` field from `vitest.config.mts`
aryaemami59 Feb 21, 2024
8f52019
Run type checker alongside runtime tests
aryaemami59 Feb 21, 2024
e1a335d
Move test setup file into `test` folder
aryaemami59 Feb 21, 2024
bdeb473
Bump Vitest
aryaemami59 Feb 24, 2024
e62f478
Add path aliases and test against dist
aryaemami59 Feb 24, 2024
8e6add0
Trigger CI
aryaemami59 Feb 24, 2024
35dd78e
Remove Node ESM setup from `vitest.config.mts`
aryaemami59 Feb 24, 2024
047a071
Remove Node ESM setup from `tsup.config.ts`
aryaemami59 Feb 24, 2024
d337a31
Change CI Node version to 20
aryaemami59 Feb 24, 2024
ab44f2b
Change CI Node version to 20
aryaemami59 Feb 24, 2024
86448eb
Remove `import.meta.dirname` from `tsup.config.ts`
aryaemami59 Feb 24, 2024
1f8dcd7
Merge branch 'master' of https://github.com/reduxjs/react-redux into …
aryaemami59 Mar 8, 2024
d1380fa
Merge branch 'master' of https://github.com/reduxjs/react-redux into …
aryaemami59 Mar 10, 2024
42a6202
Fix lockfile
aryaemami59 Mar 10, 2024
6e4d4fd
Update GitHub Actions versions
aryaemami59 Mar 10, 2024
584ee8a
Merge branch 'master' of https://github.com/reduxjs/react-redux into …
aryaemami59 Mar 11, 2024
327d7cc
Bump `vitest` to latest version
aryaemami59 Mar 16, 2024
890aa40
Merge branch 'master' of https://github.com/reduxjs/react-redux into …
aryaemami59 Mar 21, 2024
d35255c
Merge branch 'master' of https://github.com/reduxjs/react-redux into …
aryaemami59 Mar 21, 2024
d69e108
Merge branch 'master' of https://github.com/reduxjs/react-redux into …
aryaemami59 Apr 7, 2024
8c073e2
Merge branch 'master' of https://github.com/reduxjs/react-redux into …
aryaemami59 Apr 9, 2024
ac8af6e
Merge branch 'master' of https://github.com/reduxjs/react-redux into …
aryaemami59 Apr 15, 2024
ae2e174
Merge branch 'master' of https://github.com/reduxjs/react-redux into …
aryaemami59 Apr 30, 2024
8a111a9
Merge branch 'master' of https://github.com/reduxjs/react-redux into …
aryaemami59 May 1, 2024
8f0824a
Merge branch 'migrate-typetests-to-vitest' of https://github.com/arya…
aryaemami59 May 2, 2024
3a453ac
Merge branch 'master' of https://github.com/reduxjs/react-redux into …
aryaemami59 May 2, 2024
6092181
Bump `vitest` to version 1.5.3
aryaemami59 May 2, 2024
6daba94
Merge branch 'master' of https://github.com/reduxjs/react-redux into …
aryaemami59 May 2, 2024
4d30e85
Bump TypeScript version to 5.4.5
aryaemami59 May 2, 2024
3fe18e7
Copy changes from #2168
aryaemami59 May 3, 2024
11e7a50
Bump `vitest` to version 1.6.0
aryaemami59 May 6, 2024
1e6a99e
Fix type issues in `tsup.config.ts`
aryaemami59 May 7, 2024
106f8fc
Remove `dist` folder before `build` command
aryaemami59 May 7, 2024
1836455
Run both runtime and type tests against the `dist` folder during CI
aryaemami59 May 7, 2024
1269bf4
Format files
aryaemami59 May 7, 2024
cf676e8
Distinguish between external and internal imports in tests
aryaemami59 May 7, 2024
04a8c0b
Remove some `// @ts-ignore` directives
aryaemami59 May 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Expand Up @@ -33,6 +33,7 @@
"eslint:recommended",
"plugin:import/recommended",
"plugin:react/recommended",
"plugin:react/jsx-runtime",
"plugin:@typescript-eslint/recommended"
],
"rules": {
Expand All @@ -56,7 +57,7 @@
},
{
"files": ["**/test/**/*.{ts,tsx}"],
"parserOptions": { "project": "./test/tsconfig.test.json" }
"parserOptions": { "project": true }
}
]
}
60 changes: 53 additions & 7 deletions .github/workflows/test.yml
@@ -1,15 +1,10 @@
name: Tests

on:
push:
branches: [master]
pull_request:
branches: [master]
workflow_dispatch:
on: [push, pull_request, workflow_dispatch]

jobs:
build:
name: Test Suite
name: Build and run test Suite
runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -63,12 +58,24 @@ jobs:
node-version: ${{ matrix.node }}
cache: 'yarn'

- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: package
path: .

- name: Install deps
run: yarn install

- name: Install TypeScript ${{ matrix.ts }}
run: yarn add typescript@${{ matrix.ts }}

- name: Install build artifact
run: yarn add ./package.tgz

- name: Erase path aliases
run: sed -i -e /@remap-prod-remove-line/d ./tsconfig.base.json ./vitest.config.mts

- name: Test types
run: |
yarn tsc --version
Expand Down Expand Up @@ -219,3 +226,42 @@ jobs:

- name: Build example
run: yarn build

test-dist:
name: Run local tests against build artifact
needs: [build]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node: ['20.x']
steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Use node ${{ matrix.node }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: 'yarn'

- name: Install deps
run: yarn install

- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: package
path: .

- name: Check folder contents
run: ls -lah

- name: Install build artifact
run: yarn add ./package.tgz

- name: Erase path aliases
run: sed -i -e /@remap-prod-remove-line/d ./tsconfig.base.json ./vitest.config.mts

- name: Run local tests against the build artifact
run: yarn test
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -32,3 +32,6 @@ website/translated_docs
website/build/
website/node_modules
website/i18n/*

tsconfig.vitest-temp.json

10 changes: 5 additions & 5 deletions package.json
Expand Up @@ -36,17 +36,17 @@
"dist"
],
"scripts": {
"build": "tsup",
"build": "yarn clean && tsup",
"clean": "rimraf lib dist es coverage",
"api-types": "api-extractor run --local",
"format": "prettier --write \"{src,test}/**/*.{js,ts,tsx}\" \"docs/**/*.md\"",
"lint": "eslint src test",
"lint:fix": "eslint src test --fix",
"prepare": "yarn clean && yarn build",
"pretest": "yarn lint",
"test": "vitest --run",
"test": "vitest --run --typecheck",
"test:watch": "vitest --watch",
"type-tests": "yarn tsc -p test/typetests/tsconfig.json",
"type-tests": "tsc --noEmit -p tsconfig.test.json",
"coverage": "codecov"
},
"peerDependencies": {
Expand Down Expand Up @@ -104,8 +104,8 @@
"redux": "^5.0.0",
"rimraf": "^3.0.2",
"tsup": "^7.0.0",
"typescript": "^5.4.2",
"vitest": "^1.2.1"
"typescript": "^5.4.5",
"vitest": "^1.6.0"
},
"packageManager": "yarn@4.1.0"
}
1 change: 0 additions & 1 deletion src/components/Provider.tsx
Expand Up @@ -91,7 +91,6 @@ function Provider<A extends Action<string> = UnknownAction, S = unknown>({

const Context = context || ReactReduxContext

// @ts-ignore 'AnyAction' is assignable to the constraint of type 'A', but 'A' could be instantiated with a different subtype
return <Context.Provider value={contextValue}>{children}</Context.Provider>
}

Expand Down
1 change: 0 additions & 1 deletion src/components/connect.tsx
Expand Up @@ -519,7 +519,6 @@ function connect<
WrappedComponent,
// @ts-ignore
initMapStateToProps,
// @ts-ignore
initMapDispatchToProps,
initMergeProps,
areStatesEqual,
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useSelector.ts
Expand Up @@ -142,7 +142,7 @@ export function createSelectorHook(
? useDefaultReduxContext
: createReduxContextHook(context)

const useSelector = <TState, Selected extends unknown>(
const useSelector = <TState, Selected>(
selector: (state: TState) => Selected,
equalityFnOrOptions:
| EqualityFn<NoInfer<Selected>>
Expand Down
14 changes: 7 additions & 7 deletions src/index-rsc.ts
Expand Up @@ -10,25 +10,25 @@ const _check2: typeof rsc = {} as typeof normal

// -------------------------------------------------------------------------------------

function throwNotSupportedError(
const throwNotSupportedError = ((
// eslint-disable-next-line @typescript-eslint/no-unused-vars
...args: any[]
): any {
): any => {
throw new Error(
'This function is not supported in React Server Components. Please only use this export in a Client Component.',
)
}
}) as any

export {
throwNotSupportedError as batch,
throwNotSupportedError as Provider,
throwNotSupportedError as batch,
throwNotSupportedError as connect,
throwNotSupportedError as useSelector,
throwNotSupportedError as useDispatch,
throwNotSupportedError as useStore,
throwNotSupportedError as createDispatchHook,
throwNotSupportedError as createSelectorHook,
throwNotSupportedError as createStoreHook,
throwNotSupportedError as useDispatch,
throwNotSupportedError as useSelector,
throwNotSupportedError as useStore,
}
export const ReactReduxContext = {} as any
export { default as shallowEqual } from './utils/shallowEqual'
3 changes: 1 addition & 2 deletions src/utils/react.ts
@@ -1,7 +1,6 @@
import * as ReactOriginal from 'react'
import type * as ReactNamespace from 'react'
import * as ReactOriginal from 'react'

export const React: typeof ReactNamespace =
// prettier-ignore
// @ts-ignore
'default' in ReactOriginal ? ReactOriginal['default'] : ReactOriginal as any
4 changes: 2 additions & 2 deletions test/components/Provider.spec.tsx
Expand Up @@ -3,10 +3,10 @@
import * as rtl from '@testing-library/react'
import type { Dispatch, JSX } from 'react'
import React, { Component } from 'react'
import type { ReactReduxContextValue } from 'react-redux'
import { Provider, ReactReduxContext, connect } from 'react-redux'
import type { Store } from 'redux'
import { createStore } from 'redux'
import type { ReactReduxContextValue } from '../../src'
import { Provider, ReactReduxContext, connect } from '../../src/index'

import * as ReactDOM from 'react-dom'

Expand Down
6 changes: 3 additions & 3 deletions test/components/connect.spec.tsx
@@ -1,8 +1,10 @@
/*eslint-disable react/prop-types*/

import * as rtl from '@testing-library/react'
import type { Dispatch, ElementType, MouseEvent, ReactNode, JSX } from 'react'
import type { Dispatch, ElementType, JSX, MouseEvent, ReactNode } from 'react'
import React, { Component } from 'react'
import type { ReactReduxContextValue } from 'react-redux'
import { Provider as ProviderMock, connect } from 'react-redux'
import type {
Action,
AnyAction,
Expand All @@ -12,8 +14,6 @@ import type {
UnknownAction,
} from 'redux'
import { applyMiddleware, createStore } from 'redux'
import type { ReactReduxContextValue } from '../../src/index'
import { Provider as ProviderMock, connect } from '../../src/index'

const IS_REACT_18 = React.version.startsWith('18')

Expand Down
2 changes: 1 addition & 1 deletion test/components/hooks.spec.tsx
Expand Up @@ -2,9 +2,9 @@

import * as rtl from '@testing-library/react'
import React from 'react'
import { Provider as ProviderMock, connect } from 'react-redux'
import type { AnyAction } from 'redux'
import { createStore } from 'redux'
import { Provider as ProviderMock, connect } from '../../src/index'

const IS_REACT_18 = React.version.startsWith('18')

Expand Down
2 changes: 1 addition & 1 deletion test/hooks/hooks.withTypes.test.tsx
@@ -1,6 +1,6 @@
import type { Action, ThunkAction } from '@reduxjs/toolkit'
import { configureStore, createAsyncThunk, createSlice } from '@reduxjs/toolkit'
import { useDispatch, useSelector, useStore } from '../../src'
import { useDispatch, useSelector, useStore } from 'react-redux'

export interface CounterState {
counter: number
Expand Down
10 changes: 5 additions & 5 deletions test/hooks/useDispatch.spec.tsx
@@ -1,12 +1,12 @@
import React from 'react'
import { createStore } from 'redux'
import { renderHook } from '@testing-library/react-hooks'
import React from 'react'
import type { ProviderProps, ReactReduxContextValue } from 'react-redux'
import {
createDispatchHook,
Provider as ProviderMock,
useDispatch,
createDispatchHook,
} from '../../src/index'
import type { ProviderProps, ReactReduxContextValue } from '../../src/'
} from 'react-redux'
import { createStore } from 'redux'

const store = createStore((c: number = 1): number => c + 1)
const store2 = createStore((c: number = 1): number => c + 2)
Expand Down
12 changes: 6 additions & 6 deletions test/hooks/useReduxContext.spec.tsx
@@ -1,10 +1,10 @@
import { renderHook } from '@testing-library/react-hooks'
import { createContext } from 'react'
import type { ReactReduxContextValue } from '../../src/components/Context'
import {
createReduxContextHook,
useReduxContext,
} from '../../src/hooks/useReduxContext'
} from '@internal/hooks/useReduxContext'
import { renderHook } from '@testing-library/react-hooks'
import { createContext } from 'react'
import type { ReactReduxContextValue } from 'react-redux'

describe('React', () => {
describe('hooks', () => {
Expand All @@ -14,7 +14,7 @@ describe('React', () => {

const { result } = renderHook(() => useReduxContext())

expect(result.error.message).toMatch(
expect(result.error?.message).toMatch(
/could not find react-redux context value/,
)

Expand All @@ -29,7 +29,7 @@ describe('React', () => {

const { result } = renderHook(() => useCustomReduxContext())

expect(result.error.message).toMatch(
expect(result.error?.message).toMatch(
/could not find react-redux context value/,
)

Expand Down
10 changes: 5 additions & 5 deletions test/hooks/useSelector.spec.tsx
@@ -1,5 +1,6 @@
/*eslint-disable react/prop-types*/

import type { UseSelectorOptions } from '@internal/hooks/useSelector'
import * as rtl from '@testing-library/react'
import type { DispatchWithoutAction, FunctionComponent, ReactNode } from 'react'
import React, {
Expand All @@ -11,15 +12,12 @@ import React, {
useReducer,
useState,
} from 'react'
import type { Action, AnyAction, Store } from 'redux'
import { createStore } from 'redux'
import type { UseSelectorOptions } from '../../src/hooks/useSelector'
import type {
ProviderProps,
ReactReduxContextValue,
Subscription,
TypedUseSelectorHook,
} from '../../src/index'
} from 'react-redux'
import {
Provider,
ReactReduxContext,
Expand All @@ -28,7 +26,9 @@ import {
shallowEqual,
useDispatch,
useSelector,
} from '../../src/index'
} from 'react-redux'
import type { Action, AnyAction, Store } from 'redux'
import { createStore } from 'redux'

// disable checks by default
function ProviderMock<A extends Action<any> = AnyAction, S = unknown>({
Expand Down
13 changes: 4 additions & 9 deletions test/integration/dynamic-reducers.spec.tsx
Expand Up @@ -4,10 +4,10 @@ import * as rtl from '@testing-library/react'
import type { ReactNode } from 'react'
import React from 'react'
import ReactDOMServer from 'react-dom/server'
import type { ReactReduxContextValue } from 'react-redux'
import { Provider, ReactReduxContext, connect } from 'react-redux'
import type { Store } from 'redux'
import { combineReducers, createStore } from 'redux'
import type { ReactReduxContextValue } from '../../src/index'
import { Provider, ReactReduxContext, connect } from '../../src/index'

describe('React', () => {
/*
Expand Down Expand Up @@ -180,10 +180,7 @@ describe('React', () => {
// This generates errors for using useLayoutEffect in v7
// We hide that error by disabling console.error here

vi.spyOn(console, 'error')
// eslint-disable-next-line no-console
// @ts-ignore
console.error.mockImplementation(() => {})
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})

const markup = ReactDOMServer.renderToString(
<Provider store={store}>
Expand All @@ -199,9 +196,7 @@ describe('React', () => {
expect(markup).toContain('Hello world')
expect(markup).toContain('Hello dynamic world')

// eslint-disable-next-line no-console
// @ts-ignore
console.error.mockRestore()
consoleErrorSpy.mockRestore()
})
})
})
2 changes: 1 addition & 1 deletion test/integration/server-rendering.spec.tsx
Expand Up @@ -12,9 +12,9 @@ import type { PayloadAction } from '@reduxjs/toolkit'
import type { FunctionComponent } from 'react'
import React from 'react'
import { renderToString } from 'react-dom/server'
import { Provider, connect } from 'react-redux'
import type { Dispatch, Store } from 'redux'
import { createStore } from 'redux'
import { Provider, connect } from '../../src/index'

describe('React', () => {
describe('server rendering', () => {
Expand Down