[英]Returning a Promise from "describe" is not supported. Tests must be defined synchronously
Particular test passed but I am getting this.特定的测试通过了,但我得到了这个。
console.log node_modules/jest-jasmine2/build/jasmine/Env.js:502
●
Test suite failed to run
Returning a Promise from "describe" is not supported. Tests must be defined synchronously.
Returning a value from "describe" will fail the test in a future version of Jest.
> 4 | describe('handlers.getSemesters', async () => {
Here is complete test code这是完整的测试代码
describe('handlers.getSemesters', async () => {
it('should return an array of Semesters', async () => {
academicCalendarRequest.request = jest.fn();
academicCalendarRequest.request.mockReturnValue([
{
description: 'Semester1',
}
]);
const expected = [
{
description: 'Semester1',
},
];
const handlers = new Handlers();
const actual = await handlers.getSemesters();
expect(actual).toEqual(expected);
});
});
How can I fix it?我该如何解决?
Change改变
describe('handlers.getSemesters', async () => {
To至
describe('handlers.getSemesters', () => {
And then put asynchronous code inside of it
block然后将异步代码放入
it
的块内
it('should return an array of Semesters', async () => {
// ...
})
Async functions are syntactic sugar for returned promise chains, and Mocha's describe blocks do not support (as in waiting for resolution of) returned promises. 异步函数是返回的promise链的语法糖,而Mocha的describe块不支持(如等待解析)返回的promise。 Failing to warn about it is perhaps less helpful than it could be, but the behavior is pretty much expected under the current design.
没有提出警告可能没那么有用,但在目前的设计下,这种行为几乎是预料之中的。
In my case I needed to get async result for working within multiple tests.就我而言,我需要获得异步结果才能在多个测试中工作。 "Describe" has to be sync and we can't use nested test blocks:
“描述”必须是同步的,我们不能使用嵌套的测试块:
describe('Not valid nested structure', () => {
it('async parent', async () => {
it('child', () => {})
});
});
So my solution is:所以我的解决方案是:
describe('TaskService', () => {
let testTask;
// Common async test
it('should set test task', async () => {
testTask = await connection.create();
// you can add expect here
});
it('should update task', async () => {
testTask.status = null;
const updated = await connection.update(testTask);
expect(updated.status).toEqual(null);
});
it('other operation with testTask above', async () => {
// ..
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.