繁体   English   中英

不支持从“描述”返回 Promise。 测试必须同步定义

[英]Returning a Promise from "describe" is not supported. Tests must be defined synchronously

特定的测试通过了,但我得到了这个。

    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 () => {

这是完整的测试代码

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);
      });
    });

我该如何解决?

改变

describe('handlers.getSemesters', async () => {

describe('handlers.getSemesters', () => {

然后将异步代码放入it的块内

it('should return an array of Semesters', async () => {
  // ...
})

异步函数是返回的promise链的语法糖,而Mocha的describe块不支持(如等待解析)返回的promise。 没有提出警告可能没那么有用,但在目前的设计下,这种行为几乎是预料之中的。

就我而言,我需要获得异步结果才能在多个测试中工作。 “描述”必须是同步的,我们不能使用嵌套的测试块:

describe('Not valid nested structure', () => {
    it('async parent', async () => {
        it('child', () => {})
    });
});

所以我的解决方案是:

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM