在整个mocha测试之前运行异步代码

Tho*_*ggi 9 javascript asynchronous mocha.js node.js promise

我正在寻找一种在整个mocha测试之前运行异步代码的方法.

下面是一个测试示例,该测试使用参数和期望数组以及对此数组中所有项的循环来生成函数断言.

var assert = require('assert')

/* global describe, it*/

var fn = function (value) {
  return value + ' ' + 'pancake'
}

var tests = [
  {
    'arg': 'kitty',
    'expect': 'kitty pancake'
  },
  {
    'arg': 'doggy',
    'expect': 'doggy pancake'
  },
]

describe('example', function () {
  tests.forEach(function (test) {
    it('should return ' + test.expect, function (){
      var value = fn(test.arg)
      assert.equal(value, test.expect)
    })
  })
})
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是,如果测试值来自一个promise,它将如何工作,如下所示:

var assert = require('assert')
var Promise = require('bluebird')

/* global describe, it*/

var fn = function (value) {
  return value + ' ' + 'pancake'
}

function getTests () {
  return Promise.resolve('kitty pancake')
  .delay(500)
  .then(function (value) {
    return [
      {
        'arg': 'kitty',
        'expect': value
      },
      {
        'arg': 'doggy',
        'expect': 'doggy pancake'
      }
    ]
  })
}

getTests().then(function (tests) {
  describe('example', function () {
    tests.forEach(function (test) {
      it('should return ' + test.expect, function (){
        var value = fn(test.arg)
        assert.equal(value, test.expect)
      })
    })
  })  
})
Run Code Online (Sandbox Code Playgroud)

还尝试过:

describe('example', function () {
  getTests().then(function (tests) {
    tests.forEach(function (test) {
      it('should return ' + test.expect, function (){
        var value = fn(test.arg)
        assert.equal(value, test.expect)
      })
    })
  })
})
Run Code Online (Sandbox Code Playgroud)

但是在此示例中,没有任何测试运行,因为mocha不识别describe语句,因为它在promise中.

before/ beforeEach无论如何都不会做任何事情来帮助进行格式测试,除非是一个beforeTest能够为mocha提供知识的钩子,知道在整个测试之前需要运行异步操作.

Dan*_*rez 1

我不确定是否有任何简单的方法可以做到这一点,但您可以尝试以编程方式运行 Mocha

这是一个有点肮脏的版本,只是为了展示这个想法。

data.js

var Promise = require('bluebird')

module.exports.tests = []

function getTests () {
  return Promise.resolve('kitty pancake')
  .delay(500)
  .then(function (value) {
     module.exports.tests = [
      {
        'arg': 'kitty',
        'expect': value
      },
      {
        'arg': 'doggy',
        'expect': 'doggy pancake'
      }
    ]
  })
}

module.exports.getTests = getTests
Run Code Online (Sandbox Code Playgroud)

test-launcher.js

var Mocha = require('mocha'),
    fs = require('fs'),
    path = require('path')

// First, you need to instantiate a Mocha instance.
var mocha = new Mocha()

// Then, you need to use the method "addFile" on the mocha
// object for each file.

// Here is an example:
fs.readdirSync('test').filter(function(file){
    // Only keep the .js files
    return file.substr(-3) === '.js'

}).forEach(function(file){
    // Use the method "addFile" to add the file to mocha
    mocha.addFile(
        path.join('test', file)
    )
})

// make sure your tests are loaded before running the tests
require('./data').getTests().then(function () {

  // Now, you can run the tests.
  mocha.run(function(failures){
    process.on('exit', function () {
      process.exit(failures)
    })
  })
})
Run Code Online (Sandbox Code Playgroud)

test/index.js

var assert = require('assert')

var tests = require('../data').tests

var fn = function (value) {
  return value + ' ' + 'pancake'
}

describe('test', function () {
  describe('example', function () {
    tests.forEach(function (test) {
      it('should return ' + test.expect, function (){
        var value = fn(test.arg)
        assert.equal(value, test.expect)
      })
    })
  })
})
Run Code Online (Sandbox Code Playgroud)

然后你可以通过跑步来休息test-launcher.js