在继续描述中的流程之前,如何等待beforeEach执行? [英] How to wait for beforeEach to execute before continuing with the flow in describe?
问题描述
我有以下TypeScript文件:
I have the following TypeScript files:
foo.ts
import { shouldBehaveLikeBar } from "./bar";
describe("Foo", function() {
const wallets: Wallet[];
beforeEach(async function() {
wallets = (await ethers.getSigners()) as Wallet[];
});
shouldBehaveLikeBar(wallets);
});
bar.ts
export function shouldBehaveLikeBar(wallets: Wallet[]) {
describe("Bar", function() {
it("should test something", async function() {
const something = await callFunctionThatNeeds(wallets[0].address);
expect(something).to.equal(true);
});
});
}
基本上,我需要wallets[0]
才能存在于should test something
测试套件中.但是没有,我收到了这个错误:
Basically, I need wallets[0]
to exist in the should test something
test suite. But it doesn't, I'm getting this error:
TypeError:无法读取未定义的属性地址"
TypeError: Cannot read property 'address' of undefined
我认为在将值传递到shouldBehaveLikeBar
之前,mocha将等待beforeEach
执行.我该怎么办?
I thought that mocha would wait for beforeEach
to execute before passing the values onto shouldBehaveLikeBar
. How can I do this?
推荐答案
我想出了如何实现自己想要的东西.它涉及使用延迟的根套件功能.
I figured out how to achieve what I want. It involves using the delayed root suite functionality.
这是应该重写foo.ts
文件的方式:
This is how the foo.ts
file should be rewritten:
import { shouldBehaveLikeBar } from "./bar";
setTimeout(async function() {
const wallets: Wallet[] = (await ethers.getSigners()) as Wallet[];
describe("Foo", function() {
shouldBehaveLikeBar(wallets);
});
run();
}, 1000);
还有一个注意事项:确保您拥有 @ types/mocha 软件包,因此TypeScript编译器可以推断run
函数的来源.
Also a side note: make sure that you have the @types/mocha package installed, so the TypeScript compiler can infer the provenience of the run
function.
这篇关于在继续描述中的流程之前,如何等待beforeEach执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!