全球`之前'和`之前'为摩卡?

Fre*_*ind 63 javascript unit-testing mocha.js

我现在正在使用mocha进行javascript单元测试.

我有几个测试文件,每个文件都有一个beforebeforeEach,但它们完全相同.

我如何提供全局beforebeforeEach所有这些(或其中一些)?

AJc*_*dez 79

在test文件夹的根目录中,创建一个test/helper.js包含before和beforeEach 的全局测试助手

// globals
global.assert = require('assert');

// setup
before();
beforeEach();

// teardown
after();
afterEach();
Run Code Online (Sandbox Code Playgroud)

  • 它位于Mocha网站上:"请注意,您也可以选择任何文件并添加"root"级别挂钩,例如在describe()s之外添加beforeEach()然后回调将在任何测试用例之前运行,而不管文件是什么这是因为Mocha有一个没有名字的root套件." 请看[本节,底部](http://mochajs.org/#asynchronous-code). (27认同)
  • 您不应该明确要求它.事实上,它会抛出一个错误,因为之前,之前等等都不会存在于所需的上下文中.只要它包含在测试目录中,代码就应该在任何测试之前执行. (11认同)
  • 直接链接到@kamituel所说的内容:http://mochajs.org/#root-level-hooks (6认同)

Fra*_*cke 28

来自摩卡文档 ......

ROOT-LEVEL HOOKS

您也可以选择任何文件并添加"root"级别挂钩.例如,在所有describe()块之外添加beforeEach().这将导致beforeSach()的回调在任何测试用例之前运行,无论它存在于哪个文件中(这是因为Mocha有一个隐含的describe()块,称为"root套件"

describe()首先收集所有常规用品,然后再运行,这样可以保证首先调用它.

'use strict'
let run = false

beforeEach(function() {
    if ( run === true ) return
    console.log('GLOBAL ############################')
    run = true
});
Run Code Online (Sandbox Code Playgroud)

如果要在每次测试之前每次都看到它运行,请删除run-flag.

我把这个文件命名了test/_beforeAll.test.js.它无需在任何地方导入/需要,但文件名中的.test.(相应.spec.)很重要,以便你的测试人员选择它...


奖金追踪8-):使用mocha.opts\ o /

如果有东西,你真的只想在运行测试之前设置一次(无论哪个......),这mocha.opts是一个非常优雅的选择!- 只需require在文件中添加一个(是的,即使它对mocha的贡献很小,而且对你的测试设置有贡献).它将在以前可靠地运行一次:

在此输入图像描述

(在这个例子中,我检测到,如果单个测试或许多测试即将运行.在前一种情况下,我输出每个log.info(),而在完整运行中我将冗长减少到错误+警告...)

更新:

如果有人知道某种方式,要访问即将运行的mocha套件的一些基本属性once.js,我很想知道并添加到这里.(即我的suiteMode检测是糟糕的,如果有另一种方法可以检测,要运行多少次测试...)

  • 这个答案应该有更多的票! (3认同)

Mic*_*ley 27

声明一个beforebeforeEach在一个单独的文件中(我使用spec_helper.coffee)并要求它.

spec_helper.coffee

afterEach (done) ->
  async.parallel [
    (cb) -> Listing.remove {}, cb
    (cb) -> Server.remove {}, cb
  ], ->
    done()
Run Code Online (Sandbox Code Playgroud)

test_something.coffee

require './spec_helper'
Run Code Online (Sandbox Code Playgroud)

  • 你能解释一下,那里发生了什么? (8认同)

Cir*_*四事件 6

mochaHooksMocha 8 上的 root hook 插件最小示例

\n

该机制目前记录在: https: //mochajs.org/#root-hook-plugins

\n

它不适用于before,仅适用于beforeEach但是,因为before不在可用钩子列表中: https: //mochajs.org/#available-root-hooks

\n

这是一个演示:

\n

测试/global.js

\n
// Root hook.\nexports.mochaHooks = {\n  beforeEach(done) {\n    console.log(\'mochaHooks.beforeEach\');\n    done();\n  },\n};\n\n// Bonus: global fixture, runs once before everything.\nexports.mochaGlobalSetup = async function() {\n  console.log(\'mochaGlobalSetup\');\n};\n
Run Code Online (Sandbox Code Playgroud)\n

测试/mytest.js

\n
var assert = require(\'assert\');\n\ndescribe(\'describe0\', function() {\n  // Only runs before the current describe.\n  before(async () => {\n    console.error(\'before describe 0\');\n  });\n  beforeEach(async () => {\n    console.error(\'beforeEach describe 0\');\n  });\n  it(\'it 0 0\', function() {\n    assert.equal(0, 0);\n  });\n  it(\'it 0 1\', function() {\n    assert.equal(0, 0);\n  });\n\n  describe(\'describe 0 0\', function() {\n    before(async () => {\n      console.error(\'before describe 0 0\');\n    });\n    beforeEach(async () => {\n      console.error(\'beforeEach describe 0 0\');\n    });\n    it(\'it 0 0 0\', function() {\n      assert.equal(0, 0);\n    });\n    it(\'it 0 0 1\', function() {\n      assert.equal(0, 0);\n    });\n  });\n\n  describe(\'describe 0 1\', function() {\n    before(async () => {\n      console.error(\'before describe 0 1\');\n    });\n    beforeEach(async () => {\n      console.error(\'beforeEach describe 0 1\');\n    });\n    it(\'it 0 1 0\', function() {\n      assert.equal(0, 0);\n    });\n    it(\'it 0 1 1\', function() {\n      assert.equal(0, 0);\n    });\n  });\n});\n
Run Code Online (Sandbox Code Playgroud)\n

然后你启用该文件--require

\n
npx mocha --require test/global.js test/\n
Run Code Online (Sandbox Code Playgroud)\n

结果:

\n
mochaGlobalSetup\n\n\n  describe0\nbefore describe 0\nmochaHooks.beforeEach\nbeforeEach describe 0\n    \xe2\x9c\x93 it 0 0\nmochaHooks.beforeEach\nbeforeEach describe 0\n    \xe2\x9c\x93 it 0 1\n    describe 0 0\nbefore describe 0 0\nmochaHooks.beforeEach\nbeforeEach describe 0\nbeforeEach describe 0 0\n      \xe2\x9c\x93 it 0 0 0\nmochaHooks.beforeEach\nbeforeEach describe 0\nbeforeEach describe 0 0\n      \xe2\x9c\x93 it 0 0 1\n    describe 0 1\nbefore describe 0 1\nmochaHooks.beforeEach\nbeforeEach describe 0\nbeforeEach describe 0 1\n      \xe2\x9c\x93 it 0 1 0\nmochaHooks.beforeEach\nbeforeEach describe 0\nbeforeEach describe 0 1\n      \xe2\x9c\x93 it 0 1 1\n\n\n  6 passing (6ms)\n
Run Code Online (Sandbox Code Playgroud)\n

所以我们看到全局钩子在每个本地钩子之前运行beforeEach

\n

因为before我找不到比定义一个助手并从每个中调用它更好的解决方案before How can I make Mocha load a helper.js file that Defines global hooks or utility?

\n

在 mocha 8.3.2、节点 v14.16.0 上测试。

\n