在继续进行描述之前,如何等待beforeEach执行?

问题描述 投票:-1回答:1

我有以下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。我该怎么办?

typescript async-await mocha
1个回答
0
投票

我想出了如何实现自己想要的。它涉及使用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函数的来源。

© www.soinside.com 2019 - 2024. All rights reserved.