伊斯坦布尔没有为mocha chai和chai-http测试提供报道

时间:2018-02-02 16:44:48

标签: node.js mocha chai istanbul

我正在尝试为我的api生成伊斯坦布尔代码覆盖。我已经在SO中找到了很多答案,而且伊斯坦布尔的文档也没有,但是没有什么对我有用。摩卡测试运行正常,一切都过去了,甚至istanbul也创建了覆盖文件夹,但它没有在其中创建报告。我的应用程序是简单的快递应用程序,我正在使用柴,柴-http和摩卡为它。以下是 test / app.js 文件和 package.json 文件的代码。

'Can't resolve symbols...'

的package.json

const app=require('../app');
const chai=require('chai');
const chaiHttp=require('chai-http');
const should=chai.should();
const tokenForToday="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImFudXJhbmJhcm1hbkBnbWFpbC5jb20iLCJpYXQiOjE1MTc1Mzg4ODcsImV4cCI6MTUxNzYyNTI4N30.8YJ35mwWhzz4kT-fruhcPANUXyZJYvCsKKHTejZNnWA";
chai.use(chaiHttp);

describe('CB API',()=>{
    /**
     * Test /api route
     */

    describe('/GET api',()=>{
      it('it should GET the api default content', (done) => {
      chai.request(app)
          .get('/api')
          .end((err, res) => {
              res.should.have.status(200);
              res.body.should.be.eql({message:'Welcome to Cloudboost API'});
            done();
          });
      });
    })

    /**
     * Test /login POST route
     */
     describe('/POST login',()=>{
       it('it should return the authentication token', (done) => {
         let bdy={
           email:"anuranbarman@gmail.com",
           password:"anuran"
         }
       chai.request(app)
           .post('/api/login')
           .send(bdy)
           .end((err, res) => {
               res.should.have.status(200);
               res.body.should.have.property('token');
               res.body.should.have.property('auth').eql(true);
             done();
           });
       });
     })

     /**
      * Test /login POST without email field
      */
      describe('/POST login',()=>{
        it('it should return false success', (done) => {
          let bdy={
            password:"anuran"
          }
        chai.request(app)
            .post('/api/login')
            .send(bdy)
            .end((err, res) => {
                res.should.have.status(422);
                res.body.should.have.property('auth').eql(false);
              done();
            });
        });
      })

      /**
       * Test /image route without token
       */
       describe('/POST image without token',()=>{
         it('it should return false success', (done) => {
           let bdy={
             url:"https://d1qb2nb5cznatu.cloudfront.net/startups/i/490103-917cc2864d0246e313e9521971422f09-medium_jpg.jpg?buster=1430997518"
           }
         chai.request(app)
             .post('/api/image')
             .send(bdy)
             .end((err, res) => {
                 res.should.have.status(401);
                 res.body.should.have.property('success').eql(false);
               done();
             });
         });
       })

       /**
        * Test /image with token but with invalid url
        */
        describe('/POST image with invalid url',()=>{
          it('it should return false success', (done) => {
            let bdy={
              url:"https://d1qb2nb5cznatu.cloudfrt.net/startups/i/490103-917cc2864d0246e313e9521971422f09-medium_jpg.jpg?buster=1430997518"
            }
          chai.request(app)
              .post('/api/image')
              .set('token',tokenForToday)
              .send(bdy)
              .end((err, res) => {
                  res.should.have.status(500);
                  res.body.should.have.property('success').eql(false);
                done();
              });
          });
        })

        /**
         * Test /image with token but with NO url
         */
         describe('/POST image with no url',()=>{
           it('it should return false success', (done) => {

           chai.request(app)
               .post('/api/image')
               .set('token',tokenForToday)
               .end((err, res) => {
                   res.should.have.status(422);
                   res.body.should.have.property('success').eql(false);
                 done();
               });
           });
         })

         /**
          * Test /image with token and with valid url
          */
          describe('/POST image',()=>{
            it('it should return false success', (done) => {
              let bdy={
                url:"https://d1qb2nb5cznatu.cloudfront.net/startups/i/490103-917cc2864d0246e313e9521971422f09-medium_jpg.jpg?buster=1430997518"
              }
            chai.request(app)
                .post('/api/image')
                .set('token',tokenForToday)
                .send(bdy)
                .end((err, res) => {
                    res.should.have.status(200);
                  done();
                });
            });
          })


          /**
           * Test /image with invalid token but with valid url
           */
           describe('/POST image',()=>{
             it('it should return false success', (done) => {
               let bdy={
                 url:"https://d1qb2nb5cznatu.cloudfront.net/startups/i/490103-917cc2864d0246e313e9521971422f09-medium_jpg.jpg?buster=1430997518"
               }
             chai.request(app)
                 .post('/api/image')
                 .set('token','tokenForToday')
                 .send(bdy)
                 .end((err, res) => {
                     res.should.have.status(500);
                     res.body.should.have.property('success').eql(false);
                   done();
                 });
             });
           })

           /**
            * Test /patch with token and data and patch
            */
            describe('/PATCH patch',()=>{
              it('it should return success', (done) => {
                const dataString=JSON.stringify({
                        name: "dante",
                        age: "22"
                });
                const patchString=JSON.stringify([
                  { "op": "replace", "path": "/name", "value": "anuran" },
                  { "op": "add", "path": "/position", "value": "NodeJS Developer" }
                ])
                let bdy={
                  data:dataString,
                  patch:patchString
                }

              chai.request(app)
                  .patch('/api/patch')
                  .set('token',tokenForToday)
                  .set('content-type', 'application/x-www-form-urlencoded')
                  .send(bdy)
                  .end((err, res) => {
                    console.log(res.body);
                      res.should.have.status(200);
                      res.body.should.have.property('success').eql(true);
                    done();
                  });
              });
            })


            /**
             * Test /patch without token
             */
             describe('/PATCH patch',()=>{
               it('it should return false success', (done) => {
                 const dataString=JSON.stringify({
                         name: "dante",
                         age: "22"
                 });
                 const patchString=JSON.stringify([
                   { "op": "replace", "path": "/name", "value": "anuran" },
                   { "op": "add", "path": "/position", "value": "NodeJS Developer" }
                 ])
                 let bdy={
                   data:dataString,
                   patch:patchString
                 }

               chai.request(app)
                   .patch('/api/patch')
                   .set('content-type', 'application/x-www-form-urlencoded')
                   .send(bdy)
                   .end((err, res) => {
                     console.log(res.body);
                       res.should.have.status(422);
                       res.body.should.have.property('success').eql(false);
                     done();
                   });
               });
             })


             /**
              * Test /patch without data field
              */
              describe('/PATCH patch',()=>{
                it('it should return false', (done) => {

                  const patchString=JSON.stringify([
                    { "op": "replace", "path": "/name", "value": "anuran" },
                    { "op": "add", "path": "/position", "value": "NodeJS Developer" }
                  ])
                  let bdy={
                    patch:patchString
                  }

                chai.request(app)
                    .patch('/api/patch')
                    .set('token',tokenForToday)
                    .set('content-type', 'application/x-www-form-urlencoded')
                    .send(bdy)
                    .end((err, res) => {
                      console.log(res.body);
                        res.should.have.status(422);
                        res.body.should.have.property('success').eql(false);
                      done();
                    });
                });
              })

              /**
               * Test /patch with invalid patch object
               */
               describe('/PATCH patch',()=>{
                 it('it should return false success', (done) => {
                   const dataString=JSON.stringify({
                           name: "dante",
                           age: "22"
                   });
                   const patchString=JSON.stringify([
                     { "op": "INVALID", "path": "/name", "value": "anuran" },
                     { "op": "add", "path": "/position", "value": "NodeJS Developer" }
                   ])
                   let bdy={
                     data:dataString,
                     patch:patchString
                   }

                 chai.request(app)
                     .patch('/api/patch')
                     .set('token',tokenForToday)
                     .set('content-type', 'application/x-www-form-urlencoded')
                     .send(bdy)
                     .end((err, res) => {
                       console.log(res.body);
                         res.should.have.status(200);
                         res.body.should.have.property('success').eql(false);
                       done();
                     });
                 });
               })
})

我使用此命令从命令行运行它:

sudo ./node_modules/istanbul封面./node_modules/.bin/_mocha

2 个答案:

答案 0 :(得分:2)

对于我的node.js(typescript)项目,我使用这种脚本进行测试+覆盖:

"test": npm-run-all --parallel build:watch cross-env DOTENV_FILE=.env.test nodemon -e ts --exec mocha build/**/unit/**/*.spec.js --recursive --inspect --watch --reporter spec --bail

只需使用npm run test

即可启动它

编辑:

对于伊斯坦布尔:

第1步 - 在istanbul

下运行您的服务器
$ ./node_modules/.bin/istanbul cover --handle-sigint  server.js

第2步 - 运行测试,无需在istanbul下运行

$ ./node_modules/.bin/_mocha  -R spec ./.instrument/test/** --recursive

完整报告位于coverage/lcov-report/index.html

答案 1 :(得分:0)

您可以在覆盖脚本之后添加--exit标志