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

Prevent resetMocks: true from clearing mock implementations #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions example/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
preset: 'ts-jest',
resetMocks: true,
setupFiles: ['jest-launchdarkly-mock'],
testEnvironment: 'jsdom',
testPathIgnorePatterns: ['tsoutput'],
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
preset: 'ts-jest',
setupFiles: ['./src/index.ts'],
resetMocks: true,
testPathIgnorePatterns: ['<rootDir>/example/', '<rootDir>/node_modules/'],
}
6 changes: 5 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { renderHook } from '@testing-library/react-hooks'
import { useFlags, useLDClient, useLDClientError, LDProvider, asyncWithLDProvider } from 'launchdarkly-react-client-sdk'
import { mockFlags, ldClientMock, resetLDMocks } from './index'
import { mockFlags, ldClientMock, resetLDMocks, setupMocks } from './index'

describe('main', () => {
beforeEach(() => {
setupMocks()
})

test('mock kebab-case correctly', () => {
mockFlags({ 'dev-test-flag': true })

Expand Down
20 changes: 12 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ export const ldClientMock = {
waitUntilReady: jest.fn(),
}

/* eslint-disable @typescript-eslint/no-explicit-any */
mockAsyncWithLDProvider.mockImplementation(() => Promise.resolve((props: any) => props.children))
mockLDProvider.mockImplementation((props: any) => props.children)
mockUseLDClient.mockImplementation(() => ldClientMock)
mockWithLDConsumer.mockImplementation(() => (children: any) => children)
mockWithLDProvider.mockImplementation(() => (children: any) => children)
/* eslint-enable @typescript-eslint/no-explicit-any */
export const setupMocks = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

To be consistent with resetLDMocks, can you please rename this to setupLDMocks?

/* eslint-disable @typescript-eslint/no-explicit-any */
mockAsyncWithLDProvider.mockImplementation(() => Promise.resolve((props: any) => props.children))
mockLDProvider.mockImplementation((props: any) => props.children)
mockUseLDClient.mockImplementation(() => ldClientMock)
mockUseFlags.mockImplementation(() => ({}))
mockWithLDConsumer.mockImplementation(() => (children: any) => children)
mockWithLDProvider.mockImplementation(() => (children: any) => children)
/* eslint-enable @typescript-eslint/no-explicit-any */
}
setupMocks()

export const mockFlags = (flags: LDFlagSet) => {
mockUseFlags.mockImplementation(() => {
Expand All @@ -75,7 +79,7 @@ export const mockFlags = (flags: LDFlagSet) => {
}

export const resetLDMocks = () => {
mockUseFlags.mockImplementation(() => ({}))
setupMocks()

Object.keys(ldClientMock).forEach((k) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down