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

问题描述 投票:0回答:2

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

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

我该如何修复它?

javascript node.js jestjs
2个回答
122
投票

改变

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

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

然后将异步代码放入

it
块中

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

3
投票

describe
必须同步,我们不能使用嵌套测试块:

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

所以我的解决方案是:

    describe('TaskService', () => {
        let testTask;
        
        // Common async test
        it('should set async 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 () => {
            // ..
        });
    });
© www.soinside.com 2019 - 2024. All rights reserved.