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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration to configure Jest test methods in global environment #10481

Closed
jdbruijn opened this issue Sep 5, 2020 · 5 comments 路 Fixed by #10484
Closed

Add configuration to configure Jest test methods in global environment #10481

jdbruijn opened this issue Sep 5, 2020 · 5 comments 路 Fixed by #10484

Comments

@jdbruijn
Copy link

jdbruijn commented Sep 5, 2020

馃殌 Feature Proposal

Add configuration to configure whether Jest test methods and objects are put into the global environment.

Motivation

Please outline the motivation for the proposal.

The @jest/globals package was added in #9801, but Jest still sets the globals, which isn't needed when using @jest/globals. PR #9306 described a proposal and some work to add a configuration (noJestGlobals) that allows users to explicitly disable Jest adding to the globals. This is a way of forcing the use of @jest/globals and thus ensuring that the functions are explicitly imported and can't accidentally be used in other (non-test) code for example. I've read most of the conversation sin #9306, #9801 and some of the referenced tickets and didn't find an issue that is tracking this feature.

I have a branch locally which also adds a config option to not add it to global. As you note, getting that to work with jasmine is finicky, so I might just bother with circus for now.

Originally posted by @SimenB in #9306 (comment)

I don't know how hard this is to implement but I'd be happy to help on this even though I have zero knowledge/familiarity with the Jest code base.

Example

Please provide an example for how this feature would be used.

Configuration added.
addGlobalEnvironment [boolean]

Default: true

Indicates whether Jest test methods and objects are put into the global environment. By setting this to false, explicit import, like import { describe, test } from '@jest/globals are required to use the Jest test methods and objects.

Pitch

Why does this feature belong in the Jest core platform?

To allow users to consistently require explicit Jest imports.

@jdbruijn
Copy link
Author

jdbruijn commented Sep 5, 2020

For the time being I have added an ESLint configuration to give lint errors when Jest globals are used. I'm also using eslint-plugin-jest, but that doesn't have such a rule as far as I've seen.

  rules: {
    'no-restricted-globals': [
      'error',
      /**
       * From Jest global environment.
       * https://github.com/facebook/jest/blob/v26.4.2/packages/jest-globals/src/index.ts#L16-L27
       */
      'it',
      'test',
      'fit',
      'xit',
      'xtest',
      'describe',
      'xdescribe',
      'fdescribe',
      'beforeAll',
      'beforeEach',
      'afterEach',
      'afterAll',
    ],
  },

@SimenB
Copy link
Member

SimenB commented Sep 6, 2020

Just pushed up the branch I had locally. Rebased it, but no other changes since April. I don't really remember the state of it. Missing tests and docs, but beyond that. I don't even remember if it runs correctly 馃槢

https://github.com/SimenB/jest/tree/no-inject-globals

@SimenB
Copy link
Member

SimenB commented Sep 6, 2020

As for the eslint plugin, we might wanna consider not adding to globals by default: jest-community/eslint-plugin-jest#556 (comment)

@SimenB
Copy link
Member

SimenB commented Sep 7, 2020

#10484

@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 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
2 participants