我有以下TypeScript文件:
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
测试套件中。但事实并非如此,我收到此错误:
TypeError:无法读取未定义的属性'address'
我以为摩卡咖啡会先等待beforeEach
执行,然后再将值传递到shouldBehaveLikeBar
。我该怎么办?
我想出了如何实现自己想要的。它涉及使用delayed root suite功能。
这是foo.ts
文件的重写方式:
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
函数的来源。