最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jest testing pass variable to another test - Stack Overflow

matteradmin3PV0评论

I'm trying to assign a variable from one test to be accessed within another test. For example:

let user;

test('create new user', async () => {
  const response = await createUser()
  user = response
})

test('delete user', async () => {
  const response = await deleteUser(user.id)
})

I understand that jest has a --runInBand option, however this still has user as undefined in "delete user". Any ideas how to acplish this with jest? Thank you

I'm trying to assign a variable from one test to be accessed within another test. For example:

let user;

test('create new user', async () => {
  const response = await createUser()
  user = response
})

test('delete user', async () => {
  const response = await deleteUser(user.id)
})

I understand that jest has a --runInBand option, however this still has user as undefined in "delete user". Any ideas how to acplish this with jest? Thank you

Share Improve this question asked Feb 2, 2019 at 2:05 dzmdzm 23.6k51 gold badges152 silver badges229 bronze badges 2
  • I replicated the same code in sandbox[codesandbox.io/s/p77xpx0wn7] (as jest not available in Stackoverflow snippets). The code works perfectly fine. Might need more details on how it has been implemented. – CRayen Commented Feb 2, 2019 at 2:55
  • You can use beforeAll method to get and store user data globally but you can use it for the same file, if you want to share the same user details with other test files, the bad news is you cannot. because JEST test runs Independently, Mocha allows tests to run in sync so that you can share one user's data across files – Durai Vinoth Commented Jun 13, 2024 at 8:10
Add a ment  | 

2 Answers 2

Reset to default 7

Each test runs independently, and for good reason. Tests should be confirming isolated conditions, methods, and logic. All --runInBand does is run the tests in serially, but they still won't necessarily be able to share data objects the way you seem to be expecting.

Also, assuming these methods defer to a backend service of some kind, you're not going to easily be able to fully test the behavior of that system. It sounds like you want an end-to-end or integration testing framework, as opposed to a unit testing framework like Jest.

Keeping with Jest, you're likely going to need to mock whatever backend service is being called in createUser and deleteUser. Jest mocks can help replace external functions with new ones that create the types of conditions you want to test.

Alternatively or in addition, you might be able to stub your user object using beforeAll or beforeEach, creating sample data that allows you to test how deleteUser behaves when it's passed a particular object (likely bypassing whatever backend persistence with an aforementioned mock).

You can create the variables in the describe and then assign them in the beforeAll() or beforeEach(). If your tests haven't already deleted it, you could/should delete the values in the afterEach() or afterAll().

describe('My Test', () => {
  let user: User;

  beforeAll(async () => {
    user = await createUser();
  });

  test('delete user', async () => {
    const response = await deleteUser(user.id);
  });
});
Post a comment

comment list (0)

  1. No comments so far