在单个昂贵的操作(例如HTTP GET)之后执行多个mocha测试

时间:2017-10-21 11:10:57

标签: javascript node.js mocha

如果我有一组要访问的网址:

let tests = [
  { status: 200, mediaType: 'text/html', req: { method: 'GET', uri: 'http://stackoverflow.com/' } },
  { status: 200, mediaType: 'text/html', req: { method: 'GET', uri: 'http://stackoverflow.com999/' } },
  { status: 200, mediaType: 'text/html', req: { method: 'GET999', uri: 'http://stackoverflow.com/' } },
]

以及要对每个执行的一组测试:

it('should have status ' + test.status, () => {
  expect(test.response.statusCode).to.equal(test.status)
})
it('should have content type ' + test.mediaType, () => {
  let s = test.response.headers['content-type']
  let mediaType = s.indexOf(';') === -1 ? s : s.substr(0, s.indexOf(';'))
  expect(mediaType).to.equal(test.mediaType)
})
it('should have a body', () => {
  expect(test.body).to.not.equal('')
})

如何在每组测试中仅执行一次昂贵的操作?另外,如果网址没有加载,我也不想运行测试。

1 个答案:

答案 0 :(得分:0)

mocha Working With Promises documentation有一个例子,每个(一个,诚然)测试等待beforeEach承诺在执行前完成。如果你还需要beforeEach,你可以保存beforeEach的承诺,你可以让他们等待一个承诺:

let P = null
beforeEach(function () {
  if (!P) P = new Promise(...)
  return P
})

但更有可能的是,您只想使用before

beforeEach(function () {
  return new Promise(...)
})

有关问题的完整列表:

let expect = require('chai').expect
let request = require('request')

let tests = [
  { status: 200, mediaType: 'text/html', req: { method: 'GET', uri: 'http://localhost/' } },
  { status: 200, mediaType: 'text/html', req: { method: 'GET', uri: 'http://localhost999/' } },
  { status: 200, mediaType: 'text/html', req: { method: 'GET999', uri: 'http://localhost/' } },
]

tests.forEach(function (test) {
  describe(test.req.method + ' ' + test.req.uri, function () {
    before('should load', function() {
      return new Promise(function (resolve, reject) {
        request(test.req, function (error, response, body) {
          if (error) {
            reject(test.rejection = error)
          } else {
            test.response = response
            test.body = response
            resolve()
          }
        })
      })
    });

    it('should have status ' + test.status, () => {
      expect(test.response.statusCode).to.equal(test.status)
    })
    it('should have content type ' + test.mediaType, () => {
      let s = test.response.headers['content-type']
      let mediaType = s.indexOf(';') === -1 ? s : s.substr(0, s.indexOf(';'))
      expect(mediaType).to.equal(test.mediaType)
    })
    it('should have a body', () => {
      expect(test.body).to.not.equal('')
    })
  })
})

这为您提供了非常流畅的输出:

  GET http://stackoverflow.com/
    ✓ should have status 200
    ✓ should have content type text/html
    ✓ should have a body

  GET http://stackoverflow.com999/
    1) "before each" hook: should load for "should have status 200"

  GET999 http://stackoverflow.com/
    ✓ should have status 200
    ✓ should have content type text/html
    ✓ should have a body

如果更改示例以使用stackoverflow的localhost intead,则可以查看访问日志以验证每个URL是否已加载一次:

127.0.0.1 - - [21/Oct/2017:06:52:10 -0400] "GET / HTTP/1.1" 200 12482 "-" "-"
127.0.0.1 - - [21/Oct/2017:06:52:10 -0400] "GET999 / HTTP/1.1" 501 485 "-" "-"

请注意,上一个操作GET999 http://stackoverflow.com/在stackoverflow上提供了200(通过清漆缓存),在apache上提供了501:

  2) GET999 http://localhost/
       should have status 200:

      AssertionError: expected 501 to equal 200
      + expected - actual

      -501
      +200