Global `before` and `beforeEach` for mocha?

后端 未结 5 962
长情又很酷
长情又很酷 2021-02-01 00:38

I\'m using mocha for javascript unit-testing now.

I have several test files, each file has a before and beforeEach, but they are exactly the s

相关标签:
5条回答
  • 2021-02-01 00:44

    I've had similar issue when I needed to "mock" global variables used by one of dependencies.

    I used .mocharc.js for that, since code in that JS file is being executed once when "mocha" environment is being setup.

    Example .mocharc.js:

    global.usedVariable = "someDefinedValue";
    
    /** other code to be executed when mocha env setup **/
    
    module.exports = {};
    

    This worked for me, nevertheless this looks quite "dirty" way to do that. Please, comment if you know a better place for that code :)

    0 讨论(0)
  • 2021-02-01 00:54

    Declare a before or beforeEach in a separate file (I use spec_helper.coffee) and require it.

    spec_helper.coffee

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

    test_something.coffee

    require './spec_helper'
    
    0 讨论(0)
  • 2021-02-01 00:59

    from the mocha documentation…

    ROOT-LEVEL HOOKS

    You may also pick any file and add “root”-level hooks. For example, add beforeEach() outside of all describe() blocks. This will cause the callback to beforeEach() to run before any test case, regardless of the file it lives in (this is because Mocha has an implied describe() block, called the “root suite

    All regular describe()-suites are first collected and only then run, this kinda guarantees this being called first.

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

    Remove the run-flag, if you want to see it run each time, before every test.

    I named this file test/_beforeAll.test.js. It has no need to be imported/required anywhere, but the .test. (resp. .spec.) in the filename is important, so that your testrunner picks it up…


    bonus track 8-): using mocha.opts \o/

    If there's stuff, you truly only want to set up once before running your tests (regardless which ones...), mocha.opts is a surprisingly elegant option! – Just add a require to your file (yes, even if it contributes little to mocha, but rather to your test setup). It will run reliably once before:

    ( in this example I detect, if a single test or many tests are about to run. In the former case I output every log.info(), while on a full run I reduce verbosity to error+warn... )

    Update:

    If someone knows a way, to access some basic properties of the mocha suite that is about to be run in once.js, I would love to know and add here. (i.e. my suiteMode-detection is lousy, if there was another way to detect, how many tests are to be run…)

    0 讨论(0)
  • 2021-02-01 01:00

    The use of a modules can make it easier to have a global setup/teardown for your test suite. Here is an example using RequireJS (AMD modules):

    First, let's define a test environment with our global setup/teardown:

    // 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
      });
    });
    

    In our JS runner (included in mocha's HTML runner, along the other libs and test files, as a <script type="text/javascript">…</script>, or better, as an external JS file):

    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 could be implemented like this:

    // 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
        });
      });
    });
    
    0 讨论(0)
  • 2021-02-01 01:06

    In the root of the test folder, create a global test helper test/helper.js which has your before and beforeEach

    // globals
    global.assert = require('assert');
    
    // setup
    before();
    beforeEach();
    
    // teardown
    after();
    afterEach();
    
    0 讨论(0)
提交回复
热议问题