在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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...