可重复使用的 CONST 用于使用摩卡/柴进行各种测试



我正在使用摩卡/柴运行一系列测试。

我尽量使这些测试尽可能简单,以便于阅读。这就是为什么我使用了很多 it(( 声明。

对于这些测试中的每一个,我都使用相同的常量。而不是每次都重新声明它,我想只声明一次并完成它。

describe('#getLastAchievements', async function (): Promise<void> {
        it("should return an array", async function (): Promise<void> {
            const lastAch: any[] = await achievementsServiceFunctions.getLastAchievements(idAdmin, 3);
            expect(lastAch).not.to.be.equal(null);
            expect(lastAch).to.be.an('array');
        });
        it('should not be empty', async function (): Promise<void> {
            const lastAch: Object[] = await achievementsServiceFunctions.getLastAchievements(idAdmin, 3);
            expect(lastAch.length).to.be.above(0);
        });

我尝试以各种方式声明我的 const,但每次测试不运行或 conts 未定义。这是我尝试过的:

-在 it(( 之前声明它

-在 before(( 函数中声明它

-在匿名函数中声明它,然后将 it(( 包含在此函数中

-在 describe(( 函数之外声明它

有没有办法只声明一次这个 const 以将其重用于各种测试?

你可以在 beforeEach 中声明内容,如果它们对于每个 it(( 都相同。

例:

describe('myTest', () => {
    let foo;
    beforeEach(() => {
        foo = new Foo();
    });
    it('test 1', () => {
        //do something with foo
    });
    it('test 2', () => {
        //do something with foo
    });
})

最新更新