如何在异步笑话测试中传入 done() 参数。每个案例

une*_*ast 5 jestjs ts-jest

我正在尝试编写一个测试异步方法的 jest 测试用例,我想传入done()参数,以便 jest 在结束测试之前等待它被触发,但是,我不确定把它放在哪里。

有任何想法吗?

const testcases = [
        [
            'Crew',
            [1,2,3],
            Enum.Level1
        ],
        [
            'Staff',
            [4,5,6],
            Enum.Level2
        ]
    ];
test.each(testcases )(
        'Should be able to load differing cases %p',
        (
            typeName: string,
            initalVals: string[],
            type: LevelType
        ) => {
            // some call that updates mobx store state

            when(
                () => mobxstoreProperty.length == initalVals.length,
                () => {
                    // my assertions

                    done();
                }
            );
        }
    );
Run Code Online (Sandbox Code Playgroud)

对于单个笑话测试,我可以这样做:

test('my single test', done => {
  // some call that updates mobx store state

     when(
       () => mobxstoreProperty.length == initalVals.length,
       () => {
         // my assertions
         done();
       }
    );
});
Run Code Online (Sandbox Code Playgroud)

只是不确定当我使用该test.each方法时该怎么做。

sch*_*mar 5

我使用命名参数,并且可以将该done()方法添加为最后一个函数参数。例如像这样:

const testcases: {
    typeName: string;
    initalVals: string[],
    type: LevelType
}[] = [
    {
        typeName: 'Crew',
        initalVals: [1,2,3],
        type: Enum.Level1
    },
    {
        typeName: 'Staff',
        initalVals: [4,5,6],
        type: Enum.Level2
    },
];
test.each(testcases)(
    'Should be able to load differing cases %p',
    // Must use `any` for `done`, as TypeScript infers the wrong type:
    ({typeName, initalVals, type}, done: any) => {
        // some call that updates mobx store state
        when(
            () => mobxstoreProperty.length == initalVals.length,
            () => {
                // my assertions

                done();
            }
        );
    }
);
Run Code Online (Sandbox Code Playgroud)

我还没有测试您是否可以将done()方法添加为带有数组参数的最后一个参数,但也许这也有效。