【发布时间】:2017-12-10 00:51:23
【问题描述】:
我使用ts-jest 和jest 用打字稿编写我的测试文件。
我很困惑如何输入模块的模拟函数。
这是我的代码:
./module.ts:
import {IObj} from '../interfaces';
const obj: IObj = {
getMessage() {
return `Her name is ${this.genName()}, age is ${this.getAge()}`;
},
genName() {
return 'novaline';
},
getAge() {
return 26;
}
};
export default obj;
./module.test.ts:
import * as m from './module';
describe('mock function test suites', () => {
it('t-1', () => {
// I think the jest.Mock<string> type here is not correct.
m.genName: jest.Mock<string> = jest.fn(() => 'emilie');
expect(jest.isMockFunction(m.genName)).toBeTruthy();
expect(m.genName()).toBe('emilie');
expect(m.getMessage()).toEqual('Her name is emilie, age is 26');
expect(m.genName).toHaveBeenCalled();
});
});
如何输入模块m的模拟函数genName?
typescript在这里给我一个错误:
错误:(8, 7) TS2540: 无法分配给“genName”,因为它是常量或只读属性。
【问题讨论】:
标签: javascript typescript jestjs