. Advertisement .
..3..
. Advertisement .
..4..
Jest is considered as a satisfying Testing Framework in JavaScript that it attends to the simplicity. Jest is well-documented and requires little arrangement; it also can be extended to meet the requirements. There are many testing types but you can call 3 main categories as follow: unit testing, integration and UI testing. Today, we will show the function with unit testing that you may deal with. It uses mocks and spices on the complicated objects by Jet. Reading our post to get useful information about the Jet problem!
Why don’t you use mocks and spices in Jet on the complicated object?
When you want to test and write the test for the exposed codebase in javaScript throughout Jet. This code includes some function applying cases and it is conditionally put in it and accomplished via the browser at the time of loading the page. So, you have problem with mock up custom object with the following function:
const setEnterpriseCookie = () => {
// Get the current page uri
let path = window.location.pathname;
// Matches all pages containing '/regex_expression'
if (path.match(/.*/regex_expression.*/)) {
window.TOOL.cookie.setCookie(...args);
}
};
It can be understood that you mock both of the window.location.pathname get back the string and window.TOOL.cookie.setCookie() for a function of mock. To know the detail with function mock that you try to test, see the written test here:
var windowSpy;
describe('Tests for the page-specific-methods.js file', () => {
beforeEach( () => {
windowSpy = jest.spyOn(global, 'window', 'get');
});
afterEach( () => {
windowSpy.mockRestore();
})
test('Test the page path detecting the enterprise string', () => {
windowSpy.mockImplementation( () => ({
location: {
pathname: '/enterprise/contact',
},
TOOL: {
cookie: {
setCookie: jest.fn(),
},
},
}));
setEnterpriseCookie();
expect(window.TOOL.cookie.setCookie).toBeCalledTimes(1);
expect(window.TOOL.cookie.setCookie).toHaveBeenLastCalledWith(...args);
})
});
So, the written test above is a failure. It means window.TOOL.cookie.setCookie returned the result zero times. Although the function is performing with window.location.pathname as the requirement, which function is incorrect?
The issue can occur when you mock window.TOOL.cookie.setCookie because it is still not getting back the right value. Therefore, find the method to address this problem in the next part.
How to solve this problem?
If you try many times and have not found the correct code or function, please use Object.defineProperty().
The Object.defineProperty() is a method to define a current property on the object directly or adjust a current property and return on the object. Syntax of Object.defineProperty():
Object.defineProperty(obj, prop, descriptor)
Parameter: Three parameters mentioned above are accepted by this method and described as the following: Obj: This parameter contains the object for which the user will define the attribute. Prop: The name of a property that will be defined or updated is inluded in this parameter. Descriptor: The descriptor for the property being defined or changed is included in this parameter. Return Value: The object that was supplied as an argument to the function is returned by this method.
Below is a command of index.js:
const setEnterpriseCookie = (...args) => {
let path = window.location.pathname;
if (path.match(/.*/enterprise.*/)) {
window.TOOL.cookie.setCookie(...args);
}
};
exports.setEnterpriseCookie = setEnterpriseCookie;
And index.test.js as follow:
const { setEnterpriseCookie } = require('./');
describe('63274598', () => {
describe('Tests for the page-specific-methods.js file', () => {
test('Test the page path detecting the enterprise string', () => {
Object.defineProperty(window, 'location', {
value: { pathname: '/enterprise/contact' },
});
Object.defineProperty(window, 'TOOL', {
value: {
cookie: {
setCookie: jest.fn(),
},
},
});
setEnterpriseCookie('123');
expect(window.TOOL.cookie.setCookie).toBeCalledTimes(1);
expect(window.TOOL.cookie.setCookie).toHaveBeenLastCalledWith('123');
});
});
});
After that, the result for the unit test is passed with the follow value:
PASS stackoverflow/63274598/index.test.js (13.923s)
63274598
Tests for the page-specific-methods.js file
âœ" Test the page path detecting the enterprise string (4ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 50 | 100 | 100 |
index.js | 100 | 50 | 100 | 100 | 3
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 15.975s
This method is very simple, but it works flawlessly for you. It will help you resolve your error and makes your error will run well. So, what are you waiting without applying it? Let’s try it to get your desired results!
Conclusion
The solution mentioned above is the fastest method for those still confused with the problem How to use Mocks and Spies on the complicated objects by Jest. If you still need support or have any questions related to JavaScript, we have one vibrant community where all members are always willing to assist you with any trouble. Finally, wishing you a more successful day with new approaches or functions and sees you!
Read more
Leave a comment