mocha/chai 的各种测试的可重用常量
reusable const for various tests with mocha/chai
我正在运行使用mocha/chai进行一系列测试。
我尽量使这些测试保持简单,以便于阅读。这就是我使用大量 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 一次,以便在各种测试中重新使用它?
如果每个 it() 都相同,您可以在 beforeEach 中声明内容。
示例:
describe('myTest', () => {
let foo;
beforeEach(() => {
foo = new Foo();
});
it('test 1', () => {
//do something with foo
});
it('test 2', () => {
//do something with foo
});
})
我正在运行使用mocha/chai进行一系列测试。
我尽量使这些测试保持简单,以便于阅读。这就是我使用大量 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 一次,以便在各种测试中重新使用它?
如果每个 it() 都相同,您可以在 beforeEach 中声明内容。
示例:
describe('myTest', () => {
let foo;
beforeEach(() => {
foo = new Foo();
});
it('test 1', () => {
//do something with foo
});
it('test 2', () => {
//do something with foo
});
})