How to jest spyOn a commonJS default export - javascript

Is it possible to jest.spyOn a default export so that I can call the mockImplementation method to change what the function does before each test?
// code.js
module.exports = () => {
// some irrelevant code I want to rewrite with a mock
};
// test
const code = require('./code.js');
const mockCode = jest.spyOn(code, 'default'); // this line doesn't work with the error: "Cannot spy the default property because it is not a function; undefined given instead"
it('some test', async () => {
mockCode.mockImplementationOnce(() => { console.log('test') });
});
I've also tried to use jest.mock() unsuccessfully:
const code = require('./code');
jest.mock('./code',
() => () => {
console.log('test');
}
);
it('some test', async () => {
code.mockImplementationOnce(() => { console.log('test2') }); // error
});

Related

Argument of type '"openURL"' is not assignable to parameter of type 'never' using Jest

I am beginner in Jest and these are classes defined.
APIService class is not exported; only openURL function is defined.
APIService.ts
export const openURL = async (openURL : string) => {
await Linking.openURL(openURL );
};
RegistrationPage.tsx
import{openURL} from '../APIService';
RegistrationPage.test.ts
test('should call function openURL with empty value', async () => {
const url = '';
const mockOpenURL = jest.fn();
mockOpenURL .mockImplementation(() => Promise.resolve(''));
const openURLSpy = jest.spyOn(openURL, 'openURL');
const mockURL = await openURL(url);
expect(mockOpenURL).toBeCalled();
expect(mockURL).toEqual(url);
expect(mockOpenURL).toHaveBeenCalledWith(url);
openURLSpy.mockRestore();
});
After writing this function as per my understating may be it has loopholes not having properly mocked or spyed
running it causing an error for Argument of type '"openURL"' is not assignable to parameter of type 'never' using Jest
suggestions to improve this testcase will be helpful.
Just defining a variable locally like const mockOpenURL = jest.fn() cannot mock anything.
jest.spyOn(openURL, 'openURL') fails because openURL is a function and there's there's no openURL.openURL property.
It's APIService module that needs to be mocked:
import{openURL} from '../APIService';
jest.mock('../APIService', () => {
return {
__esModule: true,
openURL: jest.fn()
}
};
...
openURL.mockResolvedValue();
// code that calls openURL
Thank you #
jest.mock('../APIService.ts', () => {
return {
__esModule: true,
openURL: jest.fn(),
};
});
test('openURL function to be called with', async () => {
const openURL = 'www.google.com';
const openURL = jest.fn();
openURL.mockReturnValue(openURL );
});

How to check if one function triggers another function with `Mocha`, `Chai`, `Sinon`

How to create test with Mocha, Chai, Sinon to check if one function triggers another function.
I would like to check if funcToTrigger triggers funcToSpy
import { expect } from 'chai';
import sinon from 'sinon';
it('one function should trigger other function', () => {
const funcToSpy = () => {
console.log('I should be called');
};
const funcToTrigger = () => {
funcToSpy();
};
const spyFunc = sinon.spy(funcToSpy);
funcToTrigger();
expect(spyFunc.called).to.be.true;
});
When I test only one function it works fine:
it('function should be called', () => {
const funcToSpy = () => {
console.log('I should be called');
};
const spyFunc = sinon.spy(funcToSpy);
spyFunc();
expect(spyFunc.called).to.be.true;
});
Based on documentation:
var spy = sinon.spy(myFunc);
Wraps the function in a spy. You can pass this spy where the original
function would otherwise be passed when you need to verify how the
function is being used.
Usage examples:
import { expect } from 'chai';
import sinon from 'sinon';
it('use Object', () => {
const Test = {
funcToSpy: () => {
console.log('I should be called');
},
};
const funcToTrigger = () => {
Test.funcToSpy();
};
const spyFunc = sinon.spy(Test, 'funcToSpy');
funcToTrigger();
expect(spyFunc.called).to.be.true;
});
it('use Function', () => {
const funcToSpy = () => {
console.log('I should be called');
};
const spyFunc = sinon.spy(funcToSpy);
const funcToTrigger = () => {
spyFunc();
};
funcToTrigger();
expect(spyFunc.called).to.be.true;
});
it('use Function Argument', () => {
const funcToSpy = () => {
console.log('I should be called');
};
const funcToTrigger = (funcToSpy) => {
funcToSpy();
};
const spyFunc = sinon.spy(funcToSpy);
funcToTrigger(spyFunc);
expect(spyFunc.called).to.be.true;
});
Result:
$ npx mocha index.spec.js
I should be called
✓ use Object
I should be called
✓ use Function
I should be called
✓ use Function Argument
3 passing (3ms)
$
Your test fail because: funcToTrigger has defined and always calls the original funcToSpy.
In the 'use Object' case, funcToTrigger calls method inside object Test, which has been replaced by spy, which is wrapping funcToSpy.
In the 'use Function' case, funcToTrigger calls spy directly, and the spy is wrapping funcToSpy.
In the 'use Function Argument' case, funcToTrigger calls first argument which is a spy, which is wrapping funcToSpy.

How to undo mocked require in jest?

I am mocking a library by doing this:
let helperFn;
let mock;
beforeEach(() => {
mock = jest.fn();
require('./helperFn').default = mock;
})
If I do this in a test, does it mean that from now on within the whole test suite that default function of helperFn will be associated with that mock?
In the Jest documentations I see how to reset the mock, but I don't see how to remove the mock from a required function. I am concerned that from that test on, all the calls into helperFn.default will see that mock.
ES6 modules
Here is an ES6 example:
helperFn.js
export default () => 'original';
code.js
import helperFn from './helperFn';
export const func = () => helperFn();
code.test.js
import * as helperFnModule from './helperFn';
import { func } from './code';
describe('helperFn mocked', () => {
let mock;
beforeEach(() => {
mock = jest.spyOn(helperFnModule, 'default');
mock.mockReturnValue('mocked');
});
afterEach(() => {
mock.mockRestore();
});
test('func', () => {
expect(func()).toBe('mocked'); // Success!
});
});
describe('helperFn not mocked', () => {
test('func', () => {
expect(func()).toBe('original'); // Success!
});
});
Details
Since ES6 imports are live views of the module exports, it is easy to mock an export and then restore it afterwards.
Node.js modules
Here is a Node.js example:
helperFn.js
exports.default = () => 'original';
code.js
const helperFn = require('./helperFn').default;
exports.func = () => helperFn();
code.test.js
describe('helperFn mocked', () => {
beforeEach(() => {
const helperFnModule = require('./helperFn');
helperFnModule.default = jest.fn(() => 'mocked');
});
afterEach(() => {
jest.resetModules();
});
test('func', () => {
const { func } = require('./code');
expect(func()).toBe('mocked'); // Success!
});
});
describe('helperFn not mocked', () => {
test('func', () => {
const { func } = require('./code');
expect(func()).toBe('original'); // Success!
});
});
Details
The default export gets remembered by code.js when it runs, so changing the default export of helperFn.js doesn't affect func once code.js is required. Jest also caches modules and returns the same module for multiple require calls unless jest.resetModules is called.
So for Node.js modules it is often easiest to require code within the test itself and use jest.resetModules to reset any mocking.

sinon mock not catching calls

I am having a hard time understanding what I am doing wrong.
I have a JS class as such:
export default class A {
constructor(repository) {
this._repository = repository;
}
async process(date) {
// ...
this._repository.writeToTable(entry);
}
}
and I am attempting to write a test that mocks the repository using sinon.mock
This is what I have so far:
describe('A', () => {
describe('#process(date)', () => {
it('should work', async () => {
const repository = { writeToTable: () => {} };
const mock = sinon.mock(repository);
const a = new A(repository);
await a.process('2017-06-16');
mock.expects('writeToTable').once();
mock.verify();
});
});
});
but it always fails saying that
ExpectationError: Expected writeToTable([...]) once (never called)
I've checked (added a console.log) and it is calling the object I defined on the test.
I ran this locally and read the documentation on sinonjs.org and you seem to be doing everything right.
I tried re-writing your example using a spy and ended up with something like this to get a passing test:
import sinon from "sinon";
import { expect } from "chai";
import A from "./index.js";
describe("A", () => {
describe("#process(date)", () => {
it("should work", async () => {
const repository = { writeToTable: sinon.spy() };
const a = new A(repository);
await a.process("2017-06-16");
expect(repository.writeToTable.calledOnce).to.be.true;
});
});
});

How do I make Sinon.JS callCount increment

So I've got a Chai/Mocha/Sinon test like this:
import sinon from 'sinon'
describe(`My Test`, () => {
it(`should track the number of calls`, () => {
function testMe() {
console.log(`test me`)
}
const spy = sinon.spy(testMe)
testMe()
console.log(spy.getCalls())
console.log(spy.callCount)
})
})
When the test runs, the following is logged:
test me
[]
0
This is baffling. What am I doing wrong?
If you want spy on regular functions, the only way you can track calls to that function is by calling the spy:
it(`should track the number of calls`, () => {
function testMe() {
console.log(`test me`)
}
const spy = sinon.spy(testMe)
spy()
console.log(spy.getCalls())
console.log(spy.callCount)
})
If testMe would have been a property of an object (or a method of a class), you could call the original, because in that situation Sinon can replace the original with the spied-on version. For instance:
describe(`My Test`, () => {
it(`should track the number of calls`, () => {
const obj = {
testMe() {
console.log(`test me`)
}
};
const spy = sinon.spy(obj, 'testMe')
obj.testMe();
console.log(spy.callCount)
})
})

Categories