之前整个摩卡试运行异步code [英] Run async code before entire mocha test

查看:138
本文介绍了之前整个摩卡试运行异步code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种方式,整个摩卡测试之前运行异步code。

I'm looking for a way to run async code before the entire mocha test.

下面是一个使用的参数和放大器阵列测试的例子;该数组中的期望和遍历所有项目的生产函数的断言。

Here's an example of a test that uses an array of arguments & expectations and loops over all of the items in this array to produce function assertions.

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

现在,我的问题是如何将这项工作如果从价值的承诺来测试,这样的:

Now, my question is how would this work if the tests value came from a promise, like this:

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

也试过:

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

然而,在测试这个例子没有运行,因为摩卡并不因为它是一个承诺内识别描述语句。

However in this example none of the tests run because mocha doesn't recognize the describe statement because it's within a promise.

之前 / beforeEach 不会做任何事情来帮助与格式的测试反正,除非是将与知识有需要整个测试之前,所要执行的异步操作提供摩卡beforeTest 挂钩。

before / beforeEach won't do anything to help with a test in the format anyway unless the was a beforeTest hook that would supply mocha with the knowledge that there's an async operation that needs to be run before the entire test.

推荐答案

我不知道是否有任何简单的方法来做到这一点,但你可以尝试的运行摩卡编程的。

I am not sure if there is any easy way to do this, but you could try to run Mocha programatically.

下面是一个什么,这可能看起来有点脏版,只是为了显示这个想法。

Here is a little dirty version of what this could look like, just to show the idea.

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

测试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)
    })
  })
})

测试/ 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)
      })
    })
  })
})

您可以再运行你掌握运行测试launcher.js

You can then run you rests by running test-launcher.js.

这篇关于之前整个摩卡试运行异步code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆