简体   繁体   English

如何在`before`钩子之前运行beforeEach

[英]how to run beforeEach before `before` hook

I have the following code.我有以下代码。

describe("mytest", () => {

   beforeEach(() => {
      console.log("first");
   })

   context("first context", () => {
      before(() => {
         console.log("second");
      })

   })
 
})

What I want to achieve is that I want first to get printed first and then second .我想要实现的是我first打印, second再打印。 It seems like the problem is before hook.似乎问题出before钩子之前。 beforeEach runs before each test case, but not before before hook. beforeEach在每个测试用例之前运行,但不是在钩子before Any ideas?有任何想法吗?

Please specify your DSL in your question.请在您的问题中指定您的 DSL。 I am assuming you are using BDD .我假设您正在使用BDD I am not sure how Jest is used here, if this is a Mocha unit test?如果这是 Mocha 单元测试,我不确定 Jest 是如何在这里使用的?

In order to run your suite, you will need tests that actually get evaluated.为了运行您的套件,您将需要实际得到评估的测试。

In the case of the inner-context, you are calling before prior to calling beforeEach .在内部上下文的情况下,您在调用beforeEach before调用 before。 To reverse your call, you could change before to beforeEach inside the context .要撤消您的通话,您可以在context中将before更改为beforeEach

 let step = 0; mocha.setup('bdd'); chai.should(); describe("mytest", () => { beforeEach(() => { console.log(`[before each ${step}] first`); step++; }); it('outer', () => { [].length.should.equal(0); }); context("first context", () => { beforeEach(() => { console.log(`[before ${step}] second`); }); it('inner', () => { [].length.should.equal(0); }); }); }); mocha.run();
 <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.2.5/mocha.css"> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.2.5/mocha.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.2.0/chai.js"></script> <div id="mocha"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM