簡體   English   中英

NODEJS:使用 supertest 和 mocha 的測試自動通過 GitLab CI/CD

[英]NODEJS : Tests with supertest and mocha automatically pass on 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();
  });
});
});

這是 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

因此,在推送之后,管道開始並且在我有這些輸出之后:
gitlab ci/cd 輸出

另一方面,當我在本地執行測試時,我有正確的輸出(2 次通過和 1 次失敗),如下所示:

本地測試輸出

有什么我遺漏或做錯了嗎? 如何在 GitLab 中獲得相同的輸出?

在嘗試了不同的配置后,我發現問題來自我正在與之交談的服務器沒有運行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM