Javascript Jest-如何为每个测试重置对象状态?

Javascript Jest-如何为每个测试重置对象状态?,javascript,testing,jestjs,Javascript,Testing,Jestjs,我不太会开玩笑,我正试图弄清楚如何在每次测试后重置测试对象 当前代码 describe.only('POST request - missing entry', () => { // newBlog is the "test" object let newBlog = { title: 'Test Title', author: 'Foo Bar', url: 'www.google.com',

我不太会开玩笑,我正试图弄清楚如何在每次测试后重置测试对象

当前代码

describe.only('POST request - missing entry', () => {
    // newBlog is the "test" object
    let newBlog = {
        title: 'Test Title',
        author: 'Foo Bar',
        url: 'www.google.com',
        likes: 100
    }

    test('sets "likes" field to 0 when missing', async () => {
        delete newBlog.likes // propagates to next test
        console.log(newBlog)
    })

    test('returns 400 error when "title" and "url" fields are missing', async () => {
        console.log(newBlog)
    })
})
目标:我正在编写测试,使用jest测试错误的POST请求。i、 e.我的POST请求将故意为每个测试缺少字段

第一次测试将省略
likes
字段,而第二次测试将缺少
title、url
字段。目标是只编写一次
newBlog
对象,而不是为每个测试重写对象

问题这里的主要问题是,第一个测试的结果会传播到下一个测试,即,当删除第一个测试的
likes
字段时,它保持不变,并在没有
likes
字段的情况下启动第二个测试

我想知道如何为每次测试重置对象的内容

尝试到目前为止,我尝试了几件事:

  • 我在每次之前使用
    以以下方式重置
    新日志
  • 但是,上述代码不起作用,因为
    newBlog
    在不同的范围内,所以每个测试都不能识别
    newBlog
    变量

  • 我还使用
    在每次
    之后以以下方式重置:
  • 这次,它运行了,但给了我和第一个代码片段相同的结果

    我想知道如何为每个测试重置对象,因为stackoverflow中讨论的许多解决方案似乎都关注于重置函数而不是对象


    提前感谢您的帮助。

    尝试以下方法,在“描述”中声明变量,并在“每个之前”中重置它:

    describe.only('POST request - missing entry', () => {
    // newBlog is the "test" object
    let newBlog;
    
    beforeEach(() => {
        newBlog = {
             title: 'Test Title',
             author: 'Foo Bar',
             url: 'www.google.com',
             likes: 100
        }
    
    }}
    
    test('sets "likes" field to 0 when missing', async () => {
        delete newBlog.likes // propagates to next test
        console.log(newBlog)
    })
    
    test('returns 400 error when "title" and "url" fields are missing', async () => {
        console.log(newBlog)
    })
    })
    

    尝试以下操作,在“描述”中声明变量,并在“之前每个”中重置它:

    describe.only('POST request - missing entry', () => {
    // newBlog is the "test" object
    let newBlog;
    
    beforeEach(() => {
        newBlog = {
             title: 'Test Title',
             author: 'Foo Bar',
             url: 'www.google.com',
             likes: 100
        }
    
    }}
    
    test('sets "likes" field to 0 when missing', async () => {
        delete newBlog.likes // propagates to next test
        console.log(newBlog)
    })
    
    test('returns 400 error when "title" and "url" fields are missing', async () => {
        console.log(newBlog)
    })
    })
    

    你需要使用Jest的函数是正确的;但是,从beforeach()返回的惟一内容是承诺,而从beforeach()返回
    newBlog
    的生成器什么也不做。我要做的是在descripe函数中创建一个局部变量,并在每次测试运行之前让beforeach()重新分配该变量,如下所示

    fdescribe('POST request - missing entry', () => {
      let newBlog;
    
      beforeEach(() => {
        newBlog = {
          title: 'Test Title',
          author: 'Foo Bar',
          url: 'www.google.com',
          likes: 100
        }
      });
    
      test('sets "likes" field to 0 when missing', async () => { 
        delete newBlog.likes; // remove likes key from copied object
        console.log(newBlog);
      });
    
      test('returns 400 error when "title" and "url" fields are missing', async () => {
        delete newBlog.title;
        delete newBlog.url;
        console.log(newBlog);
      });
    })
    

    此外,清除模拟函数的所有调用和实例,这些调用和实例不会以您希望在此处使用的方式重置
    newBlog
    变量。

    您正确地使用了Jest函数;但是,从beforeach()返回的惟一内容是承诺,而从beforeach()返回
    newBlog
    的生成器什么也不做。我要做的是在descripe函数中创建一个局部变量,并在每次测试运行之前让beforeach()重新分配该变量,如下所示

    fdescribe('POST request - missing entry', () => {
      let newBlog;
    
      beforeEach(() => {
        newBlog = {
          title: 'Test Title',
          author: 'Foo Bar',
          url: 'www.google.com',
          likes: 100
        }
      });
    
      test('sets "likes" field to 0 when missing', async () => { 
        delete newBlog.likes; // remove likes key from copied object
        console.log(newBlog);
      });
    
      test('returns 400 error when "title" and "url" fields are missing', async () => {
        delete newBlog.title;
        delete newBlog.url;
        console.log(newBlog);
      });
    })
    

    此外,清除模拟函数的所有调用和实例,这些调用和实例不会以您希望在此处使用的方式重置
    newBlog
    变量。

    在这种情况下,我通常深度复制测试对象。因此,我有一个助手函数(您也可以使用Lodash的deepClone函数)

    在测试中,您创建所需测试对象的新深度副本,而不是像下面这样改变它的状态:

    test('sets "likes" field to 0 when missing', async () => {
      let testObj = deepCopy(newBlog)  
      delete testObj.likes // propagates to next test
      console.log(testObj)
    })
    

    在这种情况下,我通常深度复制测试对象。因此,我有一个助手函数(您也可以使用Lodash的deepClone函数)

    在测试中,您创建所需测试对象的新深度副本,而不是像下面这样改变它的状态:

    test('sets "likes" field to 0 when missing', async () => {
      let testObj = deepCopy(newBlog)  
      delete testObj.likes // propagates to next test
      console.log(testObj)
    })
    

    在每次
    @DanielA之前声明您的
    newBlog
    变量otuside。怀特谢谢,这就是我需要的!在每次
    @DanielA之前声明您的
    newBlog
    变量otuside。怀特谢谢,这就是我需要的!