node.js - 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
另一方面,当我在本地执行测试时,我有正确的输出(2 次通过和 1 次失败),如下所示:
有什么我遗漏或做错了吗?如何在 GitLab 中获得相同的输出?
解决方案
在尝试了不同的配置后,我发现问题来自我正在与之交谈的服务器没有运行
推荐阅读
- r - R:如何在数据表中显示唯一行
- c# - 自适应卡片未在 Web 应用程序中显示图像
- javascript - 即使更新了 vuex 存储也不会更新 Nuxtjs 页面
- c# - 在按单选按钮查看中更改结果
- python - 无法在 Django 中取消订单
- r - R:如何对数据框中的选定变量应用命令?
- scala - 放弃断言的非单位值编译器错误
- c# - 如何在没有数据库连接的情况下使用 SQL Server 类(例如 SqlCommand)来防止 SQL 注入?
- html - 在 Google API 脚本的 HTML 服务中插入 Google Drive Link 时拒绝连接错误
- android - Firebase 云消息传递不断使我的应用程序崩溃