chai-http:无法读取断言处未定义的 属性 'headers'。<anonymous>
chai-http: Cannot read property 'headers' of undefined at Assertion.<anonymous>
我在 gitlab 管道上 运行 进行单元测试时遇到此错误,但它在我的本地环境中运行良好:
TypeError: Cannot read property 'headers' of undefined
at Assertion.<anonymous> (node_modules/chai-http/lib/http.js:175:19)
at Assertion.propertyGetter (node_modules/chai/lib/chai/utils/addProperty.js:62:29)
at Object.get (<anonymous>)
at Object.proxyGetter [as get] (node_modules/chai/lib/chai/utils/proxify.js:98:22)
at Assertion.<anonymous> (node_modules/chai-http/lib/http.js:224:40)
at Assertion.propertyGetter (node_modules/chai/lib/chai/utils/addProperty.js:62:29)
at Object.get (<anonymous>)
at Object.proxyGetter [as get] (node_modules/chai/lib/chai/utils/proxify.js:98:22)
at /app/tests/Auth/AuthGetRegister.js:10:21
at Test.Request.callback (node_modules/superagent/lib/node/index.js:728:3)
at ClientRequest.<anonymous> (node_modules/superagent/lib/node/index.js:647:10)
at Socket.socketErrorListener (_http_client.js:469:9)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
这是我的单元测试文件架构:
tests/
├── Auth/
│ ├── AuthGetRegister.js
├── common.js
└── top.js
该应用也在 docker 环境中运行。
通过以下命令进行单元测试 运行:
./node_modules/.bin/mocha tests --reporter mocha-junit-reporter --reporter-options mochaFile=./test_results/test-results.xml --unhandled-rejections=strict
common.js:
let chai = require("chai");
let chaiHttp = require('chai-http');
let jwt = require('jsonwebtoken');
let fs = require('fs');
let privateKey = fs.readFileSync( __dirname + '/../RSA/jwtRS256.key');
let options = {
server: "http://localhost:3000/api",
jwt: jwt.sign({ userId: "fa76aff7-83e7-4ec2-b3e7-79d1629577b0" }, privateKey, { expiresIn: '3m', algorithm: 'RS256' })
};
chai.use(chaiHttp);
exports.options = options;
exports.chai = chai;
exports.assert = chai.assert;
exports.expect = chai.expect;
top.js:
var common = require("./common");
/*
** Auth
*/
describe("GET /api/auth/register", function () {
require('./Auth/AuthGetRegister');
});
AuthGetRegister.js:
let common = require("../common");
let options = common.options;
let chai = common.chai;
let expect = common.expect;
it('it should return status 200 with a list of pathologies', (done) => {
chai.request(options.server)
.get('/auth/register')
.end((err, res) => {
expect(res).to.be.json;
expect(res).to.have.status(200);
done();
});
});
我以 AuthGetRegister 为例,但我的所有单元测试都出现相同的错误。
我已经尝试了建议的解决方法 here,但它不起作用。
这是我的 gitlab 配置问题。在服务器完全 运行.
之前,单元测试是 运行
我添加了一个 wait-for-it 脚本来等待服务器 运行,然后 运行 单元测试。
我在 gitlab 管道上 运行 进行单元测试时遇到此错误,但它在我的本地环境中运行良好:
TypeError: Cannot read property 'headers' of undefined
at Assertion.<anonymous> (node_modules/chai-http/lib/http.js:175:19)
at Assertion.propertyGetter (node_modules/chai/lib/chai/utils/addProperty.js:62:29)
at Object.get (<anonymous>)
at Object.proxyGetter [as get] (node_modules/chai/lib/chai/utils/proxify.js:98:22)
at Assertion.<anonymous> (node_modules/chai-http/lib/http.js:224:40)
at Assertion.propertyGetter (node_modules/chai/lib/chai/utils/addProperty.js:62:29)
at Object.get (<anonymous>)
at Object.proxyGetter [as get] (node_modules/chai/lib/chai/utils/proxify.js:98:22)
at /app/tests/Auth/AuthGetRegister.js:10:21
at Test.Request.callback (node_modules/superagent/lib/node/index.js:728:3)
at ClientRequest.<anonymous> (node_modules/superagent/lib/node/index.js:647:10)
at Socket.socketErrorListener (_http_client.js:469:9)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
这是我的单元测试文件架构:
tests/
├── Auth/
│ ├── AuthGetRegister.js
├── common.js
└── top.js
该应用也在 docker 环境中运行。
通过以下命令进行单元测试 运行:
./node_modules/.bin/mocha tests --reporter mocha-junit-reporter --reporter-options mochaFile=./test_results/test-results.xml --unhandled-rejections=strict
common.js:
let chai = require("chai");
let chaiHttp = require('chai-http');
let jwt = require('jsonwebtoken');
let fs = require('fs');
let privateKey = fs.readFileSync( __dirname + '/../RSA/jwtRS256.key');
let options = {
server: "http://localhost:3000/api",
jwt: jwt.sign({ userId: "fa76aff7-83e7-4ec2-b3e7-79d1629577b0" }, privateKey, { expiresIn: '3m', algorithm: 'RS256' })
};
chai.use(chaiHttp);
exports.options = options;
exports.chai = chai;
exports.assert = chai.assert;
exports.expect = chai.expect;
top.js:
var common = require("./common");
/*
** Auth
*/
describe("GET /api/auth/register", function () {
require('./Auth/AuthGetRegister');
});
AuthGetRegister.js:
let common = require("../common");
let options = common.options;
let chai = common.chai;
let expect = common.expect;
it('it should return status 200 with a list of pathologies', (done) => {
chai.request(options.server)
.get('/auth/register')
.end((err, res) => {
expect(res).to.be.json;
expect(res).to.have.status(200);
done();
});
});
我以 AuthGetRegister 为例,但我的所有单元测试都出现相同的错误。
我已经尝试了建议的解决方法 here,但它不起作用。
这是我的 gitlab 配置问题。在服务器完全 运行.
之前,单元测试是 运行我添加了一个 wait-for-it 脚本来等待服务器 运行,然后 运行 单元测试。