2017-08-24 137 views
0

我的package.json代码:Jest测试用例不失败。

"dependencies": { 
    "gulp-jest": "^2.0.0", 
    "jest-cli": "^20.0.4", 
    "supertest": "^1.2.0" 
}  
"scripts": { 
    "test": "gulp test" 
} 

我gulpfile测试任务是为

gulp.task('run-tests', (done) => { 
    gulp.src(['__tests__']) 
    .pipe(jest({ testMatch: ['**/*.test.js'] })) 
    .on('error', (error) => { 
    done(error); 
    }) 
    .on('finish',() => { 
    done(); 
    }); 
}); 

我的测试用例文件是

const request = require('supertest'); 
test('POST: Should create a new plan', (done) => { 
     request('https://localhost:3000') 
     .post('/api/name') 
     .set('authorization', `bearer ${accessToken}`) 
     .set('content-type', 'application/json') 
     .send({ 
      name: 'TestPlanCreated' 
     }) 
     .end((err, res) => { 
      if (err) { 
      return done.fail(err); 
      } 
     expect(res.statusCode).toBe(299); 
     done(); 
     }); 

成功时它应该给的StatusCode 200,我是通过期望超过200而使其失败,但是测试用例不失败并且不能通过。

回答

0

我改变了我的任务为:

gulp.task('run-tests', (done) => { 
    const options = { 
    projects: [__dirname], 
    silent: true, 
    testMatch: ['**/?(*.)(test).js?(x)'], 
    runInBand: true, 
    }; 
    jest.runCLI(options, [__dirname]) 
    .then((result) => { 
    if (result.numFailedTestSuites || result.numFailedTests) { 
     return done(result); 
    } 
    return done(); 
    }) 
    .catch(error => done(error)); 
}); 

和更新的packges到

"gulp-nsp": "^2.4.2", 
"gulp-sequence": "^0.4.6", 
"jest-cli": "^21.1.0", 

现在测试用例开始失败/通行证。