I have a requirement to test a function within my TypeScript module.
module-to-test.ts
import { config } from './app-config';
export const isSomethingWhatINeedSelector = createSelector(
firstDependencySelector,
secondDependencySelector,
(first, second) => config.key && (first || !second)
);
Instead of writing multiple tests for this scenario, I am looking into utilizing the describe.each([[],[],[]])
feature to streamline the testing process.
I specifically need to change the value of config.key dynamically for each iteration when using describe.each. When attempting to mock the configuration file like this at the beginning of the test-file:
jest.mock('./app-config', () => ({
config: {
key : false,
},
}));
it affects the entire file and all its tests. My goal is to create mocks inside individual "test/it" functions to allow for dynamic changes in the key value.
Currently, the following code is not behaving as expected
:
describe.each([
[
'should be ....',
true, false
],
[
'should be ....',
false, true
],
/* and so forth... [], [], [] ... with only two parameters required for the question*/
])('Functionality of ...', (
testTitle = '',
mockStatusOfConfigKey,
expected,
) => {
const state = ... /* initial */
beforeEach(() => {
jest.resetModules();
/*....configure the state*/
});
it(testTitle, () => {
jest.mock('./app-config', () => ({ /*...or doMock(), which does not work as intended*/
config: {
key : mockStatusOfConfigKey,
},
}));
expect(isSomethingWhatINeedSelector(state)).toBe(expected);
});
});
Any suggestions on how to make mocks dynamically changeable within test functions?
config.key can only be true or false