所以我正在对NodeJS上的Rest API项目进行一些单元测试。
我设置了8个测试,可以进行基本的Rest API调用,例如登录和注销用户。我一遍又一遍地运行测试,问题是有时测试通过没问题,但有时某些测试失败并显示以下错误Error: Error: Parse Error: Duplicate Content-Length
我要添加2个屏幕截图,一个用于通过测试,另一个用于测试失败。这两个屏幕截图来自在完全相同的代码上运行的测试。我正在使用的代码也随附:
const request = require('supertest');
const userOneId = new mongoose.Types.ObjectId();
const userOne = {
_id: userOneId,
name: 'Fabian Santos',
email: 'testuser@test.com',
password: '123Data!',
active: true,
};
const userTwoId = new mongoose.Types.ObjectId();
const userTwo = {
_id: userTwoId,
name: 'Wencel Santos',
email: 'testuser2@test.com',
password: '123Data!!',
active: true,
tokens: [
{
token: jwt.sign({ _id: userTwoId }, process.env.JWT_SECRET),
},
{
token: jwt.sign({ _id: userTwoId }, process.env.JWT_SECRET + 'extra'),
},
],
};
const userThreeId = new mongoose.Types.ObjectId();
const userThree = {
_id: userThreeId,
name: 'Fabian Santos',
email: 'testuser3@test.com',
password: '123Data!',
active: false,
};
const setupDB = async () => {
await User.deleteMany();
await new User(userOne).save();
await new User(userTwo).save();
await new User(userThree).save();
};
beforeEach(setupDB);
test('Should create a new user', async () => {
const response = await request(app)
.post('/api/users')
.send({
name: 'Wencel Santos',
email: 'wencelsantos@gmail.com',
password: 'Mpasss123!',
})
.expect(201);
// Assert user create din DB
const user = await User.findById(response.body.user._id);
expect(user).toBeTruthy();
// Assert Expected object
expect(response.body).toMatchObject({
user: {
active: false,
name: 'Wencel Santos',
email: 'wencelsantos@gmail.com',
},
token: user.activationToken,
});
});
test('Should not create a new user with inapropiate password', async () => {
await request(app)
.post('/api/users')
.send({
name: 'Wencel Santos',
email: 'wencelsantos@gmail.com',
password: 'Mpasss123',
})
.expect(400);
});
test('Should not create a new user with existing email', async () => {
await request(app)
.post('/api/users')
.send({
name: 'Wencel Santos',
email: userOne.email,
password: 'Mpasss123',
})
.expect(400);
});
test('Should not create a new user with incomplete info', async () => {
await request(app)
.post('/api/users')
.send({
name: '',
email: 'wencelsantos@gmail.com',
password: 'Mpasss123!',
})
.expect(400);
await request(app)
.post('/api/users')
.send({
name: 'Wencel Santos',
email: '',
password: 'Mpasss123',
})
.expect(400);
await request(app)
.post('/api/users')
.send({
name: 'Wencel Santos',
email: 'wencelsantos@gmail.com',
password: '',
})
.expect(400);
});
test('Should login existing user', async () => {
const response = await request(app)
.post('/api/users/login')
.send({
email: userOne.email,
password: userOne.password,
})
.expect(200);
const user = await User.findById(userOne._id);
// Assert Expected object
expect(user.tokens[0]).toMatchObject({ token: response.body.token });
});
test('Should not login non existing user', async () => {
await request(app)
.post('/api/users/login')
.send({
email: 'wencelsantos@gmail.com',
password: userOne.password,
})
.expect(400);
});
test('Should not login with wrong password or missing information', async () => {
await request(app)
.post('/api/users/login')
.send({
email: '',
password: userOne.password,
})
.expect(400);
await request(app)
.post('/api/users/login')
.send({
email: userOne.password,
password: 'aksdjfaksldjf',
})
.expect(400);
await request(app)
.post('/api/users/login')
.send({
email: userOne.password,
password: '',
})
.expect(400);
});
test('Should not login inactive user', async () => {
await request(app)
.post('/api/users/login')
.send({
email: userThree.email,
password: userThree.password,
})
.expect(400);
});
test('Should logout user', async () => {
await request(app)
.post('/api/users/logout')
.set('Authorization', `Bearer ${userTwo.tokens[0].token}`)
.send()
.expect(200);
const user = await User.findById(userTwo._id);
expect(user.tokens).toHaveLength(1);
});
现在我添加到套件中的测试越多,发生此错误的可能性就越大。
值得注意的是,如果我分别运行每个测试,这意味着我注释掉了其他测试,它们总是会通过,只有当我取消对所有测试的注释并带内运行它们时才会出现错误。
我的Jest配置是这个jest --watch --runInBand