question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[jest-plugin-set] Set variables should be created before beforeEach is called

See original GitHub issue

Description

Nested set calls must ALL come before beforeEach in order to be set properly. Otherwise, it’ll use the values from outer set calls which is incorrect

Screenshot

Test Plan

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:4
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
negativetwelvecommented, Jul 24, 2018

Hi @bxt, thanks for your comment. If I remember correctly, the issue comes from something like:

import set from 'jest-plugin-set';

describe('outer', () => {
  set('a', () => 1);
  set('b', () => a);

  beforeEach(() => {
    console.log(a, b);
  });

  describe('inner', () => {
    set('a', () => 5);

    beforeEach(() => {
      console.log(a, b);
    });

    it('returns the correct value', () => {
      expect(a + b).toEqual(10);
    });
  });
});

It will log:

1, 1
1, 1

when it should be:

1, 1
5, 5

and if I remember correctly, the test should fail on this and return 2 instead of 10. Let me know if you’re seeing the same behavior.

(I wrote the code without testing, please excuse if there are small errors)

0reactions
timscottcommented, Feb 16, 2019

I ran into this also today. Here’s another repro.

describe('', () => {
  set('a', () => x)
  beforeEach(() => {
    console.log(a);
  })
  describe('', () => {
    set('x', () => 1)
    test('', () => {
      expect(a).toEqual(1)
    })
  })
})

Fails with ReferenceError: x is not defined on the second line.

Pulling variables from inner contexts to use in beforeEach is definitely a useful pattern which can eliminate a lot of duplicate code. Something I always do in rspec.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass variable from beforeEach hook to tests in jest?
Just declare sandbox so it is available in the scope of beforeEach and test: let sandbox; beforeEach(async () => { sandbox ...
Read more >
Setup and Teardown - Jest
You have a method initializeCityDatabase() that must be called before each of these tests, and a method clearCityDatabase() that must be ...
Read more >
Using Jest beforeEach to write better unit tests in JavaScript ...
In this post, we will delve into using Jest beforeEach properly to make unit testing better with JavaScript, let's get going! Jest before...
Read more >
How I Was Completely Wrong About Setting Up/Tearing Down ...
So to test my constructor here, I developed the following test suite: ... describe 's scope meant that the beforeEach callback would get...
Read more >
DSL - Kahlan
it : contains the code to test. Keep its description short and clear. Setup and Teardown. As the name implies, the beforeEach function...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found