首页 > 解决方案 > NODEJS:使用 supertest 和 mocha 的测试自动通过 GitLab CI/CD

问题描述

我正在尝试在 express nodejs 应用程序上使用 mocha 和 supertest 运行测试,问题是执行测试的 GitLab 运行器会自动通过它们,即使它们是错误的并且应该引发错误。

当我在本地运行它们时,我有正确的输出。

我是这些框架/技术的新手。

这是我的项目树:
项目树

我的测试:

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();
  });
});
});

这是我的 .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

因此,在推送之后,管道开始并且在我有这些输出之后:
gitlab ci/cd 输出

另一方面,当我在本地执行测试时,我有正确的输出(2 次通过和 1 次失败),如下所示:

本地测试输出

有什么我遗漏或做错了吗?如何在 GitLab 中获得相同的输出?

标签: node.jsexpressautomated-testsmocha.jsgitlab-ci

解决方案


在尝试了不同的配置后,我发现问题来自我正在与之交谈的服务器没有运行


推荐阅读