Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context in before/afterEach calls #3553

Closed
sarenji opened this issue May 11, 2017 · 13 comments
Closed

Context in before/afterEach calls #3553

sarenji opened this issue May 11, 2017 · 13 comments

Comments

@sarenji
Copy link

sarenji commented May 11, 2017

Do you want to request a feature or report a bug?

Bug. Related issues (Jasmine-related): #3506 #3505

What is the current behavior?

The this context is not shared over a single test: Before, during, and after.

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

/* eslint-env jest */

beforeEach(function () {
  this.hello = 'hi';
});

afterEach(function () {
  console.log(this.hello);
});

describe('context', () => {
  it('should work', function () {
    console.log(this.hello);
  });
});

What is the expected behavior?

I should see "hi" printed twice. Instead, I see undefined printed twice.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

Jest 20.0.1.
Works on 19.0.0.


As an aside, I think it's best to recommend against this usage, especially as the style for JavaScript has moved toward () => {} functions rather than function() {} functions. Just dealing with some old code here :) I felt this issue might be helpful in documenting Jasmine's behavior and discussing next steps for Jest.

@thymikee
Copy link
Collaborator

It's not a bug, it's a feature.
Seriously it is, Jest is not Jasmine and restricts context to test/before/after function scope. Use shared variables outside of test function scope.

let hello;
beforeEach(function () {
  hello = 'hi';
});

afterEach(function () {
  console.log(hello);
});

describe('context', () => {
  it('should work', function () {
    console.log(hello);
  });
});

@artdent
Copy link

artdent commented May 19, 2017

This was quite the breaking change for us. I wish it had been mentioned in the release notes :-(.

@cpojer
Copy link
Member

cpojer commented May 19, 2017

Apologies, we rewrote Jasmine and this happened as a result of that :(

avaly added a commit to avaly/jest-codemods that referenced this issue Jul 16, 2017
avaly added a commit to avaly/jest-codemods that referenced this issue Jul 16, 2017
avaly added a commit to avaly/jest-codemods that referenced this issue Jul 16, 2017
avaly added a commit to avaly/jest-codemods that referenced this issue Jul 16, 2017
avaly added a commit to avaly/jest-codemods that referenced this issue Jul 17, 2017
avaly added a commit to avaly/jest-codemods that referenced this issue Jul 18, 2017
avaly added a commit to avaly/jest-codemods that referenced this issue Jul 20, 2017
avaly added a commit to avaly/jest-codemods that referenced this issue Jul 24, 2017
avaly added a commit to avaly/jest-codemods that referenced this issue Jul 27, 2017
@skovhus
Copy link
Contributor

skovhus commented Aug 7, 2017

jest-codemods can now help people upgrade from older versions of Jest. See https://github.com/skovhus/jest-codemods/releases/tag/0.11.0

FYI @cpojer @thymikee @sarenji

@anatoliyarkhipov
Copy link

Just wondering, is there any article with explanation why this was removed?

@mattdell
Copy link

It would be great to get an explanation for the removal of this. To me, at least, this seems like a strange convention to enforce.

@Ghirigoro
Copy link

As Jest possibly moves towards TypeScript it might be worth revisiting this (or some similar solution for context). If you're using strict types (i.e. no implicit null/undefined) then you're forced to always initialize the local variable when you declare it or else always check that it's initialized:

let aVar:SomeType|undefined = undefined;

// Compiler doesn't know that beforeEach is is going to be called 
// so you have to define the variable above as possibly undefined...
beforeEach( ()=> { aVar = new SomeType; } );

test("A Test", ()=> {
    // Which means you have to ensure that the variable is defined 
    // wherever you use it.
    if (aVar !== undefined) { 
        // do  something with aVar...
    }
});

It's not necessarily a big deal especially if initializing the variable is trivial but if it's complex it can get slightly irritating.

@yss14
Copy link

yss14 commented Feb 13, 2019

@Ghirigoro

How about

let aVar!: SomeType;

beforeEach( ()=> { aVar = new SomeType; } );

test("A Test", ()=> {
    // do something with aVar without if-statement...
});

This way you have strict null checks for the whole project, but you can mitigate it for test cases on your own risk.

@Ghirigoro
Copy link

@yss14

That seems pretty good. I didn't realize you could use the bang operator on declarations. You learn something new every day. Thanks!

@monokrome
Copy link

monokrome commented Nov 22, 2019

Yo, this needs to be fixed. How is it a "feature"? It's not necessarily a bug, but nobody gets any value out of the scoping not working as expected. It feels like bad design in user experience from the point of view of a user.

@dav1app
Copy link

dav1app commented Feb 3, 2020

I don't get it. If I run with Jest experimental feature --detectLeak, it clearly says that I should not keep references to the global scope.

image

Am I losing something important?

@jmar777
Copy link

jmar777 commented Feb 4, 2021

It's not a bug, it's a feature.
Seriously it is, Jest is not Jasmine and restricts context to test/before/after function scope. Use shared variables outside of test function scope.

Sorry for waking up a zombie thread, but is this behavior documented anywhere? I just ran into the same issue of writing completely "normal" JavaScript that broke in ways that seemed to defy all reasonable explanation until I found this issue. I'm happy to do it the jest way, but I don't really feel confident that I understand what that is?

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests