Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用相同的上下文运行多个mocha文件和套件_Node.js_Automated Tests_Mocha.js_Integration Testing - Fatal编程技术网

Node.js 使用相同的上下文运行多个mocha文件和套件

Node.js 使用相同的上下文运行多个mocha文件和套件,node.js,automated-tests,mocha.js,integration-testing,Node.js,Automated Tests,Mocha.js,Integration Testing,我正在尝试使用一些共享上下文运行几个集成测试。共享的上下文是单个express应用程序,我尝试在套件/文件之间共享它,因为它需要几秒钟的时间来启动 我通过实例化一个“runner”mocha测试套件来实现它,该套件将具有测试功能,只需根据需要要求每个测试文件,并且工作正常(一个副作用是需要子测试文件的测试将以“成功”结束)在文件中的任何测试实际运行之前,但这是一个小问题) 问题: 我想通知测试运行程序导入套件中的所有测试都已完成,这样我就可以调用测试运行程序中的关闭逻辑并干净地结束测试。在标准测

我正在尝试使用一些共享上下文运行几个集成测试。共享的上下文是单个express应用程序,我尝试在套件/文件之间共享它,因为它需要几秒钟的时间来启动

我通过实例化一个“runner”mocha测试套件来实现它,该套件将具有测试功能,只需
根据需要要求
每个测试文件,并且工作正常(一个副作用是需要子测试文件的测试将以“成功”结束)在文件中的任何测试实际运行之前,但这是一个小问题)

问题:

我想通知测试运行程序导入套件中的所有测试都已完成,这样我就可以调用测试运行程序中的关闭逻辑并干净地结束测试。在标准测试函数中,您可以传递一个
done
函数来表示测试中的代码已完成,因此我将每个子测试包装在一个descripe块中,以使用
after
钩子来表示整个测试模块已完成:

// test-runner.js:

describe('Integration tests', function () {
  let app
  let log
  this.timeout(300000) // 5 mins
  before(function (done) {

    app = require('../app')
    app.initialize()
      .then(() => {
        done()
      })
      .catch(err => {
        log.error(err)
        done(err)
      })
  })

  it('Running api tests...', (done) => {
    require('./integration/api.test')(app, done)
  })
但是当我这样做的时候,测试套件就不能运行了。默认超时将刚好过期,如果我设置了更高的超时,它就在那里(等待更长的超时)。如果我钩住一个调试会话,那么测试会立即退出,并且后钩子(和前钩子)永远不会被调用

关于如何做到这一点,我也愿意接受其他想法,但我还没有找到任何好的解决方案,允许在测试之间共享一些上下文,同时将它们分解成不同的文件

// ./integration/api.test.js:

module.exports = (app) => {
  let api = supertest.agent(app)
      .set('Content-Type', 'application/json')

  describe('Authorization', () => {
    describe('Trying to access authorization sections', () => {
      it('should be denied for /home', async () => {
        await api.get(`${baseUrl}/home`)
          .expect(STATUS_CODE.UNAUTHORIZED)
      })
...

// test-runner.js:

describe('Integration tests', function () {
  let app
  let log
  this.timeout(300000) // 5 mins
  before(function (done) {

    app = require('../app')
    app.initialize()
      .then(() => {
        done()
      })
      .catch(err => {
        log.error(err)
        done(err)
      })
  })

  it('Running api tests...', (done) => {
    require('./integration/api.test')(app, done)
  })
// ./integration/api.test.js:

module.exports = (app, done) => {
  let api = supertest.agent(app)
      .set('Content-Type', 'application/json')

  describe('All api tests', () => {
    let api
    before(() => {
      api = supertest.agent(app)
        .set('Content-Type', 'application/json')
    })
    after(() => {
      done()  // should be calling the done function passed in by test runner
    })
    describe('Authorization', () => {
      describe('Trying to access authorization sections', () => {
        it('should be denied for /home', async () => {
          await api.get(`${baseUrl}/home`)
            .expect(STATUS_CODE.UNAUTHORIZED)
        })
...