Ensuring Express App is running before each Mocha Test

后端 未结 6 2085
别跟我提以往
别跟我提以往 2020-12-13 15:00

I am working on developing a REST API using ExpressJS, NodeJS, Mongoose and Mocha.

The thing is that I have an app.coffee file, thats responsible for setting up Expr

6条回答
  •  执念已碎
    2020-12-13 15:41

    There might be a more straightforward way, but I went to Grunt for automating my functional tests. There's an express and mocha plugin to reach your goal. My gruntfile:

    'use strict';
    
    module.exports = function (grunt) {
    grunt.initConfig({
        express: {
            options: {}
          , test: {
                options: {
                    script: './app.js'
                }
            }
        }
      , simplemocha: {
            options: {
                globals: ['should']
              , timeout: 8000
              , ignoreLeaks: false
              , ui: 'bdd'
              , reporter: 'tap'
            }
          , all: { src: ['tests/*.test.js'] }
        }
    })
    
    grunt.loadNpmTasks('grunt-express-server')
    grunt.loadNpmTasks('grunt-simple-mocha')
    
    grunt.registerTask('default', ['express:test', 'simplemocha', 'express:test:stop'])
    }
    

    bonus: add 'grunt' as a git pre-commit hook. This way you cannot commit without passing all the tests

提交回复
热议问题