Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  134] [ 1]  / answers: 1 / hits: 10799  / 4 Years ago, fri, october 16, 2020, 12:00:00

I am new to Jest, and I am trying to figure out how to to reset the test object after each test.


Current Code


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)
})
})

Objective: I am writing test using jest to test for bad POST request. i.e. my POST request will intentionally have missing fields for each test.


likes field will be omitted from first test while title, url field will be missing from second test. Goal is to write newBlog object only once rather than rewriting objects for each tests.


Problem Main issue here is that the result of first test propagates to next test, i.e. when removing likes field for first test, it stays like that and starts the second test without having likes field.


I want to know how I can reset the content of object for each test.


Attempts So far, I tried few things:



  1. I used BeforeEach to reset the newBlog in following manner:


beforeEach(() => {
let newBlog = {
title: 'Test Title',
author: 'Foo Bar',
url: 'www.google.com',
likes: 100
}

return newBlog
})

However, above code does not work since newBlog is in different scope so each test does not recognize newBlog variable.



  1. I also used AfterEach to reset in following manner:


  afterEach(() => {
jest.clearAllMocks()
})

This time, it ran but gave me the same results as first code snippet.


I would like to know how to reset objects for each test as many of the solution discussed in stackoverflow seems to focus on resetting functions rather than objects.


Thank you for your help in advance.


More From » testing

 Answers
6

Try something like this, declare the variable in the describe and reset it in the beforeEach:


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)
})
})

[#2470] Tuesday, October 13, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kamronr

Total Points: 749
Total Questions: 110
Total Answers: 122

Location: Dominica
Member since Sat, Nov 5, 2022
2 Years ago
kamronr questions
Mon, Dec 21, 20, 00:00, 3 Years ago
Sat, Oct 3, 20, 00:00, 4 Years ago
Sun, Jul 28, 19, 00:00, 5 Years ago
Mon, Mar 18, 19, 00:00, 5 Years ago
;