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

Remove console-testing-library #4603

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e034d5b
Fix `console` spy related issues in `devWarnings.test.tsx`
aryaemami59 May 14, 2024
858047d
Fix `console` spy related issues in `createApi.test.ts`
aryaemami59 May 14, 2024
db2380a
Fix `console` spy related issues in `injectEndpoints.test.tsx`
aryaemami59 May 14, 2024
bfaa88d
Fix issues related to `console` spies in `createSlice.test.ts`
aryaemami59 May 16, 2024
df18c84
Fix issues related to `console` spies in `serializableStateInvariantM…
aryaemami59 May 16, 2024
dd325c6
Fix issues related to `console` spies in `immutableStateInvariantMidd…
aryaemami59 May 16, 2024
c14f407
Fix `console` spy related issues in `fakeBaseQuery.test.tsx`
aryaemami59 Aug 1, 2024
fe04ad6
Fix issues related to `console` spies in `createAsyncThunk.test.ts`
aryaemami59 May 16, 2024
6729bda
Fix issues related to `console` spies in `createReducer.test.ts`
aryaemami59 May 16, 2024
3818755
Fix `console` spy related issues in `queryFn.test.tsx`
aryaemami59 Aug 1, 2024
90c2d01
Remove `vi.resetAllMocks()` call from `utils.spec.ts`
aryaemami59 May 16, 2024
15a0718
Fix `console` spy related issues in `effectScenarios.test.ts`
aryaemami59 May 14, 2024
ef44b7f
Fix `console` spy related issues in `listenerMiddleware.test.ts`
aryaemami59 May 14, 2024
692a529
Fix issue with `console` spy inside `buildHooks.test.tsx`
aryaemami59 May 14, 2024
36e5e74
Fix issues related to spies and stubbing environments in `utils.spec.ts`
aryaemami59 May 14, 2024
ff7a3b9
Fix issues related to stubbing envs in `createReducer.test.ts`
aryaemami59 May 16, 2024
2a738d9
Fix issues related to stubbing envs in `createSlice.test.ts`
aryaemami59 May 16, 2024
0ad3729
Fix issues related to stubbing envs in `createAsyncThunk.test.ts`
aryaemami59 May 16, 2024
e773192
Fix issues related to stubbing envs in `getDefaultMiddleware.test.ts`
aryaemami59 Sep 4, 2024
da9ae2f
Remove `satisfies` operators in `queryFn.test.tsx`
aryaemami59 Aug 1, 2024
bd04abd
Remove `console-testing-library` as it is no longer needed
aryaemami59 Aug 2, 2024
bb729ff
Remove `jest-snapshot` from `resolutions` field of root `package.json`
aryaemami59 Sep 4, 2024
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
68 changes: 0 additions & 68 deletions .yarn/patches/console-testing-library-npm-0.6.1-4d9957d402.patch

This file was deleted.

26 changes: 0 additions & 26 deletions .yarn/patches/console-testing-library__npm_0.3.1.patch

This file was deleted.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
},
"resolutions": {
"jest-snapshot": "29.3.1"
},
"scripts": {
"build": "yarn build:packages",
"test": "yarn test:packages",
Expand Down
1 change: 0 additions & 1 deletion packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"@typescript-eslint/eslint-plugin": "^6",
"@typescript-eslint/parser": "^6",
"axios": "^0.19.2",
"console-testing-library": "patch:console-testing-library@npm%3A0.6.1#~/.yarn/patches/console-testing-library-npm-0.6.1-4d9957d402.patch",
"esbuild": "^0.23.0",
"esbuild-extra": "^0.4.0",
"eslint": "^7.25.0",
Expand Down
39 changes: 22 additions & 17 deletions packages/toolkit/src/entities/tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,71 @@
import { vi } from 'vitest'
import { noop } from '@internal/listenerMiddleware/utils'
import { AClockworkOrange } from './fixtures/book'

describe('Entity utils', () => {
describe(`selectIdValue()`, () => {
const OLD_ENV = process.env
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(noop)

beforeEach(() => {
vi.resetModules() // this is important - it clears the cache
process.env = { ...OLD_ENV, NODE_ENV: 'development' }
vi.stubEnv('NODE_ENV', 'development')
})

afterEach(() => {
process.env = OLD_ENV
vi.resetAllMocks()
consoleWarnSpy.mockClear()
vi.unstubAllEnvs()
})

afterAll(() => {
consoleWarnSpy.mockRestore()
})

it('should not warn when key does exist', async () => {
const { selectIdValue } = await import('../utils')
const spy = vi.spyOn(console, 'warn')

selectIdValue(AClockworkOrange, (book: any) => book.id)
expect(spy).not.toHaveBeenCalled()
expect(consoleWarnSpy).not.toHaveBeenCalled()
})

it('should warn when key does not exist in dev mode', async () => {
const { selectIdValue } = await import('../utils')
const spy = vi.spyOn(console, 'warn')

expect(process.env.NODE_ENV).toBe('development')

selectIdValue(AClockworkOrange, (book: any) => book.foo)

expect(spy).toHaveBeenCalled()
expect(consoleWarnSpy).toHaveBeenCalledOnce()
})

it('should warn when key is undefined in dev mode', async () => {
const { selectIdValue } = await import('../utils')
const spy = vi.spyOn(console, 'warn')

expect(process.env.NODE_ENV).toBe('development')

const undefinedAClockworkOrange = { ...AClockworkOrange, id: undefined }
selectIdValue(undefinedAClockworkOrange, (book: any) => book.id)

expect(spy).toHaveBeenCalled()
expect(consoleWarnSpy).toHaveBeenCalledOnce()
})

it('should not warn when key does not exist in prod mode', async () => {
process.env.NODE_ENV = 'production'
vi.stubEnv('NODE_ENV', 'production')

const { selectIdValue } = await import('../utils')
const spy = vi.spyOn(console, 'warn')

selectIdValue(AClockworkOrange, (book: any) => book.foo)

expect(spy).not.toHaveBeenCalled()
expect(consoleWarnSpy).not.toHaveBeenCalled()
})

it('should not warn when key is undefined in prod mode', async () => {
process.env.NODE_ENV = 'production'
vi.stubEnv('NODE_ENV', 'production')

const { selectIdValue } = await import('../utils')
const spy = vi.spyOn(console, 'warn')

const undefinedAClockworkOrange = { ...AClockworkOrange, id: undefined }
selectIdValue(undefinedAClockworkOrange, (book: any) => book.id)

expect(spy).not.toHaveBeenCalled()
expect(consoleWarnSpy).not.toHaveBeenCalled()
})
})
})
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { noop } from '@internal/listenerMiddleware/utils'
import type { PayloadAction } from '@reduxjs/toolkit'
import {
configureStore,
createAction,
createListenerMiddleware,
createSlice,
isAnyOf,
TaskAbortError,
} from '@reduxjs/toolkit'
import { vi } from 'vitest'

import type { PayloadAction } from '@reduxjs/toolkit'

import { createListenerMiddleware, TaskAbortError } from '../index'

import type { TypedAddListener } from '../index'

describe('Saga-style Effects Scenarios', () => {
interface CounterState {
Expand Down Expand Up @@ -57,10 +54,7 @@ describe('Saga-style Effects Scenarios', () => {
return new Promise((resolve) => setTimeout(resolve, ms))
}

beforeAll(() => {
const noop = () => {}
vi.spyOn(console, 'error').mockImplementation(noop)
})
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)
Copy link
Member

Choose a reason for hiding this comment

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

I just wanted to suggest that we do

Suggested change
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)
using consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)

inside the tests that actually need this, and skip the mockClear and mockRestore calls, but I'm not sure if vitest actually returns a Disposable here. I know jest does (because I added the functionality there) - could you check if vitest has that now, too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure I can try it. The type tests will probably fail, so I'll probably exclude the runtime tests from the type tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like vitest doesn't return a Disposable, though I might try to submit a PR to add that functionality for vitest.

Copy link
Member

Choose a reason for hiding this comment

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

Will definitely be a win in the long run, even if it's not applicable to this PR in the end :)


beforeEach(() => {
listenerMiddleware = createListenerMiddleware<CounterState>()
Expand All @@ -72,6 +66,14 @@ describe('Saga-style Effects Scenarios', () => {
})
})

afterEach(() => {
consoleErrorSpy.mockClear()
})

afterAll(() => {
consoleErrorSpy.mockRestore()
})

test('throttle', async () => {
// Ignore incoming actions for a given period of time while processing a task.
// Ref: https://redux-saga.js.org/docs/api#throttlems-pattern-saga-args
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
import {
listenerCancelled,
listenerCompleted,
} from '@internal/listenerMiddleware/exceptions'
import type {
AbortSignalWithReason,
AddListenerOverloads,
} from '@internal/listenerMiddleware/types'
import { noop } from '@internal/listenerMiddleware/utils'
import type {
Action,
ListenerEffect,
ListenerEffectAPI,
PayloadAction,
TypedRemoveListener,
TypedStartListening,
UnknownAction,
} from '@reduxjs/toolkit'
import {
TaskAbortError,
addListener,
Expand All @@ -10,28 +28,6 @@ import {
removeListener,
} from '@reduxjs/toolkit'
import type { Mock } from 'vitest'
import { vi } from 'vitest'

import type {
Action,
ListenerEffect,
ListenerEffectAPI,
PayloadAction,
TypedAddListener,
TypedRemoveListener,
TypedStartListening,
UnknownAction,
} from '@reduxjs/toolkit'

import {
listenerCancelled,
listenerCompleted,
} from '@internal/listenerMiddleware/exceptions'

import type {
AbortSignalWithReason,
AddListenerOverloads,
} from '@internal/listenerMiddleware/types'

const middlewareApi = {
getState: expect.any(Function),
Expand All @@ -51,8 +47,6 @@ const middlewareApi = {
throwIfCancelled: expect.any(Function),
}

const noop = () => {}

// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
export interface Deferred<T> extends Promise<T> {
resolve(value?: T | PromiseLike<T>): void
Expand Down Expand Up @@ -119,9 +113,7 @@ describe('createListenerMiddleware', () => {
const testAction2 = createAction<string>('testAction2')
const testAction3 = createAction<string>('testAction3')

beforeAll(() => {
vi.spyOn(console, 'error').mockImplementation(noop)
})
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)

beforeEach(() => {
listenerMiddleware = createListenerMiddleware()
Expand All @@ -136,6 +128,14 @@ describe('createListenerMiddleware', () => {
})
})

afterEach(() => {
consoleErrorSpy.mockClear()
})

afterAll(() => {
consoleErrorSpy.mockRestore()
})

describe('Middleware setup', () => {
test('Allows passing an extra argument on middleware creation', () => {
const originalExtra = 42
Expand Down
14 changes: 8 additions & 6 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { noop } from '@internal/listenerMiddleware/utils'
import type { SubscriptionOptions } from '@internal/query/core/apiState'
import type { SubscriptionSelectors } from '@internal/query/core/buildMiddleware/types'
import { server } from '@internal/query/tests/mocks/server'
Expand Down Expand Up @@ -32,7 +33,6 @@ import {
import userEvent from '@testing-library/user-event'
import { HttpResponse, http } from 'msw'
import { useEffect, useState } from 'react'
import type { MockInstance } from 'vitest'

// Just setup a temporary in-memory counter for tests that `getIncrementedAmount`.
// This can be used to test how many renders happen due to data changes or
Expand Down Expand Up @@ -967,14 +967,16 @@ describe('hooks tests', () => {
})

describe('Hook middleware requirements', () => {
let mock: MockInstance
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(noop)

beforeEach(() => {
mock = vi.spyOn(console, 'error').mockImplementation(() => {})
afterEach(() => {
consoleErrorSpy.mockClear()
})

afterEach(() => {
mock.mockReset()
afterAll(() => {
consoleErrorSpy.mockRestore()
})

test('Throws error if middleware is not added to the store', async () => {
Expand Down
Loading