测试函数不能同时接受 'done' 回调
Test functions cannot both take a 'done' callback
我正在尝试使用 nestjs 创建一个简单的测试,但我遇到了这个错误
Test functions cannot both take a 'done' callback and return something. Either use a 'done' callback, or return a promise.
Returned value: Promise {}
单元测试这么简单,我用done()的时候却报错了;
it('throws an error if user signs up with email that is in use', async (done) => {
fakeUsersService.find = () => Promise.resolve([{ id: 1, email: 'a', password: '1' } as User]);
try {
await service.signup('asdf@asdf.com', 'asdf');
} catch (err) {
done();
}
});
您正在合并 Async/Await 和完成。
要么使用 asnyc/await,要么完成。
it('throws an error if user signs up with email that is in use', async () => {
try {
await service();
expect(...);
} catch (err) {
}
});
或使用完成格式
it('throws an error if user signs up with email that is in use', (done) => {
...
service()
.then( ...) {}
.catch( ...) {}
}
done();
});
此外,如果您想同时使用两者,您可以将当前版本的 jest 降级到:26.6.3。
对我来说工作得很好,我正在使用 async + done
对于 jest 的最后一个版本,你不能使用 `async/await ,promise and done together.
解决方案是
it("throws an error if user sings up with email that is in use", async () => {
fakeUsersService.find = () =>
Promise.resolve([{ id: 1, email: "a", password: "1" } as User]);
await expect(service.signup("asdf@asdf.com", "asdf")).rejects.toThrow(
BadRequestException
);
});
根据你的听力异常改变BadRequestException
v27之前jest默认使用jest-jasmine2
对于27版本,jest使用jest-circus不支持done回调
因此您需要更改默认的 testRunner。
覆盖 react-app-rewired 对我有用
// config-overrides.js
module.exports.jest = (config) => {
config.testRunner = 'jest-jasmine2';
return config;
};
我正在尝试使用 nestjs 创建一个简单的测试,但我遇到了这个错误
Test functions cannot both take a 'done' callback and return something. Either use a 'done' callback, or return a promise.
Returned value: Promise {}
单元测试这么简单,我用done()的时候却报错了;
it('throws an error if user signs up with email that is in use', async (done) => {
fakeUsersService.find = () => Promise.resolve([{ id: 1, email: 'a', password: '1' } as User]);
try {
await service.signup('asdf@asdf.com', 'asdf');
} catch (err) {
done();
}
});
您正在合并 Async/Await 和完成。
要么使用 asnyc/await,要么完成。
it('throws an error if user signs up with email that is in use', async () => {
try {
await service();
expect(...);
} catch (err) {
}
});
或使用完成格式
it('throws an error if user signs up with email that is in use', (done) => {
...
service()
.then( ...) {}
.catch( ...) {}
}
done();
});
此外,如果您想同时使用两者,您可以将当前版本的 jest 降级到:26.6.3。 对我来说工作得很好,我正在使用 async + done
对于 jest 的最后一个版本,你不能使用 `async/await ,promise and done together.
解决方案是
it("throws an error if user sings up with email that is in use", async () => {
fakeUsersService.find = () =>
Promise.resolve([{ id: 1, email: "a", password: "1" } as User]);
await expect(service.signup("asdf@asdf.com", "asdf")).rejects.toThrow(
BadRequestException
);
});
根据你的听力异常改变BadRequestException
v27之前jest默认使用jest-jasmine2
对于27版本,jest使用jest-circus不支持done回调
因此您需要更改默认的 testRunner。
覆盖 react-app-rewired 对我有用
// config-overrides.js
module.exports.jest = (config) => {
config.testRunner = 'jest-jasmine2';
return config;
};