// processData.js code (all in the same file)
export const getCurrentTotals = async () => {
// setup etc.
const data = await s3.getObject(params).promise();
return JSON.parse(data.Body.toString('utf-8'));
};
export const calculateTotalsAndRecents = async (input) => {
const currentTotals = await getCurrentTotals();
// do calculations etc
// processData.test.js
import * as processData from './processData.js';
// ...
describe('#calculateTotalsAndRecents', () => {
beforeEach(() => {
const mock = jest.spyOn(processData, 'getCurrentTotals');
mock.mockImplementationOnce(() => Promise.resolve({}));
});
it('calculates totals', async () => {
const r = await processData.calculateTotalsAndRecents({ year: 2019, ...});
The issue here is that the call to getCurrentTotals
in calculateTotalsAndRecents
always calls the actual function instead. I've tried a few possible fixes like
How can I mock an ES6 module import using Jest?
Jest mock async function from default import
Tried adding jest.mock('./processData.js')
after the import but that mocks all the functions in the file. I want to override just one function in the file for just the one test.
Do I have to mock the whole file with __mocks__
or is there a simpler way to do this?