测试时出现 CircleCI 错误(我认为 MongoDB 相关)- 无法读取 null 的 属性 'split'
CircleCI error while testing (i think MongoDB related) - Cannot read property 'split' of null
我得到:
TypeError: Cannot read property 'split' of null
在来自 mongoose.connect 的错误对象中,在 CircleCI 中,后来在测试超时时,第一个失败并出现 500 错误。
看起来像 mongo/mongoose 错误,我是不是在测试中做错了什么,我的意思是我确定我做错了,因为通过简单的非异步测试我没有得到这个错误。
顺便说一句,这在本地工作正常,没有错误,只是在 CircleCI 测试中失败了。
app.ts 数据库(mongoose)连接代码:
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();
});
});
刚刚在使用 mongoose 将应用程序部署到 Heroku 时发生了这个错误。我已经用引号设置了环境变量,当我删除它们时问题就消失了。希望这有帮助。
我得到:
TypeError: Cannot read property 'split' of null
在来自 mongoose.connect 的错误对象中,在 CircleCI 中,后来在测试超时时,第一个失败并出现 500 错误。 看起来像 mongo/mongoose 错误,我是不是在测试中做错了什么,我的意思是我确定我做错了,因为通过简单的非异步测试我没有得到这个错误。 顺便说一句,这在本地工作正常,没有错误,只是在 CircleCI 测试中失败了。
app.ts 数据库(mongoose)连接代码:
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();
});
});
刚刚在使用 mongoose 将应用程序部署到 Heroku 时发生了这个错误。我已经用引号设置了环境变量,当我删除它们时问题就消失了。希望这有帮助。