在继续描述中的流程之前,如何等待beforeEach执行? [英] How to wait for beforeEach to execute before continuing with the flow in describe?

查看:101
本文介绍了在继续描述中的流程之前,如何等待beforeEach执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆