微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在nodeJS中使用Jest和supertest进行测试时,随机出现“重复的内容长度”错误

如何解决在nodeJS中使用Jest和supertest进行测试时,随机出现“重复的内容长度”错误

所以我正在对NodeJS上的Rest API项目进行一些单元测试。

我设置了8个测试,可以进行基本的Rest API调用,例如登录和注销用户。我一遍又一遍地运行测试,问题是有时测试通过没问题,但有时某些测试失败并显示以下错误Error: Error: Parse Error: Duplicate Content-Length我要添加2个屏幕截图,一个用于通过测试,另一个用于测试失败。这两个屏幕截图来自在完全相同的代码上运行的测试。我正在使用的代码也随附:

enter image description here

enter image description here

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!!',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,email: 'testuser3@test.com',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,token: user.activationToken,});
});

test('Should not create a new user with inapropiate password',async () => {
  await request(app)
    .post('/api/users')
    .send({
      name: 'Wencel Santos',password: 'Mpasss123',})
    .expect(400);
});

test('Should not create a new user with existing email',email: userOne.email,})
    .expect(400);
});

test('Should not create a new user with incomplete info',async () => {
  await request(app)
    .post('/api/users')
    .send({
      name: '',})
    .expect(400);
  await request(app)
    .post('/api/users')
    .send({
      name: 'Wencel Santos',email: '',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',})
    .expect(400);
});

test('Should not login with wrong password or missing information',async () => {
  await request(app)
    .post('/api/users/login')
    .send({
      email: '',})
    .expect(400);
  await request(app)
    .post('/api/users/login')
    .send({
      email: userOne.password,password: 'aksdjfaksldjf',})
    .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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。