从 beforeEach 中提取函数并分配给 `this` 变量
Extract function from beforeEach and assign to `this` variable
我的beforeEach
设置非常冗长,不同文件之间的不同测试用例之间存在重用。
有没有一种方法可以提取 beforeEach
的主体并仍然分配给 this
变量?
示例:
describe(function () {
beforeEach(async function () {
this.a = a.new(...);
this.b = b.new(...);
this.c = c.new(...);
...
});
describe("a", function () {
it("calls a func", async function () {
await this.a.func();
});
});
});
并将 beforeEach
的主体提取到 setup
函数中(在第二个文件中):
describe(function () {
beforeEach(async function () {
[ this.a, this.b, this.c ] = setup();
});
...
});
//exampleSetup.js
async function setup() {
const a = a.new(...);
const b = b.new(...);
const c = c.new(...);
return [a, b, c];
}
module.exports = setup;
//tester.js
const setup = require("exampleSetup");
let a, b, c;
beforeEach(async function () {
[ a, b, c ] = setup()
...
});
我的beforeEach
设置非常冗长,不同文件之间的不同测试用例之间存在重用。
有没有一种方法可以提取 beforeEach
的主体并仍然分配给 this
变量?
示例:
describe(function () {
beforeEach(async function () {
this.a = a.new(...);
this.b = b.new(...);
this.c = c.new(...);
...
});
describe("a", function () {
it("calls a func", async function () {
await this.a.func();
});
});
});
并将 beforeEach
的主体提取到 setup
函数中(在第二个文件中):
describe(function () {
beforeEach(async function () {
[ this.a, this.b, this.c ] = setup();
});
...
});
//exampleSetup.js
async function setup() {
const a = a.new(...);
const b = b.new(...);
const c = c.new(...);
return [a, b, c];
}
module.exports = setup;
//tester.js
const setup = require("exampleSetup");
let a, b, c;
beforeEach(async function () {
[ a, b, c ] = setup()
...
});