Considering the example below
//AuthenticatorService.js
const UserService = require("./UserService");
class AuthenticatorService{
constructor(){
//Some initialisation
}
//Some methods
}
module.exports = new AuthenticatorService();
//UserService.js
class UserService{
constructor(){
//Again some initialisations
}
}
module.exports = new UserService();
So when I try to mock the UserService class in my AuthenticatorService.spec.js file the constructor of the UserService is getting executed as it is being exported in that way. But I don't want that to be executed. Is it possible to mock the UserService module inside AuthenticatorService.spec.js file without invoking its constructor.
You could use jest.mock(moduleName, factory, options) to do this.
E.g.
UserService.js:
class UserService {
constructor() {
console.log('initialize UserService');
}
hello() {
console.log('user hello real implementation');
}
}
module.exports = new UserService();
AuthenticatorService.js:
const userService = require('./UserService');
class AuthenticatorService {
constructor() {
console.log('initialize AuthenticatorService');
}
hello() {
userService.hello();
}
}
module.exports = new AuthenticatorService();
AuthenticatorService.test.js:
const authenticatorService = require('./AuthenticatorService');
const userService = require('./UserService');
jest.mock('./UserService', () => {
return { hello: jest.fn() };
});
describe('62967707', () => {
afterAll(() => {
jest.resetAllMocks();
});
it('should pass', () => {
userService.hello.mockImplementationOnce(() => console.log('user hello mocked implementation'));
authenticatorService.hello();
expect(userService.hello).toBeCalledTimes(1);
});
});
unit test result:
PASS stackoverflow/62967707/AuthenticatorService.test.js (12.636s)
62967707
✓ should pass (11ms)
console.log
initialize AuthenticatorService
at new AuthenticatorService (stackoverflow/62967707/AuthenticatorService.js:5:13)
console.log
user hello mocked implementation
at Object.<anonymous> (stackoverflow/62967707/AuthenticatorService.test.js:13:60)
-------------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
AuthenticatorService.js | 100 | 100 | 100 | 100 |
-------------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 14.703s
As you can see in the logs, the real constructor of UserService will not execute. And we mocked hello method of the UserService class's instance.
Related
Minimal, reproducible example
SomeClass.js
import Rx from "rxjs/Rx.js";
class SomeClass {
constructor() {
this.subject = new Rx.Subject();
}
}
SomeClass.test.js
import SomeClass from "./SomeClass.js";
let someClass = new SomeClass();
After running npm test, I get an error saying TypeError: Cannot read property 'Subject' of undefined. Rxjs has been installed in my project.
Any ideas?
Option 1. use Manual Mocks, mock rxjs/Rx.js module explicitly.
E.g.
SomeClass.js:
import Rx from 'rxjs/Rx.js';
export default class SomeClass {
constructor() {
this.subject = new Rx.Subject();
}
}
SomeClass.test.js:
import SomeClass from './SomeClass';
import Rx from 'rxjs/Rx.js';
jest.mock('rxjs/Rx.js', () => {
return { Subject: jest.fn() };
});
describe('65261235', () => {
it('should pass', () => {
let someClass = new SomeClass();
expect(Rx.Subject).toBeCalledTimes(1);
});
});
unit test result:
PASS examples/65261235/SomeClass.test.js
65261235
✓ should pass (2 ms)
--------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
SomeClass.js | 100 | 100 | 100 | 100 |
--------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 5.877 s
Option 2. enable automock for jestjs.
This option tells Jest that all imported modules in your tests should be mocked automatically. All modules used in your tests will have a replacement implementation, keeping the API surface.
I have a code like this
import fun from '../../../example';
export async function init (props:any){
if (fun()){
doSomething();
}
}
I'm creating unit tests for this code above, but I actually want to mock the implementation of fun in the file only as I can't alter fun in its original file
You can use jest.mock(moduleName, factory, options) to mock ../../../example module.
E.g.
index.ts:
import fun from './example';
export async function init(props: any) {
if (fun()) {
console.log('doSomething');
}
}
example.ts:
export default function fun() {
console.log('real implementation');
return false;
}
index.test.ts:
import { init } from './';
import fun from './example';
import { mocked } from 'ts-jest/utils';
jest.mock('./example', () => jest.fn());
describe('63166775', () => {
it('should pass', async () => {
expect(jest.isMockFunction(fun)).toBeTruthy();
const logSpy = jest.spyOn(console, 'log');
mocked(fun).mockReturnValueOnce(true);
await init({});
expect(logSpy).toBeCalledWith('doSomething');
expect(fun).toBeCalledTimes(1);
logSpy.mockRestore();
});
});
unit test result:
PASS stackoverflow/63166775/index.test.ts (13.298s)
63166775
✓ should pass (33ms)
console.log
doSomething
at CustomConsole.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 50 | 100 | 100 |
index.ts | 100 | 50 | 100 | 100 | 4
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 15.261s
I have a function, lets call it generateName, which as you’ve guessed it, generates a name. The problem is that a new name is generated each time time a test is ran.
In one of my tests, I assert that a function is called with an object containing this name. However, the name keeps on changing. I could just check that the object has property name, but I don’t really want to do that.
My idea is that I can mock the return value of the generateName function and do something like this
Import { generateName } from ‘libs/generateName’
jest.fn(generateName).mockResolvedValue ( ‘hello’ )
expect ( spy ).toHaveBeenCalledWith (
expect.objectContaining ( {
name: 'houses',
} )
)
You can use jest.mock(moduleName, factory, options) to mock libs/generateName module.
E.g.
generateName.ts:
export async function generateName() {
const name = Math.random() + '';
return name;
}
main.ts:
import { generateName } from './generateName';
export function main() {
return generateName();
}
main.test.ts:
import { main } from './main';
import { generateName } from './generateName';
jest.mock('./generateName', () => {
return {
generateName: jest.fn(),
};
});
describe('61350152', () => {
it('should pass', async () => {
(generateName as jest.MockedFunction<typeof generateName>).mockResolvedValueOnce('hello');
const actual = await main();
expect(actual).toBe('hello');
});
});
unit test results with coverage report:
PASS stackoverflow/61350152/main.test.ts (28.524s)
61350152
✓ should pass (6ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
main.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 31.98s
I have a function that looks roughly like this:
import {someLibraryMethod} from 'someLibrary';
function setFullWidth(el) {
someLibraryMethod(el);
el.setAttribute('width', '100%');
}
I'm testing this function by using Sinon to create a mock element that only supports setAttribute:
const fakeElement = {
setAttribute: sinon.spy()
};
setFullWidth(fakeElement);
assert(fakeElement.setAttribute.calledWithExactly('width', '100%'));
The problem is that someLibraryMethod assumets el is an HTML element and tries to call some other methods on it, such as hasAttribute. I could add these as stubs or spies to fakeElement too, but I was wondering if Sinon has a way to create an object that will spy on any method or property access on it?
Something like:
const fakeElement = sinon.spyOnAnyPropety();
fakeElement.setAttribute(); // valid
fakeElement.hasAttribute(); // valid
fakeElement.foo(); // also valid
// some way to check which methods were called
You can use sinon.stub(obj) to
Stubs all the object’s methods.
E.g.
index.ts:
export function setFullWidth(el) {
el.setAttribute('width', '100%');
el.hasAttribute('width');
el.foo();
}
index.spec.ts:
import { setFullWidth } from './';
import sinon, { SinonStubbedInstance } from 'sinon';
import { expect } from 'chai';
describe('58868361', () => {
const fakeElement = {
setAttribute(attr, value) {
console.log('setAttribute');
},
hasAttribute(attr) {
console.log('hasAttribute');
},
foo() {
console.log('foo');
},
};
afterEach(() => {
sinon.restore();
});
it('should spy all methods of el', () => {
const logSpy = sinon.spy(console, 'log');
const stub: SinonStubbedInstance<typeof fakeElement> = sinon.stub(fakeElement);
setFullWidth(fakeElement);
expect(stub.setAttribute.calledWithExactly('width', '100%')).to.be.true;
expect(stub.hasAttribute.calledWithExactly('width')).to.be.true;
expect(stub.foo.calledOnce).to.be.true;
expect(logSpy.callCount).to.be.equal(0);
logSpy.restore();
});
it('should restore to original methods of el', () => {
const logSpy = sinon.spy(console, 'log');
setFullWidth(fakeElement);
expect(logSpy.callCount).to.be.equal(3);
});
});
Unit test result with 100% coverage:
58868361
✓ should spy all methods of el
setAttribute
hasAttribute
foo
✓ should restore to original methods of el
2 passing (13ms)
---------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.spec.ts | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
---------------|----------|----------|----------|----------|-------------------|
Source code: https://github.com/mrdulin/mongoose5.x-lab/tree/master/src/stackoverflow/58868361
I started to write unit test cases using sinon and facing the following problem.
myfile.js
module.exports = class A{
constructor(classB_Obj){
this.classBobj = classB_Obj;
classBobj.someFunctionOfClassB(); // error coming here
}
doSomething(){
}
}
where class B is in
myfile2.js
module.exports = class B{
constructor(arg1, arg2){
this.arg1 = arg1;
this.arg2 = arg2;
}
someFunctionOfClassB(){
}
}
when I test class A and use sinon to stub Class B
const myfile2 = require('../myfile2').prototype;
const loggerStub = sinon.stub(myfile2, 'someFunctionOfClassB');
while executing it gives exception
classBobj.someFunctionOfClassB is not a function.
What is the correct way of stubbing it? I don't want to instantiate class B.
This has really nothing to do with stubbing.
You have to define this function as a static method to achieve this:
module.exports = class B{
constructor(arg1, arg2){
this.arg1 = arg1;
this.arg2 = arg2;
}
static someFunctionOfClassB(){
}
}
Then you can call the method on the class object.
When you write a normal class method you always have to instantiate the class before you can you use it on the instance:
const b = new class_Obj();
b.someFunctionOfClassB();
See also: Class vs. static method in JavaScript
Here is the unit test solution:
myfile.js:
module.exports = class A {
constructor(classB_Obj) {
this.classBobj = classB_Obj;
this.classBobj.someFunctionOfClassB();
}
doSomething() {}
};
myfile2.js:
module.exports = class B {
constructor(arg1, arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
}
someFunctionOfClassB() {}
};
myfile.test.js:
const A = require("./myfile");
const B = require("./myfile2");
const sinon = require("sinon");
describe("52559903", () => {
afterEach(() => {
sinon.restore();
});
it("should pass", () => {
const bStub = sinon.createStubInstance(B, {
someFunctionOfClassB: sinon.stub(),
});
new A(bStub);
sinon.assert.calledOnce(bStub.someFunctionOfClassB);
});
});
Unit test result with coverage report:
myfile
✓ should pass
1 passing (10ms)
----------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------------|----------|----------|----------|----------|-------------------|
All files | 87.5 | 100 | 57.14 | 87.5 | |
myfile.js | 100 | 100 | 50 | 100 | |
myfile.test.js | 100 | 100 | 100 | 100 | |
myfile2.js | 33.33 | 100 | 0 | 33.33 | 3,4 |
----------------|----------|----------|----------|----------|-------------------|
Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/52559903