Unable to mock a dependent function if its from the same module - javascript

I have been trying to debug why this is happening. I am unable to mock a dependent function if its from the same module as the function calling it. But I am able to overcome this if the mocked function is moved to a separate module which is different from the module of the function calling it.
Not working scenario
Module A (filename.ts)
export const callingFunction = () => {
//....statements
dependentFunction();
}
export const dependantFunction = () => {
//....statements
//resolve with something
}
filename.test.ts
import { callingFunction } from './fileName'
jest.mock('./fileName',() => ({
...jest.requireActuals('./fileName'),
dependentFunction: jest.fn().mockImplementation(/*....Mocked implementation*/)
})
test('...test case description...', () => {
const callingFunctionRespose: any = callingFunction();
expect(callingFunctionResponse).toEqual(/*....something.....*/);
});
The above mock does not override the dependentFunction exported by the fileName.ts module. Instead, when the exported function callingFunction() is called, it uses the implementation defined in the module. (Found this out by logging the function definitions.
But this behaviour is not observed when the dependant function is moved to it own separate module.
Working scenario
fileName.ts
import { dependentFunction } from './dependentFunctions'
export const callingFunction = () => {
//....statements
dependentFunction();
}
dependentFunctions.ts
export const dependantFunction = () => {
//....statements
//resolve with something
}
fileName.test.ts
import { callingFunction } from './fileName'
jest.mock('./dependentFunctions',() => ({
...jest.requireActuals('./dependentFunctions'),
dependentFunction: jest.fn().mockImplementation(/*....Mocked implementation*/)
})
test('...test case description...', () => {
const callingFunctionRespose: any = callingFunction();
expect(callingFunctionResponse).toEqual(/*....something.....*/);
});

You can import the functions as a module
import * as module from './fileName'
To mock the implementation you can do
jest.spyOn(module, 'dependentFunction').mockImplementation(/*....Mocked implementation*/)
To call the other function, use
module.callingFunction()

Appologies for the late update. But the solution in the following article was the fix:
https://medium.com/welldone-software/jest-how-to-mock-a-function-call-inside-a-module-21c05c57a39f

Related

How to test if an imported Promise was called by the tested function?

I have a function to test with Jest, which calls some Promises from another module. I mocked out these Promises, but .toBeCalled() always reports they were never called. I'm probably doing it wrong.
The tested function is doing something like this:
import { externalPromise } from '#helpers';
export const functionToTest = () => {
return await externalPromise();
}
This is how I'm trying to test it:
import { externalPromise } from '#helpers';
import { functionToTest } from './whatever';
jest.mock('#helpers', () => ({
...jest.requireActual('#helpers'),
externalPromise: jest.fn().mockImplementation(() => Promise.resolve())
}));
it('should call externalPromise', () => {
functionToTest();
expect(externalPromise).toHaveBeenCalled();
});
The calls to externalPromise is always 0. How can I test if it was called or not?

How to mock a class method that would be used inside an asynchronous callback?

I am new to Jest and trying to wrap my head around this testing framework. My problem is understanding about the behaviour of es6 class mocking inside an asynchronous code. Example i have an exported class like this:
//util_tests.ts
export default class Utils{
getMemberAccounts = (): Promise<Number> => new Promise((resolve, reject) => resolve(5));
}
and then the file i wanted to test (the function i want to mock is getMemberAccounts):
//test.ts
import Util from './util_test'
import logger from '../common/logger';
import _ from 'lodash';
const util = new Util();
util.getMemberAccounts().then(test =>{
logger.info(test); // this would have been populated with mocked data
});
setTimeout(async ()=> {
let test = await util.getMemberAccounts();
logger.info(test); // this wouldnt have mocked data, why? and how to solve this ?
}, 0);
export default class Test{
}
My test file is like this:
import Util from '../src/test/util_test'
import Test from '../src/test/test'
import { mocked } from 'ts-jest/utils'
jest.mock('../src/test/util_test', () => {
return jest.fn().mockImplementation(() => {
return {
getMemberAccounts: jest.fn().mockResolvedValue({
accounts: [
{
id: '0zh8ap6luxoijne8qlfu1sg',
name: 'test',
officeNumber: '23',
emailAddress: 'string#gmail'
}
]
}
)
};
});
});
describe('Test', () => {
let testCase: Test;
beforeEach(async () => {
mocked(Util).mockClear();
testCase = new Test();
})
test('test-case', async () => {
})
});
Why does my mocking doesnt work in setTimeout function? is it because it's related to the nature of setTimeout and callbacks ? How do i mock the function of the class in this case?
Leave it here if anyone needed help. So the problem is that setTimeout invoke a function to be called back later and there is no way jest knew about it and the test is finished. therefore jest cant mock the value into the function anymore. So we need to mock the setTimeout timer to resolve the value inside the callback function.

jest ReferenceError: Cannot access '' before initialization

I'm getting the error:
ReferenceError: Cannot access 'myMock' before initialization
Even though i respected jest documentation about the hoisting:
A limitation with the factory parameter is that, since calls to jest.mock() are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'.
I'm doing this:
import MyClass from './my_class';
import * as anotherClass from './another_class';
const mockMethod1 = jest.fn();
const mockMethod2 = jest.fn();
jest.mock('./my_class', () => {
return {
default: {
staticMethod: jest.fn().mockReturnValue(
{
method1: mockMethod1,
method2: mockMethod2,
})
}
}
});
as you can see both of my variables respect the "standard" but are not hoisted properly.
Am I missing something ?
Obviously it works when I just pass jest.fn() instead of my variables, but i'm not sure how to be able to use these in my test later on.
None of the answers above solved my problem, so here's my solution:
var mockMyMethod: jest.Mock;
jest.mock('some-package', () => ({
myMethod: mockMyMethod
}));
Something about using const before the imports feels weird to me. The thing is: jest.mock is hoisted. To be able to use a variable before it you need to use var, because it is hoisted as well. It doesn't work with let and const because they aren't.
The accepted answer does not handle when you need to spy on the const declaration, as it is defined inside the module factory scope.
For me, the module factory needs to be above any import statement that eventually imports the thing you want to mock.
Here is a code snippet using a nestjs with prisma library.
// app.e2e.spec.ts
import { Test, TestingModule } from '#nestjs/testing';
import { INestApplication } from '#nestjs/common';
import * as request from 'supertest';
import mockPrismaClient from './utils/mockPrismaClient'; // you can assert, spy, etc. on this object in your test suites.
// must define this above the `AppModule` import, otherwise the ReferenceError is raised.
jest.mock('#prisma/client', () => {
return {
PrismaClient: jest.fn().mockImplementation(() => mockPrismaClient),
};
});
import { AppModule } from './../src/app.module'; // somwhere here, the prisma is imported
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
)};
To clarify what Jason Limantoro said, move the const above where the module is imported:
const mockMethod1 = jest.fn(); // Defined here before import.
const mockMethod2 = jest.fn();
import MyClass from './my_class'; // Imported here.
import * as anotherClass from './another_class';
jest.mock('./my_class', () => {
return {
default: {
staticMethod: jest.fn().mockReturnValue(
{
method1: mockMethod1,
method2: mockMethod2,
})
}
}
});
The problem that the documentation addresses is that jest.mock is hoisted but const declaration is not. This results in factory function being evaluated at the time when mocked module is imported and a variable being in temporal dead zone.
If it's necessary to access nested mocked functions, they need to be exposed as a part of export object:
jest.mock('./my_class', () => {
const mockMethod1 = jest.fn();
const mockMethod2 = jest.fn();
return {
__esModule: true,
mockMethod1,
mockMethod2,
default: {
...
This also applies to manual mocks in __mocks__ where variables are accessible inside a mock only.
You should move your mocking above your imports; that could be the source of your issue. Imports are also hoisted, so multiple hoisted entries would be hoisted in order.
jest.mock('./my_class', () => {
const mockMethod = jest.fn()
const default = { staticMethod: jest.fn().mockReturnValue({ method: mockMethod }) };
return { default, mockMethod };
});
import MyClass, { mockMethod } from './my_class'; // will import your mock
import * as anotherClass from './another_class';
However, if you for some reason can't do that, you could use doMock to avoid hoisting behaviour. If this happens on the top of your file, it should be a 1 to 1 change.
const mockMyMethod = jest.fn();
jest.doMock('some-package', () => ({ myMethod: mockMyMethod }));
This solution works for me and it's pretty easy for vuejs+ jest.
Two points to note:
you should declare the absolute path and not '#/js/network/repositories'
the getter helps to defer the instantiation
const mockGetNextStatuses = jest.fn();
const mockUpdatePrintingStatus = jest.fn();
jest.mock('../../../../../../src/js/network/repositories/index.js', () => {
return {
get printing() {
return {
getNextStatuses: mockGetNextStatuses,
updatePrintingStatus: mockUpdatePrintingStatus,
}
}
}
});
or
jest.mock('../../../../../../src/js/network/repositories/index.js', () => ({
printing: {
getNextStatuses: jest.fn(),
updatePrintingStatus: jest.fn()
}
}));
import { printing } from '../../../../../../src/js/network/repositories/index.js';
// and mock the module
printing.getNextStatuses.mockReturnValue(['XX','YY']);
Example of using TypeScript with Jest and mockDebug.js module
jest.mock('debug', () => {
global.mockDebug = jest.fn();
return () => global.mockDebug;
});
// usage
describe('xxx', () => {
test('xxx', () => {
expect(global.mockDebug.mock.calls.toString()).toContain('ccc');
})
});

Jest: Vue Component can't find mocked function

I'm mocking a ES6 class which is used inside my Vue Component:
export default class DataUploadApi {
// Get uploaded files
static async getUploadedFiles() : Promise<Object> {
return WebapiBase.getAsync({uri: DATA_UPLOAD_ENPOINTS.FILES});
}
}
I've been following along with this document, but I think my syntax is slightly off with my mock:
import { mount } from '#vue/test-utils';
import DataUploadApi from '../webapi/DataUploadService';
import FileDownloadList from '../components/file-download-list.vue';
const mockGetUploadedFiles = jest.fn().mockResolvedValue({json: JSON.stringify(uploadedFilesObj)});
jest.mock('../webapi/DataUploadService', () => jest.fn().mockImplementation(() => ({getUploadedFiles: mockGetUploadedFiles})));
describe('file-download-list component', () => {
beforeEach(() => {
// #ts-ignore
DataUploadApi.mockClear(); // https://stackoverflow.com/a/52707663/1695437 dont use # imports on mocks.
mockGetUploadedFiles.mockClear();
});
describe('renders correct markup:', () => {
it('without any uploaded files', () => {
const wrapper = mount(FileDownloadList, {});
expect(wrapper).toMatchSnapshot();
});
});
});
This test passes. However, in the snapshot I can see that the API called failed with this error message:
<p>
_DataUploadService.default.getUploadedFiles is not a function
</p>
What have I done wrong with the function mock? Thanks in advance!
There seemed to be a few issues with my code:
Mocking the API
Using an internal mockImplementation seems to cause issues, and is not required if you don't need the additional mock functionality.
jest.mock('#/apps/gb-data/webapi/DataUploadService', () => ({
getUploadedFiles() {
return Promise.resolve({ uploaded_files: {} });
},
}));
Changes to the test
Both flushPromises and nextTick are required.
it('with uploaded files', async () => {
const wrapper = mount(FileDownloadList, {
stubs: fileDownloadListStubs,
});
await flushPromises();
await wrapper.vm.$nextTick();
expect(wrapper).toMatchSnapshot();
});

Several mocks of one module in one file with jest

I have to functions for example a, b. Both of them are elements of one module, and they are re-exported in index.js. Function a invokes function b.
It all works if i use jest.mock on the top of the file, but if i want to specify different mock implementation of b function in every it block it doesn't work. Also i try'ed to use jest.doMock but it doesn't work as well.
a.js
import * as fromDependencies from '.'
export function(arg) {
return !fromDependencies && !arg;
}
b.js
export function b() {
//some code
return boolean;
}
index.js
export * from 'a.js',
export * from 'b.js'
testFile
import a from '../a.js';
describe('isGroupOverlaidTest', () => {
it('should return false', () => {
jest.mock('../.', () => ({
b: jest.fn(() => true);
}))
expect(a(true)).toBe(false);
});
it('should return true', function() {
jest.mock('../.', () => ({
b: jest.fn(() => false);
}))
expect(a(false)).toBe(false);
});
});
The results are fake, anyway i want to call my mocked function not the original one. When i have jest.mock in the top of the file it works but i can achieve just one mock for on file. Do mock does't work. I'll be really grateful if somebody can provide some example how i can resolve that ;).
You could use a spy instead of a mock. Since a exports a function instead of an object then you'd have to wrap it.
import a from '../a.js'
const aWrapped = { a }
describe('isGroupOverlaidTest', () => {
it('should return false', () => {
const mockA = jest.fn()
const spyedA jest.spyOn(aWrapped, a).mockReturnValue(mockA)
expect(spyedA(true)).toBe(false);
});
});
Something like that.

Categories

Resources