我正在设置Jest以测试打字稿应用程序。
如何清除模拟函数并恢复其他测试的原始实现?
要模拟我使用的功能:jest.fn()。mockImplementationOnce()
到目前为止,我已经在beforeEach和afterEach中尝试了jest.clearAll()/ resetModules()/ resetAllMocks(),但是没有成功。
import App from './app';
import { DbService } from './lib/dbService';
describe('App', () => {
let dbService: DbService;
let app: App;
beforeEach(() => {
jest.clearAllMocks();
dbService = new DbService();
app = new App();
});
describe('getUsers', () => {
it('Should get an array users #1', () => {
expect(app).toBeInstanceOf(App);
const allUsers = app.getAllUsers();
expect(allUsers[0].id).toBeDefined();
});
it('should return an error #2', () => {
DbService.prototype.getAllUsers =
jest.fn().mockImplementationOnce(() => {
return new Error('No connection to DB');
});
expect(app.getAllUsers()).toEqual(new Error('No connection to DB'));
});
it('Should get an array users #3', () => {
expect(app).toBeInstanceOf(App);
const allUsers = app.getAllUsers();
expect(allUsers[0].id).toBeDefined();
});
});
});
import { DbService } from './lib/dbService';
export default class App {
private dbService: DbService;
constructor() {
this.dbService = new DbService();
}
getAllUsers() {
return this.dbService.getAllUsers();
}
}
let instance: DbService;
export class DbService {
constructor() {
if (!instance) {
instance = this;
}
return instance;
}
getAllUsers() {
return [
{id: 1, username: 'john'},
{id: 2, username: 'bill'}
]
}
}
我希望测试#3像测试#1一样通过,但实际上失败,并显示以下错误:
FAIL src/app.test.ts
App
getUsers
√ Should get an array users #1 (3ms)
√ should return an error #2 (1ms)
× Should get an array users #3 (1ms)
● App › getUsers › Should get an array users #3
TypeError: Cannot read property '0' of undefined
31 | expect(app).toBeInstanceOf(App);
32 | const allUsers = app.getAllUsers();
> 33 | expect(allUsers[0].id).toBeDefined();
| ^
34 | });
35 | });
36 | });
答案 0 :(得分:1)
Jest具有设置/拆卸功能:
https://flaviocopes.com/jest/#setup 要在所有测试运行之前执行一次操作,请使用beforeAll()函数:
beforeAll(() => {
//do something
})
要在每次测试运行之前执行某些操作,请使用beforeEach():
beforeEach(() => {
//do something
})
拆卸 就像您可以进行设置一样,您还可以在每次测试运行后执行一些操作:
afterEach(() => {
//do something
})
所有测试结束后:
afterAll(() => {
//do something
})
在设置功能中进行模拟并在拆解中还原
答案 1 :(得分:0)
我不确定这是否是实现这一目标的jest
方式,但是我认为您可以将原始方法实现保存在变量中,并在每次测试后重新设置该方法,以防在模型中被模拟。测试。
例如
describe('App', () => {
let dbService: DbService;
let app: App;
let originalGetAllUsersFn = DbService.prototype.getAllUsers;
//...
afterEach(() => {
// restore mocked method
DbService.prototype.getAllUsers = originalGetAllUsersFn;
});
});