简体   繁体   English

NODEJS:使用 supertest 和 mocha 的测试自动通过 GitLab CI/CD

[英]NODEJS : Tests with supertest and mocha automatically pass on GitLab CI/CD

I'm trying to run tests with mocha and supertest on an express nodejs app and the issue is that the GitLab runner on which the tests are executed automatically passes them even when they are wrong and should raise an error.我正在尝试在 express nodejs 应用程序上使用 mocha 和 supertest 运行测试,问题是执行测试的 GitLab 运行器会自动通过它们,即使它们是错误的并且应该引发错误。

I have the correct outputs when I run them locally.当我在本地运行它们时,我有正确的输出。

I'm new to these frameworks / technologies.我是这些框架/技术的新手。

Here my project tree:这是我的项目树:
项目树

My tests:我的测试:

var supertest = require("supertest");
var should = require("should");

var server = supertest.agent("http://localhost:8080");

// UNIT test begin

describe("SAMPLE unit test",function(){

 // #1 should return home page
server.get('/', function(req, res) {
tst = res.status(200).json({ "message": "hello world" });
console.log(tst);
});

it("should return home page",function(done){

// calling home page api
server
.get("/")
.expect("Content-type",/json/)
.expect(200) // THis is HTTP response
.end(function(err,res){
  // HTTP status should be 200
  //res.status.should.equal(200);
  // Error key should be false.
  //res.body.error.should.equal(false);
  console.log(res);
  done();
});
});
it("should login",function(done){
   // calling home page api
   server
   .post("/api/auth/signin")
   .send({"email":"emile.dadou@epsi.fr","password":"preudhomme"})
   .expect("Content-type",/json/)
   .expect(200) // THis is HTTP response
   .end(function(err,res){
   // HTTP status should be 200
   //res.status.should.equal(200);
   // Error key should be false.
   //res.body.error.should.equal(false);
   console.log(res);
   done();
  });
 });
 it("should raise an error",function(done){
  // calling home page api
  server
  .get("/gné") // this route doesn't exist
  .expect("Content-type",/json/)
  .expect(200) // THis is HTTP response
  .end(function(err,res){
  // HTTP status should be 200
  res.status.should.equal(200);
  // Error key should be false.
  res.body.error.should.equal(false);
  console.log(res);
  done();
  });
});
});

And here is my.gitlab-ci.yml file:这是 my.gitlab-ci.yml 文件:

image: node:latest

stages:
  - build
  - tests

build:
 type: build
 stage: build
 script:
   - ls -l
   - npm i
   - npm run build --if-present
   - npm start server.js
   - npm test

tests:
  type: test
  stage: tests
  script:
   - npm i
   - npm test

So after a push the pipeline starts and after I have these outputs:因此,在推送之后,管道开始并且在我有这些输出之后:
gitlab ci/cd 输出

on the other when I execute the tests locally I have the right outputs (2 pass and 1 failing) like this:另一方面,当我在本地执行测试时,我有正确的输出(2 次通过和 1 次失败),如下所示:

本地测试输出

Is there something that I'm missing or done wrong?有什么我遗漏或做错了吗? How can I have the same outputs in GitLab?如何在 GitLab 中获得相同的输出?

After trying different configurations i found out that the issue came from the server that i was talking to wasn't running在尝试了不同的配置后,我发现问题来自我正在与之交谈的服务器没有运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM