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

[Feature]: setup-jest as function instead of import with side effect #2755

Closed
StavNoyAkur8 opened this issue Sep 18, 2024 · 1 comment · Fixed by #2825
Closed

[Feature]: setup-jest as function instead of import with side effect #2755

StavNoyAkur8 opened this issue Sep 18, 2024 · 1 comment · Fixed by #2825
Labels
🚀 Feature Request new suggested feature

Comments

@StavNoyAkur8
Copy link

🚀 Feature Proposal

In the current setup, we import the setup file, and it executes as a side effect:

// <root>/test-setup.ts
import "jest-preset-angular/setup-jest";

I would like instead to be able to import it as a function:

import { setupJest } from "jest-preset-angular/setup-jest";

setupJest();

Motivation

Importing as a side effect is non obvious.
Many formatters will automatically put import statements at the top of the document. This makes it so that if we want to setup the global ngJest, which should be before the import, so it's accessible to the setup:

globalThis.ngJest = {
  testEnvironmentOptions:  {
    errorOnUnknownElements: true,
    errorOnUnknownProperties: true,
  };
};
import "jest-preset-angular/setup-jest";

After formatting it goes under the import:

import "jest-preset-angular/setup-jest";

globalThis.ngJest = ...

Rendering it effectively useless.

By having it as a function, we can circumvents that problem by calling it when we're ready:

import { setupJest } from "jest-preset-angular/setup-jest";

globalThis.ngJest = ...
setupJest();

Additionally, it opens the possibility to pass that config as a parameter, instead of having to set the (untyped) global:

setupJest({
  testEnvironmentOptions:  ...
});

Currently, my other workaround is to set the globals in the jest config (which is maybe a better option anyway)

Example

No response

@ahnpnl ahnpnl added the 🚀 Feature Request new suggested feature label Sep 18, 2024
@ahnpnl
Copy link
Collaborator

ahnpnl commented Sep 18, 2024

Oh ya I was playing around with this, which is related to this #354 (comment)

ahnpnl added a commit that referenced this issue Nov 15, 2024
ahnpnl added a commit that referenced this issue Nov 15, 2024
ahnpnl added a commit that referenced this issue Nov 15, 2024
@ahnpnl ahnpnl closed this as completed in 21c0238 Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚀 Feature Request new suggested feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants