schematics icon indicating copy to clipboard operation
schematics copied to clipboard

feat: add @jest/globals for schematics

Open Yehonal opened this issue 2 years ago • 8 comments

PR Checklist

Please check if your PR fulfills the following requirements:

  • [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
  • [x] Tests for the changes have been added (for bug fixes / features)
  • [ ] Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • [ ] Bugfix
  • [x] Feature
  • [ ] Code style update (formatting, local variables)
  • [ ] Refactoring (no functional changes, no api changes)
  • [ ] Build related changes
  • [ ] CI related changes
  • [ ] Other... Please describe:

What is the current behavior?

Issue Number: #1942

What is the new behavior?

This PR adds the jest/globals for every generated file

Does this PR introduce a breaking change?

  • [ ] Yes
  • [x] No

Other information

Yehonal avatar Feb 21 '23 12:02 Yehonal

I wonder if wouldn't be better if we only add those import statements if injectGlobals is false, although I personally prefer having them regardless of that flag

micalevisk avatar Feb 21 '23 17:02 micalevisk

I'm not sure there's an easy way to know it, considering that jest can be configured with a javascript file that can be dynamic, and we probably want to avoid running it to discover what config the user has.

What would be the main motivation to not use this import?

Yehonal avatar Feb 21 '23 17:02 Yehonal

What would be the main motivation to not use this import?

not having those globals isn't common (not yet, at least). And the jest docs has a note regarding that injectGlobals flag:

This option is only supported using the default jest-circus test runner.

I'm unsure on what that means, in practice tho.

The only way I can think of is by adding yet another flag to the schematics to opt-in to this change. Then the CLI would define the value of that flag at runtime. Since the jest config file can be defined by the end user (ie, it could have any name), this solution won't work for everyone.


But if Kamil agree that we could start having this import, it's fine. I like this new version more.

micalevisk avatar Feb 21 '23 18:02 micalevisk

That comment means that if you do not use jest-circus, you are forced to import those globals because they will never be injected, even with the injectGlobals: true

I guess that this import will always work, no matter the value of the config or what runner is used.

Yehonal avatar Feb 21 '23 20:02 Yehonal

Is Jest about to drop the auto-globals-injection feature any time soon?

kamilmysliwiec avatar Feb 22 '23 07:02 kamilmysliwiec

Is Jest about to drop the auto-globals-injection feature any time soon?

I'm not sure about that, but there are already certain cases where the globals are not injected anymore (when the runner is different or when you have ESM enabled) that's also why I discovered it. In any case, I think it's always better to not pollute the global scope anyway.

Yehonal avatar Feb 22 '23 10:02 Yehonal

Do those imports works when injectGlobals is true?

micalevisk avatar Feb 22 '23 14:02 micalevisk

Do those imports works when injectGlobals is true?

Yes

Yehonal avatar Feb 22 '23 15:02 Yehonal