在component.js中,需要mock一行:
const accessValue = await objValueClass.getValue();
在运行 component.test.js 时,inconsole.log
accessValue
即将到来undefined
组件.test.js
describe('** Handler unit tests **', () => {
test('test 1', async () => {
const { ValueClass } = require('../../ValueClass');
jest.mock('../../ValueClass');
const objValueClass = new ValueClass();
objValueClass.getValue.mockResolvedValue('abcd');
const { component } = require('../../component');
const res = await component();
});
});
组件.js
const { ValueClass } = require('../../ValueClass');
const component = async () => {
const objValueClass = new ValueClass();
const accessValue = await objValueClass.getValue();
console.log('accessValue###', accessValue);
}
module.exports = component;
值类.js
class ValueClass {
async getValue() {
const a = 'a';
const b = 'b';
return a+b;
}
}
module.exports = {
ValueClass
};