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 #4127

Merged
merged 79 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
5994f5f
Rename `src/dynamicMiddleware/tests/index.typetest.ts` to `src/dynami…
aryaemami59 Jan 26, 2024
799b20b
Migrate type tests for `src\dynamicMiddleware\tests\index.test-d.ts` …
aryaemami59 Jan 26, 2024
acb46b5
Rename `src/dynamicMiddleware/tests/react.typetest.ts` to `src/dynami…
aryaemami59 Jan 26, 2024
70af524
Migrate type tests for `src\dynamicMiddleware\tests\react.test-d.ts` …
aryaemami59 Jan 26, 2024
ad884ed
Rename `src/query/tests/baseQueryTypes.typetest.ts` to `src/query/tes…
aryaemami59 Jan 26, 2024
b8f6f81
Migrate type tests for `src\query\tests\baseQueryTypes.test-d.ts` to …
aryaemami59 Jan 26, 2024
9928268
Rename `src/tests/combineSlices.typetest.ts` to `src/tests/combineSli…
aryaemami59 Jan 26, 2024
e3a6d20
Migrate type tests for `src/tests/combineSlices.test-d.ts` to Vitest
aryaemami59 Jan 26, 2024
505ee08
Rename `src/tests/configureStore.typetest.ts` to `src/tests/configure…
aryaemami59 Jan 26, 2024
6fbf1bd
Migrate type tests for `src/tests/configureStore.test-d.ts` to Vitest
aryaemami59 Jan 26, 2024
6d577ad
Rename `src/tests/createAsyncThunk.typetest.ts` to `src/tests/createA…
aryaemami59 Jan 26, 2024
293c669
Migrate type tests for `src/tests/createAsyncThunk.test-d.ts` to Vitest
aryaemami59 Jan 27, 2024
bf09e94
Rename `src/tests/createEntityAdapter.typetest.ts` to `src/tests/crea…
aryaemami59 Jan 27, 2024
7a7fc95
Migrate type tests for `src/tests/createEntityAdapter.test-d.ts` to V…
aryaemami59 Jan 27, 2024
1ac69c1
Rename `src/tests/createReducer.typetest.ts` to `src/tests/createRedu…
aryaemami59 Jan 27, 2024
6d309df
Migrate type tests for `src/tests/createReducer.test-d.ts` to Vitest
aryaemami59 Jan 27, 2024
c3c7dbf
Rename `src/tests/createSlice.typetest.ts` to `src/tests/createSlice.…
aryaemami59 Jan 27, 2024
cfa8466
Migrate type tests for `src/tests/createSlice.test-d.ts` to Vitest
aryaemami59 Jan 27, 2024
1cdfa79
Rename `src/tests/getDefaultEnhancers.typetest.ts` to `src/tests/getD…
aryaemami59 Jan 27, 2024
0bd705c
Migrate type tests for `src/tests/getDefaultEnhancers.test-d.ts` to V…
aryaemami59 Jan 27, 2024
10d3add
Rename `src/tests/getDefaultMiddleware.typetest.ts` to `src/tests/get…
aryaemami59 Jan 27, 2024
7d7e219
Migrate type tests for `src/tests/getDefaultMiddleware.test-d.ts` to …
aryaemami59 Jan 27, 2024
1b6923b
Rename `src/tests/mapBuilders.typetest.ts` to `src/tests/mapBuilders.…
aryaemami59 Jan 27, 2024
4ba50e2
Migrate type tests for `src/tests/mapBuilders.test-d.ts` to Vitest
aryaemami59 Jan 27, 2024
7ffb614
Rename `src/tests/matchers.typetest.ts` to `src/tests/matchers.test-d…
aryaemami59 Jan 27, 2024
efea226
Migrate type tests for `src/tests/matchers.test-d.ts` to Vitest
aryaemami59 Jan 27, 2024
bb9aecf
Rename `src/tests/Tuple.typetest.ts` to `src/tests/Tuple.test-d.ts`
aryaemami59 Jan 27, 2024
d109c3c
Add `@ts-ignore` to `createAsyncThunk.test-d.ts` TS 4.7 issues
aryaemami59 Jan 27, 2024
008230b
Migrate type tests for `src/tests/Tuple.test-d.ts` to Vitest
aryaemami59 Jan 27, 2024
1b7a714
Migrate type tests for `src/query/tests/buildHooks.test-d.tsx` to Vitest
aryaemami59 Jan 27, 2024
3f0f097
Move type tests inside `listenerMiddleware.test.ts` to `listenerMiddl…
aryaemami59 Jan 28, 2024
6b02a91
Fix type tests inside `mapBuilders.test-d.ts`
aryaemami59 Jan 28, 2024
94814b6
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Jan 28, 2024
59f451d
Move the type tests inside `createApi.test.ts` into `createApi.test-d…
aryaemami59 Jan 28, 2024
50df02d
Move the type tests inside `cacheLifecycle.test.ts` into `cacheLifecy…
aryaemami59 Jan 28, 2024
9f7cf97
Move the type tests inside `errorHandling.test.tsx` into `errorHandli…
aryaemami59 Jan 28, 2024
f7dbd7e
Move the type tests inside `getDefaultMiddleware.test.ts` into `getDe…
aryaemami59 Jan 28, 2024
8025f61
Move the type tests inside `createAsyncThunk.test.ts` into `createAsy…
aryaemami59 Jan 28, 2024
56c3bc5
Move the type tests inside `matchers.test.tsx` into `matchers.test-d.…
aryaemami59 Jan 28, 2024
1a98378
Move type tests inside `queryLifecycle.test.tsx` into `queryLifecycle…
aryaemami59 Jan 28, 2024
5e2a47e
Move type tests from `combineSlices.test.ts` into `combineSlices.test…
aryaemami59 Jan 28, 2024
6b533d6
Rename `src/tests/createAction.typetest.tsx` to `src/tests/createActi…
aryaemami59 Jan 28, 2024
af3511d
Migrate type tests for `createAction.test-d.tsx` to Vitest
aryaemami59 Jan 28, 2024
672cc5e
Remove `typeTestHelpers.ts`
aryaemami59 Jan 28, 2024
209d0d9
Cut excessive assertions in `Tuple.test-d.ts`
aryaemami59 Jan 28, 2024
c2abc20
Cut excessive assertions in `combineSlices.test-d.ts`
aryaemami59 Jan 28, 2024
9e0cb1a
Cut excessive assertions in `configureStore.test-d.ts`
aryaemami59 Jan 28, 2024
d5155bd
Cut excessive assertions in `mapBuilders.test-d.ts`
aryaemami59 Jan 28, 2024
4042506
Cut excessive assertions in `buildHooks.test-d.tsx`
aryaemami59 Jan 28, 2024
0724a36
Cut excessive assertions in `createSlice.test-d.ts`
aryaemami59 Jan 28, 2024
9669753
Cut excessive assertions in `createEntityAdapter.test-d.ts`
aryaemami59 Jan 28, 2024
ba94b0a
Cut excessive assertions in `createAsyncThunk.test-d.ts`
aryaemami59 Jan 28, 2024
2a70d74
Cut excessive assertions in `unionTypes.test-d.ts`
aryaemami59 Jan 28, 2024
bf6bd48
Cut excessive assertions in `configureStore.test-d.ts`
aryaemami59 Jan 28, 2024
18a6b2a
Cut excessive assertions in `createAction.test-d.tsx`
aryaemami59 Jan 28, 2024
7713856
Fix issue with importing `endpointDefinitions` inside `createApi.test…
aryaemami59 Jan 28, 2024
f6d812c
Remove nested `describe` block from `createAction.test-d.tsx`
aryaemami59 Jan 28, 2024
c76527d
Add missing type import in `retry.test-d.ts`
aryaemami59 Jan 28, 2024
2518b10
Fix remaining type test issues
aryaemami59 Jan 28, 2024
50456ba
Fix `endpointDefinitions` type imports in `createApi.test.ts`
aryaemami59 Jan 28, 2024
407bcf5
Fix issue with importing `Tuple` in `getDefaultMiddleware.test.ts`
aryaemami59 Jan 28, 2024
ebb0637
Fix type issue in `getDefaultMiddleware.test-d.ts`
aryaemami59 Jan 28, 2024
b1071f6
remove (now inaccurate) TODOs
EskiMojo14 Jan 28, 2024
819e241
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Jan 29, 2024
c231da4
Remove redundant `returns.resolves` assertion
aryaemami59 Jan 29, 2024
9339d5c
Remove unnecessary `.then` blocks
aryaemami59 Jan 29, 2024
50e9640
Remove unnecessary `if` blocks
aryaemami59 Jan 29, 2024
a6a7504
Fix broken assertion
aryaemami59 Jan 29, 2024
8577395
Remove unnecessary `test` block
aryaemami59 Jan 29, 2024
d98757f
Remove IIFE's
aryaemami59 Jan 29, 2024
8bba2ee
Replace `parameters.items` with `parameter(0)`
aryaemami59 Jan 29, 2024
8b331ba
Fix type annotation
aryaemami59 Jan 29, 2024
38eff96
Replace `@ts-expect-error`s with `expectTypeOf` assertions
aryaemami59 Jan 29, 2024
a5781ee
Remove redundant assertion
aryaemami59 Jan 29, 2024
3c2afe0
Convert some expressions and statements to type test assertions in `…
aryaemami59 Jan 29, 2024
5ecd984
Add `tsconfig.vitest-temp.json` to `.gitignore`
aryaemami59 Jan 29, 2024
5a43739
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Jan 29, 2024
3418c5a
Fix `OverrideResultType` imports in test files
aryaemami59 Jan 29, 2024
77511c3
Replace `dist` imports with `@internal` imports
aryaemami59 Jan 29, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ typesversions
!.yarn/versions
.pnp.*
*.tgz

tsconfig.vitest-temp.json
95 changes: 95 additions & 0 deletions packages/toolkit/src/dynamicMiddleware/tests/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import type { Action, Middleware, UnknownAction } from 'redux'
import type { ThunkDispatch } from 'redux-thunk'
import { configureStore } from '../../configureStore'
import { createDynamicMiddleware } from '../index'

const untypedInstance = createDynamicMiddleware()

interface AppDispatch extends ThunkDispatch<number, undefined, UnknownAction> {
(n: 1): 1
}

const typedInstance = createDynamicMiddleware<number, AppDispatch>()

declare const staticMiddleware: Middleware<(n: 1) => 1>

const store = configureStore({
reducer: () => 0,
middleware: (gDM) =>
gDM().prepend(typedInstance.middleware).concat(staticMiddleware),
})

declare const compatibleMiddleware: Middleware<{}, number, AppDispatch>
declare const incompatibleMiddleware: Middleware<{}, string, AppDispatch>

declare const addedMiddleware: Middleware<(n: 2) => 2>

describe('type tests', () => {
test('instance typed at creation ensures middleware compatibility with store', () => {
const store = configureStore({
reducer: () => '',
// @ts-expect-error
middleware: (gDM) => gDM().prepend(typedInstance.middleware),
})
})

test('instance typed at creation enforces correct middleware type', () => {
typedInstance.addMiddleware(
compatibleMiddleware,
// @ts-expect-error
incompatibleMiddleware
)

const dispatch = store.dispatch(
typedInstance.withMiddleware(
compatibleMiddleware,
// @ts-expect-error
incompatibleMiddleware
)
)
})

test('withTypes() enforces correct middleware type', () => {
const addMiddleware = untypedInstance.addMiddleware.withTypes<{
state: number
dispatch: AppDispatch
}>()

addMiddleware(
compatibleMiddleware,
// @ts-expect-error
incompatibleMiddleware
)

const withMiddleware = untypedInstance.withMiddleware.withTypes<{
state: number
dispatch: AppDispatch
}>()

const dispatch = store.dispatch(
withMiddleware(
compatibleMiddleware,
// @ts-expect-error
incompatibleMiddleware
)
)
})

test('withMiddleware returns typed dispatch, with any applicable extensions', () => {
const dispatch = store.dispatch(
typedInstance.withMiddleware(addedMiddleware)
)

// standard
expectTypeOf(dispatch({ type: 'foo' })).toEqualTypeOf<Action<string>>()

// thunk
expectTypeOf(dispatch(() => 'foo')).toBeString()

// static
expectTypeOf(dispatch(1)).toEqualTypeOf<1>()

// added
expectTypeOf(dispatch(2)).toEqualTypeOf<2>()
})
})
102 changes: 0 additions & 102 deletions packages/toolkit/src/dynamicMiddleware/tests/index.typetest.ts

This file was deleted.

81 changes: 81 additions & 0 deletions packages/toolkit/src/dynamicMiddleware/tests/react.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import type { Context } from 'react'
import type { ReactReduxContextValue } from 'react-redux'
import type { Action, Middleware, UnknownAction } from 'redux'
import type { ThunkDispatch } from 'redux-thunk'
import { createDynamicMiddleware } from '../react'

interface AppDispatch extends ThunkDispatch<number, undefined, UnknownAction> {
(n: 1): 1
}

const untypedInstance = createDynamicMiddleware()

const typedInstance = createDynamicMiddleware<number, AppDispatch>()

declare const compatibleMiddleware: Middleware<{}, number, AppDispatch>
declare const incompatibleMiddleware: Middleware<{}, string, AppDispatch>

declare const customContext: Context<ReactReduxContextValue>

declare const addedMiddleware: Middleware<(n: 2) => 2>

describe('type tests', () => {
test('instance typed at creation enforces correct middleware type', () => {
const useDispatch = typedInstance.createDispatchWithMiddlewareHook(
compatibleMiddleware,
// @ts-expect-error
incompatibleMiddleware
)

const createDispatchWithMiddlewareHook =
typedInstance.createDispatchWithMiddlewareHookFactory(customContext)
const useDispatchWithContext = createDispatchWithMiddlewareHook(
compatibleMiddleware,
// @ts-expect-error
incompatibleMiddleware
)
})

test('withTypes() enforces correct middleware type', () => {
const createDispatchWithMiddlewareHook =
untypedInstance.createDispatchWithMiddlewareHook.withTypes<{
state: number
dispatch: AppDispatch
}>()
const useDispatch = createDispatchWithMiddlewareHook(
compatibleMiddleware,
// @ts-expect-error
incompatibleMiddleware
)

const createCustomDispatchWithMiddlewareHook = untypedInstance
.createDispatchWithMiddlewareHookFactory(customContext)
.withTypes<{
state: number
dispatch: AppDispatch
}>()
const useCustomDispatch = createCustomDispatchWithMiddlewareHook(
compatibleMiddleware,
// @ts-expect-error
incompatibleMiddleware
)
})

test('useDispatchWithMW returns typed dispatch, with any applicable extensions', () => {
const useDispatchWithMW =
typedInstance.createDispatchWithMiddlewareHook(addedMiddleware)
const dispatch = useDispatchWithMW()

// standard
expectTypeOf(dispatch({ type: 'foo' })).toEqualTypeOf<Action<string>>()

// thunk
expectTypeOf(dispatch(() => 'foo')).toBeString()

// static
expectTypeOf(dispatch(1)).toEqualTypeOf<1>()

// added
expectTypeOf(dispatch(2)).toEqualTypeOf<2>()
})
})
89 changes: 0 additions & 89 deletions packages/toolkit/src/dynamicMiddleware/tests/react.typetest.ts

This file was deleted.