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]: Allow globalSetup to access things in globals #12436

Closed
ahnpnl opened this issue Feb 19, 2022 · 7 comments · Fixed by #12440
Closed

[Feature]: Allow globalSetup to access things in globals #12436

ahnpnl opened this issue Feb 19, 2022 · 7 comments · Fixed by #12440

Comments

@ahnpnl
Copy link
Contributor

ahnpnl commented Feb 19, 2022

🚀 Feature Proposal

Include globals in global config so that globalSetup or globalTeardown can use it.

Motivation

I notice that Jest has globals, but it isn't included in global config. When I look at name globals, I was thinking about it would be included in global config. So it would match with things related to "global".

I have a use case thymikee/jest-preset-angular#1348 which if Jest allows globals to be in global config, it will look beautiful.

Example

Define in Jest config

// jest.config.js
//...
globals: {
      'ngcc': { project: 'tsconfig.spec.json' },
}

In a globalSetup script

import type { Config } from '@jest/types';

export = async (globalConfig: Config.GlobalConfig) => {
      console.log(globalConfig.globals);
}

Pitch

This would be a change in Jest config. I think it should be in core.

@ahnpnl
Copy link
Contributor Author

ahnpnl commented Feb 19, 2022

Hi @SimenB , if you think that is by design and cannot be changed, feel free to close this issue :) If you have other alternative ways, pls give me suggestions.

@SimenB
Copy link
Member

SimenB commented Feb 20, 2022

Since globalSetup runs for each project, it makes sense to pass projectConfig, I think?

@ahnpnl
Copy link
Contributor Author

ahnpnl commented Feb 20, 2022

Does it mean, if users have a repo with Jest projects for multiple projects like this

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  globalSetup: 'jest-preset-angular/global-setup',
  projects: ['<rootDir>/projects/app1', '<rootDir>/projects/app2'],
};

Jest shouldn't run globalSetup at all at root but should run per project?

@SimenB
Copy link
Member

SimenB commented Feb 20, 2022

I'd assume so, try it out? I've never used the global hooks at all, so I haven't really internalised how they work.

#6865 implemented it in projects, looking at it might provide clues?

@ahnpnl
Copy link
Contributor Author

ahnpnl commented Feb 20, 2022

Thanks for the information. Reading the motivation from #6865, it means globalSetup can run:

  • for each individual project which are defined via projects
  • or run at root

Since globals config option is only available in ProjectConfig, can we pass 2nd argument to globalSetup function? An example is

// globalSetup.ts
export = async (globalConfig, projectConfig) => {
     // ....
}

I think passing projectConfig to globalSetup function would be an easier implementation and seem to be more flexible

@SimenB
Copy link
Member

SimenB commented Feb 20, 2022

Yeah, that's what I meant in #12436 (comment)

ahnpnl added a commit to ahnpnl/jest that referenced this issue Feb 20, 2022
ahnpnl added a commit to ahnpnl/jest that referenced this issue Feb 20, 2022
ahnpnl added a commit to ahnpnl/jest that referenced this issue Feb 21, 2022
ahnpnl added a commit to ahnpnl/jest that referenced this issue Feb 21, 2022
ahnpnl added a commit to ahnpnl/jest that referenced this issue Feb 21, 2022
@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 Mar 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants