diff --git a/example/jest.config.js b/example/jest.config.js index 4482e5b..ad987bb 100644 --- a/example/jest.config.js +++ b/example/jest.config.js @@ -1,5 +1,6 @@ module.exports = { preset: 'ts-jest', + resetMocks: true, setupFiles: ['jest-launchdarkly-mock'], testEnvironment: 'jsdom', testPathIgnorePatterns: ['tsoutput'], diff --git a/jest.config.js b/jest.config.js index b7d3186..87a7d29 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,5 +1,6 @@ module.exports = { preset: 'ts-jest', setupFiles: ['./src/index.ts'], + resetMocks: true, testPathIgnorePatterns: ['/example/', '/node_modules/'], } diff --git a/src/index.test.ts b/src/index.test.ts index 22ccbf4..cf1acaa 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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 }) diff --git a/src/index.ts b/src/index.ts index 3e66e55..3cc9f0c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 = () => { + /* 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(() => { @@ -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