Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/restoreNames.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
'use strict';
"use strict";

describe('restoreNames', () => {
// const { restoreNames } = require('./restoreNames');
describe("restoreNames", () => {
const { restoreNames } = require("./restoreNames");

it('should ', () => {
const users = [
{
firstName: undefined,
lastName: "Holy",
fullName: "Jack Holy",
},
{
lastName: "Adams",
fullName: "Mike Adams",
},
];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This users array is defined in a shared scope and gets mutated by your first test. The second test then operates on this already-modified data, which means it isn't testing the original scenario. Tests must be independent. To fix this, you can either create a fresh copy of the users array inside each it block or use Jest's beforeEach hook to reset the data before each test.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This users array is defined once and then mutated by your tests. This means the tests are not isolated from each other, and the outcome of one can affect the next. To ensure each test is reliable and independent, you should provide a fresh copy of the test data for each test case. A beforeEach block is a great way to handle this.


it("should be a function", () => {
expect(restoreNames).toBeInstanceOf(Function);
});

// write tests here
it("should have firstName", () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test and the one on line 27 have the same name. It's a best practice to give each test a unique and descriptive name that explains the specific scenario it covers.

restoreNames(users);
expect(users[0].firstName).toBe("Jack");
});

it("should have firstName", () => {
restoreNames(users);
expect(users[1].firstName).toBe("Mike");
});
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the key requirements is that the function should not return a value. As mentioned in the previous review, you need to add a test case to verify that calling restoreNames results in undefined.