美文网首页
【原】使用mocha grunt gulp supertest

【原】使用mocha grunt gulp supertest

作者: 曹赫洋 | 来源:发表于2017-08-22 16:11 被阅读0次

    目录结构

    image.png

    npm 插件安装

    全局npm安装插件

    npm install -g grunt-cli 
    npm install -g gulp
    

    另外项目需要添加如下依赖 package.json

    "dependencies": {
        "assert": "^1.4.1",
        "express": "^4.15.4",
        "mysql": "^2.14.1"
      },
      "devDependencies": {
        "chai": "^4.1.1",
        "chai-as-promised": "^7.1.1",
        "grunt": "^1.0.1",
        "grunt-mocha": "^1.0.4",
        "grunt-mocha-cli": "^3.0.0",
        "grunt-mocha-test": "^0.13.2",
        "gulp": "^3.9.1",
        "gulp-cli": "^1.4.0",
        "gulp-concat": "^2.6.1",
        "gulp-mocha": "^4.3.1",
        "gulp-notify": "^3.0.0",
        "gulp-rename": "^1.2.2",
        "gulp-uglify": "^3.0.0",
        "istanbul": "^0.4.5",
        "load-grunt-tasks": "^3.5.2",
        "mocha": "^3.5.0",
        "should": "^11.2.1",
        "sinon": "^3.2.1",
        "supertest": "^3.0.0"
      }
    

    mocha 单元测试

    基本测试 test-demo.js

    var assert = require('assert');
    var request = require('supertest');
    var assert = require("assert");
    describe('Array', function() {  
      describe('#indexOf()', function() {
        it('should return -1 when the value is not present', function(done){
          assert.equal(-1, [1,2,3].indexOf(5));
          assert.equal(-1, [1,2,3].indexOf(0));
          done();
        });
      });
    });
    

    使用supertest 进行接口测试 test-webapi.js

    var assert = require('assert');
    var request = require('supertest');
    
    describe('Routing', function() {
      var url = process.env.TESTURL || 'http://localhost:3000';
      var userCookie = '';
    
      before(function(done) {
       done();
      });
    
      describe('#a', function() {
        it('', function(done){
         //发送数据
        //   var profile = {
        //     email: 'xuyawenwen',
        //     password: '123456',
        //   };
          request(url)
            .get('/a')
            //.send(profile)
            .expect(200) //Status code   期望
            .end(function(err,res) {
              if (err) {
                throw err;
              }
            //   console.log(res)
              console.log("success res==>",res.text);
              done();
            });
    
        });
      });
    
    });
    

    这时候执行如下命令就可以进行单元测试了

    mocha test/test-demo.js
    
    image.png
    #注意接口测试要先把接口项目跑起来
    mocha test/test-webapi.js
    
    image.png image.png

    mocha 与gulp 集成

    项目中添加gulpfile.js

    var gulp = require('gulp');
    var mocha = require('gulp-mocha');
    gulp.task('default', function() {
      return gulp.src(['test/test-*.js'], { read: false })
        .pipe(mocha({
          reporter: 'spec',
          globals: {
            should: require('should')
          }
        }));
    });
    
    image.png

    mocha 与grunt集成

    项目中添加Gruntfile.js

    module.exports = function(grunt) {
          grunt.initConfig({
            pkg: grunt.file.readJSON('package.json'),
            mochaTest: {
                src: ['test/test-*.js'],
                options: {
                    timeout: 6000,
                    reporter: 'spec'
                }
              }
          });
          grunt.loadNpmTasks('grunt-mocha-test');
          grunt.registerTask('default', ['mochaTest']);    
    };
    
    image.png

    项目笔者已经开源到了 https://github.com/caoheyang/nodeunittest

    相关文章

      网友评论

          本文标题:【原】使用mocha grunt gulp supertest

          本文链接:https://www.haomeiwen.com/subject/gmeldxtx.html