Selectively mocking a function on Jest

99
January 10, 2020, at 4:40 PM

I might be missing something major here, but I can't seem to find a suitable answer from the docs. I'm trying to selectively replace a utility function with a mock implementation in Jest. My reasoning for this is that sometimes I want to force it to fail during testing to see how it impacts the rest of my code, and also to see if my catches are working properly. For example

// actual.js
export const foo = () => {return 'foo'}
//mock.js
export const foo = (returnFoo) => {return returnFoo ? 'foo' : 'bar'}

I can't seem to use jest.mock(actual.js) because the 2 functions have different signatures. I can use jest.setMock() with jest.requireActual(),, but it'll mock foo for the entire suite. My current solution is doing

const mock = jest.spyOn(actual, 'foo');
mock.mockImplementation(() => mock.foo(returnFoo));
which can be simplified to
jest.spyOn(actual, 'foo').mockImplementation(() => mock.foo(returnFoo));

which does works. I'm really just wondering if Jest has a better way of doing this than adding a spy on every individual function I want to purposely and manually mock for individual tests. I've also tried doing const actual.foo = jest.fn(), but that doesn't work since foo in actual is a read-only export, and also Object.defineProperty(vendorsRepo, 'updateToken', { value: mockedVendorsRepo.updateToken() }); but that seems like a terrible workaround.

READ ALSO
Trying to have to columns with timestamp on google sheet

Trying to have to columns with timestamp on google sheet

Column 11 is working fine (Last modified date) but column 12 (entered date) is not showing me any result help me find the problem with this code

106
Issue regarding logging the pose object in posenet of tensorflow.js

Issue regarding logging the pose object in posenet of tensorflow.js

When I run the above code only the pose object of the last image is logged 5 times

84
async load and prepend the content of a file

async load and prepend the content of a file

I need to prepend the content of imgtphp to bb

71
Assign a role/customize HTML tooltip to PieChart/DonutChart Google Charts VueJS

Assign a role/customize HTML tooltip to PieChart/DonutChart Google Charts VueJS

I've implemented a VueJS wrapper for Google Charts called vue-google-charts and I'm looking to solve a tool tip scenario

55