测试时出现CircleCI错误(我认为与MongoDB相关)-无法读取属性';拆分';空的
我得到: TypeError:无法读取null的属性“split” 在mongoose.connect中的error对象中,在CircleCI中,以及稍后的测试timeout中,第一个测试失败,错误为500。 这看起来像是mongo/mongoose错误,我是否在测试中出错,我的意思是我肯定我会,因为在简单的非异步测试中,我没有得到这个错误。 顺便说一句,这在本地运行良好,没有错误,只是在CircleCI测试中失败 app.ts数据库(mongoose)连接代码:测试时出现CircleCI错误(我认为与MongoDB相关)-无法读取属性';拆分';空的,mongodb,testing,mongoose,continuous-integration,circleci,Mongodb,Testing,Mongoose,Continuous Integration,Circleci,我得到: TypeError:无法读取null的属性“split” 在mongoose.connect中的error对象中,在CircleCI中,以及稍后的测试timeout中,第一个测试失败,错误为500。 这看起来像是mongo/mongoose错误,我是否在测试中出错,我的意思是我肯定我会,因为在简单的非异步测试中,我没有得到这个错误。 顺便说一句,这在本地运行良好,没有错误,只是在CircleCI测试中失败 app.ts数据库(mongoose)连接代码: mongoose .con
mongoose
.connect(mongoUrl, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false,
dbName: 'expressing-space',
})
.then(() => {
// console.log('MongoDB connected..');
})
.catch((err) => {
console.log(
'MongoDB connection error. Please make sure MongoDB is running. ' + err,
);
});
测试:
import request from 'supertest';
import mongoose from 'mongoose';
import app from '../src/app';
let token: string = '';
const email = 'x@x.com';
const password = '12345';
beforeAll((done) => {
request(app)
.post('/auth/login')
.send({
email,
password,
})
.end((err, response) => {
token = response.body.token;
done();
});
});
afterAll((done) => {
// Closing the DB connection allows Jest to exit successfully.
mongoose.connection.close();
done();
});
describe('GET /likes/artists/', () => {
// token not being sent - should respond with a 401
it('should return require authorization - return 401', async (done) => {
const res = await request(app)
.get('/likes/artists/')
.set('Content-Type', 'application/json');
expect(res.status).toBe(401);
done();
});
// send the token - should respond with a 200
it('should respond with JSON data - 200', async (done) => {
const res = await request(app)
.get('/likes/artists/')
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json');
expect(res.status).toBe(200);
expect(res.type).toBe('application/json');
done();
});
});
describe('PUT /likes/artists/', () => {
it('should return require authorization - return 401', async (done) => {
const res = await request(app)
.put('/likes/artists')
.set('Content-Type', 'application/json');
expect(res.status).toBe(401);
done();
});
// #Todo: There is probalby better way to go about this. Re-check it!
it('should return json data with either name prop for success or message prop for err', async (done) => {
const name = 'dmx';
const res = await request(app)
.put('/likes/artists')
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
.send({ name });
if (res.status === 409) {
expect(res.status).toBe(409);
expect(res.body).toHaveProperty('message');
} else if (res.status === 201) {
expect(res.status).toBe(201);
expect(res.body.artist).toHaveProperty('name');
}
done();
});
});
// #Todo: There is probalby better way to go about this. Re-check it!
describe('DELETE /likes/artists/:artistId', () => {
const artistId = '5e24d7350b68952acdcafc2e';
it('should return require authorization - return 401', async (done) => {
const res = await request(app).delete(`/likes/artists/${artistId}`);
expect(res.status).toBe(401);
done();
});
it('should delete userId from artist if it exists or return 404 if not', async (done) => {
const res = await request(app)
.delete(`/likes/artists/${artistId}`)
.set('Authorization', `Bearer ${token}`);
if (res.status === 404) {
expect(res.status).toBe(404);
expect(res.body).toHaveProperty('message');
} else if (res.status === 202) {
expect(res.status).toBe(202);
expect(res.body).toHaveProperty('message');
}
done();
});
});
在Heroku部署mongoose应用程序时发生了这个错误。我用引号设置了环境变量,当我删除它们时,问题就消失了。希望这能有所帮助。当然,它必须是这么简单的东西。它为我修复了它,我也为Heroku做了同样的事情,并通过做同样的事情来修复它,但出于某种原因,我甚至没有尝试过CircleCI。非常感谢。