簡體   English   中英

摩卡的全局“before”和“beforeEach”?

[英]Global `before` and `beforeEach` for mocha?

我現在正在使用 mocha 進行 javascript 單元測試。

我有幾個測試文件,每個文件都有一個beforebeforeEach ,但它們完全一樣。

我如何為所有這些(或其中一些)提供一個全局beforebeforeEach

在 test 文件夾的根目錄中,創建一個全局測試助手test/helper.js ,其中包含您的 before 和 beforeEach

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

// setup
before();
beforeEach();

// teardown
after();
afterEach();

來自摩卡文檔……

根級掛鈎

您還可以選擇任何文件並添加“根”級掛鈎。 例如,在所有 describe() 塊之外添加 beforeEach()。 這將導致對 beforeEach() 的回調在任何測試用例之前運行,無論它位於哪個文件中(這是因為 Mocha 有一個隱含的 describe() 塊,稱為“根套件”

首先收集所有常規的describe() -suites,然后才運行,這有點保證首先調用它。

'use strict'
let run = false

beforeEach(function() {
    if ( run === true ) return
    console.log('GLOBAL ############################')
    run = true
});

如果您想在每次測試之前每次都看到它運行,請刪除運行標志。

我將此文件命名為test/_beforeAll.test.js 它不需要在任何地方導入/需要,但.test. 文件名中的 (resp. .spec. ) 很重要,這樣您的測試運行程序就會發現它……


獎勵曲目 8-):使用mocha.opts \\o/

如果有東西,你真的只想在運行你的測試之前設置一次(不管是什么......), mocha.opts是一個非常優雅的選擇! – 只需在您的文件中添加一個require (是的,即使它對 mocha 的貢獻很小,但對您的測試設置卻有貢獻)。 它之前會可靠地運行一次:

在此處輸入圖片說明

(在這個例子中,我檢測,如果單個測試或多個測試即將運行。在前一種情況下,我輸出每個log.info() ,而在完整運行時,我將詳細程度減少到錯誤+警告......)

更新:

如果有人知道一種方法來訪問即將在once.js運行的 mocha 套件的一些基本屬性,我很想知道並在此處添加。 (即我的suiteMode很糟糕,如果有另一種檢測方式,要運行多少測試......)

在單獨的文件中聲明beforebeforeEach (我使用spec_helper.coffee )並要求它。

spec_helper.coffee

afterEach (done) ->
  async.parallel [
    (cb) -> Listing.remove {}, cb
    (cb) -> Server.remove {}, cb
  ], ->
    done()

test_something.coffee

require './spec_helper'

當我需要“模擬”依賴項之一使用的全局變量時,我遇到了類似的問題。

我為此使用了 .mocharc.js,因為在設置“mocha”環境時,該 JS 文件中的代碼將被執行一次。

示例 .mocharc.js:

global.usedVariable = "someDefinedValue";

/** other code to be executed when mocha env setup **/

module.exports = {};

這對我有用,但是這看起來很“骯臟”的方式來做到這一點。 如果您知道該代碼的更好位置,請發表評論:)

mochaHooks根掛鈎插件 Mocha 8 上的最小示例

該機制目前記錄在: https : //mochajs.org/#root-hook-plugins

它不適用於before ,僅適用於beforeEach但是,因為before不在可用鈎子列表中: https : beforeEach

這是一個演示:

測試/global.js

// Root hook.
exports.mochaHooks = {
  beforeEach(done) {
    console.log('mochaHooks.beforeEach');
    done();
  },
};

// Bonus: global fixture, runs once before everything.
exports.mochaGlobalSetup = async function() {
  console.log('mochaGlobalSetup');
};

測試/mytest.js

var assert = require('assert');

describe('describe0', function() {
  // Only runs before the current describe.
  before(async () => {
    console.error('before describe 0');
  });
  beforeEach(async () => {
    console.error('beforeEach describe 0');
  });
  it('it 0 0', function() {
    assert.equal(0, 0);
  });
  it('it 0 1', function() {
    assert.equal(0, 0);
  });

  describe('describe 0 0', function() {
    before(async () => {
      console.error('before describe 0 0');
    });
    beforeEach(async () => {
      console.error('beforeEach describe 0 0');
    });
    it('it 0 0 0', function() {
      assert.equal(0, 0);
    });
    it('it 0 0 1', function() {
      assert.equal(0, 0);
    });
  });

  describe('describe 0 1', function() {
    before(async () => {
      console.error('before describe 0 1');
    });
    beforeEach(async () => {
      console.error('beforeEach describe 0 1');
    });
    it('it 0 1 0', function() {
      assert.equal(0, 0);
    });
    it('it 0 1 1', function() {
      assert.equal(0, 0);
    });
  });
});

然后使用--require啟用該文件:

npx mocha --require test/global.js test/

結果:

mochaGlobalSetup


  describe0
before describe 0
mochaHooks.beforeEach
beforeEach describe 0
    ✓ it 0 0
mochaHooks.beforeEach
beforeEach describe 0
    ✓ it 0 1
    describe 0 0
before describe 0 0
mochaHooks.beforeEach
beforeEach describe 0
beforeEach describe 0 0
      ✓ it 0 0 0
mochaHooks.beforeEach
beforeEach describe 0
beforeEach describe 0 0
      ✓ it 0 0 1
    describe 0 1
before describe 0 1
mochaHooks.beforeEach
beforeEach describe 0
beforeEach describe 0 1
      ✓ it 0 1 0
mochaHooks.beforeEach
beforeEach describe 0
beforeEach describe 0 1
      ✓ it 0 1 1


  6 passing (6ms)

所以我們看到全局鈎子在每個本地beforeEach之前beforeEach

因為before我找不到比定義一個助手並從每個before調用它更好的解決方案: 如何讓 Mocha 加載一個定義全局鈎子或實用程序的 helper.js 文件?

在 mocha 8.3.2、Node v14.16.0 上測試。

使用模塊可以更輕松地為您的測試套件進行全局設置/拆卸。 下面是一個使用 RequireJS(AMD 模塊)的例子:

首先,讓我們使用全局設置/拆卸定義一個測試環境:

// test-env.js

define('test-env', [], function() {
  // One can store globals, which will be available within the
  // whole test suite.
  var my_global = true;

  before(function() {
    // global setup
  });
  return after(function() {
    // global teardown
  });
});

在我們的 JS 運行器中(包含在 mocha 的 HTML 運行器中,連同其他庫和測試文件,作為<script type="text/javascript">…</script> ,或者更好,作為外部 JS 文件):

require([
          // this is the important thing: require the test-env dependency first
          'test-env',

          // then, require the specs
          'some-test-file'
        ], function() {

  mocha.run();
});

some-test-file.js可以這樣實現:

// some-test-file.js

define(['unit-under-test'], function(UnitUnderTest) {
  return describe('Some unit under test', function() {
    before(function() {
      // locally "global" setup
    });

    beforeEach(function() {
    });

    afterEach(function() {
    });

    after(function() {
      // locally "global" teardown
    });

    it('exists', function() {
      // let's specify the unit under test
    });
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM