In Jest, how to test a function declared inside a function? - javascript

I am trying to test a function that is declared inside another function(Parent). I tried a few things but was not able to test that. I am using react library and JEST testing framework in the project.
Here is the service file (service.ts) function which I am trying to test:
import { useThisHook } from './base-hooks';
export function someFunction(param: string) {
const [
resolveFunction,
,
{ loading, error },
] = useThisHook();
const childFun = (param : number) => {
resolveFunction(someArguments);
}
return{
someValue: childFun
}
}
Here is the spec file code:
import * as SomeHooks from './base-hooks';
import * as Service from './service';
describe('Service function', () => {
beforeEach(() => {
jest.spyOn(SomeHooks, 'useThisHook').mockReturnValue(
{
//Some Value
}
);
});
test('Some function test', () => {
const someFunctionResponse = Service.someFunction('string');
expect(someFunctionResponse).toEqual({
someValue: expect.any(Function),
});
});
});
Till here it is fine. Tests are also getting passed but the problem is that childFn is not getting the coverage and that's what my requirement is. I am new to React and Jest. I am not able to understand how can I achieve that. I tried many things but didn't succeed.

Your childFn is not being called, that's why it's not getting coverage.
You could either refactor the whole childFn to a new hook and test it individually with something like react-hooks-testing-library.
Or you could separate the childFn and declarate it outside the someFunction scope pass the resolveFunction and then test it.
You only get coverage if the code is actually called, not when it is declared.

Related

custom global function not defined when testing in Jest; works fine when not testing

I have a custom, globally-scoped function in my Express app, foo. When running my Jest test scripts, this function is caught as undefined. Thus, any tests using them fail.
index.d.ts:
declare global{
function foo(): string;
}
export {};
src/Utils/index.ts:
global.foo = function foo(){
return "bar";
};
src/Modules/Example.module.ts:
export const test = () => {
// This will return bar, as expected, when developing.
// A reference error will only be thrown when running npm test.
return foo();
};
src/Modules/Example.test.ts:
import { test } from "./Example.module";
describe("modules/example", () => {
describe("test", () => {
it("returns bar", () => {
let bar = test();
expect(bar).toBe("bar");
});
});
});
Despite this not being an issue while developing, this test results in the error:
ReferenceError: foo is not defined.
export const test = () => {
return foo();
^
...
};
You can specify src/Utils/index.ts as a setup file, which Jest will load and execute before running tests. You can add it to your Jest configuration file (or create one if you don't have one):
Assuming a CJS-format Jest configuration, jest.config.js:
module.exports = {
// Your other configuration options
"setupFiles": ["<rootDir>/src/Utils/index.ts"]
};
It will look slightly different if you are using a JSON or TypeScript Jest configuration file.
However I don't recommend using global variables (even if you use them a lot). With a proper code editor setup, it is easy to import a function from another file.

Mock a function inside a module using Jest

I'm currently having a problem with mocking a function inside another lib.
In our team, we have a project (npm lib, let's call it component-lib) with some components and functions that are being used by our React main app. We expose the functions and components in a single JS file. (index.js) like this:
import { isFeatureEnabled } from './modules/features'
import mockComponent from './components/mockComponent'
export {
isFeatureEnabled,
mockComponent
}
I'm trying to mock the return value of one of the functions inside this component-lib that is being used by the main react app, my unit test goes like this:
PS: I'm using Jest and React Testing Library
describe('unit test', () => {
jest.mock('component-lib', () => ({
...jest.requireActual('component-lib'),
isFeatureEnabled: jest.fn().mockReturnValue(false)
}))
it('Should send data', () => {
render(<Main />)
[... i do some stuff here and trigger the isFeatureEnabled function ...]
})
})
and the code I use it:
function onSubmitPassword (data) {
console.log('is this feature enabled? ', isFeatureEnabled('feature')) // logs as true
if (isFeatureEnabled('feature') {
...do this // this is executed
} else {
do that
}
}
The thing is that when I do a console.log to see the value of isFeatureEnabled, it's returning true instead of false, ignoring the mock.
Do you guys have any idea why the mock is not working in this case?
I already tried importing the lib as:
import * as components from 'component-lib'
jest.mock('component-lib', () => ({
...jest.requireActual('component-lib'),
isFeatureEnabled: jest.fn()
}))
it('Should send data', () => {
isFeatureEnabled.mockReturnValue(false)
render(<Main />)
[... i do some stuff here and trigger the isFeatureEnabled function ...]
})
and it returns an error saying the function only has a getter

Jasmine Data Provider is not working (jasmine_data_provider_1.using is not a function)

I am trying to achieve Data Driven testing in my project by using jasmine data providers.
I have a data.ts file like below
export const hardshipTestData = {
scenarios: {
scenario1: {
isHome: 'Yes'
},
scenario2: {
isHome: 'No'
}
}
};
I am using above data in spec file
import { using } from 'jasmine-data-provider';
import { hardshipTestData } from '../../data/testdata';
using(hardshipTestData.scenarios, function (data, description) {
it('testing data providers', () => {
console.log(data.isHome);
});
});
My issue here is when I am trying to write data. intelligence is not even giving the option isHome. When I enforce it and run the test I am getting the following error
TestSuite encountered a declaration exception
configuration-parser.js:48
- TypeError: jasmine_data_provider_1.using is not a function
any help is appreciated
You need to change import type. Try to replace:
import { using } from 'jasmine-data-provider';
with:
const using = require('jasmine-data-provider');
Also, keep in mind that firstly should be describe block:
describe('example test', () => {
using(hardshipTestData.scenarios, (data) => {
it('should calc with operator -', () => {
console.log(data.isHome);
});
});
});
Adding to Oleksii answer, his answer is for typescript.
but If you want to use in plain javascript use below:
Add below in your code:
var using = require('jasmine-data-provider');
Example:
var jasminedatasetobj = require("./jasmineDataDrivenData");
var using = require('jasmine-data-provider');
using(jasminedatasetobj.datadrive, function (data, description) {
it('Open NonAngular js website Alerts', async() => {
await browser.get("https://qaclickacademy.github.io/protocommerce/");
element(by.name("name")).sendKeys(data.name);
});
});
You might need to give full path of Jasmine data provider for plain javascripts to avoid module not found error.
var jsondataobj = require('../../../../config/Jsoninput.json');//define the data source location
var using = require('C:/Users/sam/AppData/Roaming/npm/node_modules/jasmine-data-provider');
describe("Test Jasmine Data provider",function(){
you need to declare the variable for "jasmine-data-provider" , because import can use to import the properties/classes.
instead of using variable you can give any name to the varible (I tried use "post" instead of "using" and it is still working as expected)
your code should be like
import { hardshipTestData } from "../Test";
const using = require("jasmine-data-provider");
describe("Login TestCases", () => {
using(hardshipTestData.scenarios, (alldata: any, alldesc: any) => {
it("login with different credentials", async () => {
console.log(data.isHome);
})
})
})
this will resolve you problem.

How to mock a directly-imported function using Jest?

I'm trying to verify that my method is correctly invoking another, imported method. For the life of me, I can't figure out how to mock the imported method using Jest.
Method I want to test
LandingPageManager.ts
import {getJSON} from './getJSON';
public fetchData(url: string) {
getJSON(url);
}
Method I want to mock
getJSON.ts
export function getJSON(url: string) {
// XHR requests logic
}
Test method
LandingPageManager.test.ts
import 'jest';
import {getJSON} from '../../../src/web/getJSON';
import {LandingPageManager} from '../../../src/web/LandingPageManager';
describe('fetchData', () => {
let manager = new LandingPageManager();
it('passes the correct URL to getJSON', () => {
const getJsonSpy = jest.mock('../../../src/web/getJSON', jest.fn());
manager.fetchData('sampleValue');
expect(getJsonSpy).toHaveBeenCalledWith('sampleValue');
getJsonSpy.restoreAllMocks();
});
});
Error I'm getting
jest.fn() value must be a mock function or spy
I've tried setting up the mock a variety of different ways. But I can't seem to get the syntax right.
Can anyone help point me in the right direction? I feel like this should be possible.
Finally figured out the answer.
Nothing needed to change in the source code (either the imported module or the class under test).
The import needed to change from:
import {getJSON} from '../../../src/web/getJSON';
to:
import * as getJSON from '../../../src/web/getJSON';
And then I was able to directly specify the function for spying with:
const jsonSpy = jest.spyOn(getJSON, 'getJSON');
Fixed test case
Here's how it all works together now.
LandingPageManager.test.ts
import 'jest';
// **** 1.) Changed the below line: ****
import * as getJSON from '../../../src/web/getJSON';
import {LandingPageManager} from '../../../src/web/LandingPageManager';
describe('fetchData', () => {
let manager = new LandingPageManager();
it('passes the correct URL to getJSON', () => {
// **** 2.) Can now specify the method for direct mocking ****
const jsonSpy = jest.spyOn(getJSON, 'getJSON');
manager.fetchData('sampleValue');
expect(jsonSpy).toHaveBeenCalledWith('sampleValue');
jest.restoreAllMocks();
});
});

Manual mock not working in with Jest

Can somebody help me with manual mocking in Jest, please? :)
I try to have Jest use the mock instead of the actual module.
my test:
// __tests__/mockTest.js
import ModuleA from "../src/ModuleA"
describe("ModuleA", () => {
beforeEach(() => {
jest.mock("../src/ModuleA")
})
it("should return the mock name", () => {
const name = ModuleA.getModuleName()
expect(name).toBe("mockModuleA")
})
})
my code:
// src/ModuleA.js
export default {
getModuleName: () => "moduleA"
}
// src/__mocks__/ModuleA.js
export default {
getModuleName: () => "mockModuleA"
}
I think I followed everything the documentation says about manual mocks, but perhaps I'm overlooking something here?
This is my result:
Expected value to be:
"mockModuleA"
Received:
"moduleA"
Module mocks are hoisted when possible with babel-jest transform, so this will result in mocked module:
import ModuleA from "../src/ModuleA"
jest.mock("../src/ModuleA") // hoisted to be evaluated prior to import
This won't work if a module should be mocked per test basis, because jest.mock resides in beforeEach function.
In this case require should be used:
describe("ModuleA", () => {
beforeEach(() => {
jest.mock("../src/ModuleA")
})
it("should return the mock name", () => {
const ModuleA = require("../src/ModuleA").default;
const name = ModuleA.getModuleName()
expect(name).toBe("mockModuleA")
})
})
Since it's not an export but a method in default export that should be mocked, this can also be achieved by mocking ModuleA.getModuleName instead of entire module.
This answer is not strictly related to the OPs question but google lead me here for a similar issue where jest.mock('module', () => ({})) in the root of a file did not work. In my case it was caused by cyclic dependencies. So if jest suddenly starts to ignore calls to jest.mock() then you might want to check for cyclic dependencies in your files.

Categories

Resources