The issue I am facing involves the following steps:
- Setting up mocks in the beforeEach function
- Attempting to modify certain mock behaviors in specific tests where uniqueness is required
- Encountering difficulty in changing the values from the initial setup in step #1
Note that this code snippet is somewhat pseudo code, so there may be some syntax errors as the exact source code is not readily available:
import Module from '../module';
...
jest.mock('../module');
describe('test suite', {
let mockFn = undefined;
beforeEach(() => {
mockFn = jest.fn(() => 'banana');
Module.function = mockFn;
});
test('happy test', () => {
// This test relies on Module.function returning 'banana' to function correctly
});
test('test with issue', () => {
let mockFn2 = jest.fn(() => 'new value instead of banana');
Module.function = mockFn2;
// Execute test code
// Calls to Module.function still return 'banana'
});
}
I have attempted various approaches such as using an afterEach function that calls mockFn.mockClear(). However, I have been unable to override the original mock function. While the default mock values work for most tests, there are specific instances where I need to ensure the code reacts differently based on returned values from these functions, yet I am unable to change it from the initial mock function assigned.
Edit - Below is an example of how the function is utilized:
function() {
...
if(Module.function().includes('somevalue'){
// Perform certain actions
}
else {
fetch(Module.function() + other stuff... )
...
}
};
Here is the definition of the function:
export default class Module {
public static function() {
// Function definition here
}
}
Thank you.