问题
In our node CLI we have a simple method:
'use strict';
const ora = require('ora');
module.exports = function startSpinner({ textOnStart, color, spinnerType }) {
const spinner = ora({
text: textOnStart,
color: color || 'cyan',
spinner: spinnerType || ''
}).start();
};
We try to use jest to test this method. We have two tests to achieve:
- Testing that ora has been called with proper object argument
- Testing that the method
start()
was called afterward
That being said we cannot achieve to mock ora
module properly.
ora
is a third party that is basically constructed as follow:
class Ora {
constructor(options){}
start(){ }
}
const oraFactory = function (opts) {
return new Ora(opts);
};
module.exports = oraFactory;
module.exports.default = oraFactory;
We are looking for a way to mock ora.
We tried to use auto mock:
const ora = require('ora');
jest.mock('ora');
const startSpinner = require('./startSpinner');
describe('startSpinner', () => {
beforeEach(() => {
startSpinner({});
});
describe('ora', () => {
it('should call ora', () => {
expect(ora).toHaveBeenCalled();
});
it('should call ora start', () => {
expect(ora.start).toHaveBeenCalled();
});
});
});
But both tests fail with respectively:
Matcher error: received value must be a mock or spy function
Received has type: function Received has value: [Function oraFactory]
and
Matcher error: received value must be a mock or spy function
Received has value: undefined
We tried to use a custom mock:
const ora = require('ora');
jest.mock('ora', () => {
return jest.fn().mockImplementation(() => {
return { start: jest.fn() };
});
});
and it ends up with the same exact result.
We even tried to convert our test to typescript and then use:
import * as ora from 'ora';
const startMock = jest.fn();
jest.mock('ora', () => {
return jest.fn().mockImplementation(() => {
return { start: startMock };
});
});
Then we were able to test successfuly that ora
was called. But we ended up with an error for expect(ora.start).toHaveBeenCalled();
or even expect((ora as any).start).toHaveBeenCalled();
:
error TS2339: Property 'start' does not exist on type 'typeof import("/Users/Dev/cli/node_modules/ora/index")'.
Surely caused by the fact the type definition of imported ora
is export default function ora(options?: Options | string): Ora;
How to then mock a third party like ora in jest's node test environnement?
回答1:
You've got a couple of options:
You can mock ora
like this:
jest.mock('ora', () => {
const start = jest.fn();
const result = { start };
return jest.fn(() => result);
});
...and then call ora
to get the object it returns (since it always returns the same object) and use that object to access start
:
it('should call ora start', () => {
const result = ora();
expect(result.start).toHaveBeenCalled(); // Success!
});
Or if you want you can attach the start
mock as a property to the ora
mock as an easy way to access it during your tests like this:
const ora = require('ora');
jest.mock('ora', () => {
const start = jest.fn();
const result = { start };
const ora = jest.fn(() => result);
ora.start = start; // attach the start mock to ora
return ora;
});
const startSpinner = require('./startSpinner');
describe('startSpinner', () => {
beforeEach(() => {
startSpinner({});
});
describe('ora', () => {
it('should call ora', () => {
expect(ora).toHaveBeenCalled(); // Success!
});
it('should call ora start', () => {
expect(ora.start).toHaveBeenCalled(); // Success!
});
});
});
来源:https://stackoverflow.com/questions/55437111/mock-node-external-module-default-with-chained-method-using-jest