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]: add isolateModulesAsync #13680

Merged
merged 14 commits into from
Dec 31, 2022
Merged

Conversation

mmanciop
Copy link
Contributor

@mmanciop mmanciop commented Dec 16, 2022

Summary

Introduce jest.isolateModulesAsync, the async equivalent of jest.isolateModules. Examples of use-cases are provided in #10428.

Test plan

Unit tests added with similar coverage and structure as isolateModules.

Credits

Changes originally proposed by @dreyks in #10428.

Fixes #10428.

Introduce `jest.isolateModulesAsync`, the async equivalent of `jest.isolateModules`.
@facebook-github-bot
Copy link
Contributor

Hi @mmanciop!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@mmanciop mmanciop marked this pull request as draft December 16, 2022 22:33
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Copy link
Contributor

@mrazauskas mrazauskas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks interesting. I left few comments.

packages/jest-types/__typetests__/jest.test.ts Outdated Show resolved Hide resolved
packages/jest-types/__typetests__/jest.test.ts Outdated Show resolved Hide resolved
packages/jest-runtime/src/index.ts Outdated Show resolved Hide resolved
packages/jest-runtime/src/index.ts Outdated Show resolved Hide resolved
packages/jest-environment/src/index.ts Outdated Show resolved Hide resolved
Michele Mancioppi and others added 2 commits December 19, 2022 08:02
Copy link
Contributor

@mrazauskas mrazauskas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to run ESLint and Prettier: yarn lint && yarn lint:prettier

If you decide to stick with fluent API idea, it needs a type test somewhere here:

https://github.com/facebook/jest/blob/41bf2300895a2c00d0525d21260f0a392819871f/packages/jest-types/__typetests__/jest.test.ts#L21-L32

packages/jest-types/__typetests__/jest.test.ts Outdated Show resolved Hide resolved
Michele Mancioppi and others added 3 commits December 19, 2022 10:31
@mmanciop mmanciop marked this pull request as ready for review December 19, 2022 10:47
@mmanciop mmanciop force-pushed the isolateModulesAsync branch from 2cf0d73 to f88db2b Compare December 19, 2022 10:48
@mmanciop
Copy link
Contributor Author

@mrazauskas I think this may be ready for a second look :-)

Copy link
Contributor

@mrazauskas mrazauskas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Looks much better. I left few comments.

@@ -2226,6 +2248,7 @@ export default class Runtime {
getTimerCount: () => _getFakeTimers().getTimerCount(),
isMockFunction: this._moduleMocker.isMockFunction,
isolateModules,
isolateModulesAsync,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn’t it better to simply inline:

Suggested change
isolateModulesAsync,
isolateModulesAsync: this.isolateModulesAsync,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 36c138e

docs/JestObjectAPI.md Outdated Show resolved Hide resolved
@mmanciop
Copy link
Contributor Author

@mrazauskas 3rd is the charm? ;-)

Copy link
Contributor

@mrazauskas mrazauskas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I think these are last details to solve.

@@ -98,6 +98,10 @@ expectError(jest.enableAutomock('moduleName'));
expectType<typeof jest>(jest.isolateModules(() => {}));
expectError(jest.isolateModules());

expectType<Promise<void>>(jest.isolateModulesAsync(async () => {}));
expectType<Promise<void>>(jest.isolateModulesAsync(() => {}));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try running type tests. This assertion is currently failing. Should be:

Suggested change
expectType<Promise<void>>(jest.isolateModulesAsync(() => {}));
expectError(jest.isolateModulesAsync(() => {}));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm I did run it and it did not fail:

mmanciop@Micheles-MacBook-Pro jest % yarn test-types
Running one project: type-tests
 PASS   type-tests  packages/expect/__typetests__/expect.test.ts
 PASS   type-tests  packages/jest-worker/__typetests__/jest-worker.test.ts
 PASS   type-tests  packages/jest-mock/__typetests__/utility-types.test.ts
 PASS   type-tests  packages/jest-mock/__typetests__/Mocked.test.ts
 PASS   type-tests  packages/jest-types/__typetests__/each.test.ts
 PASS   type-tests  packages/jest-types/__typetests__/globals.test.ts
 PASS   type-tests  packages/jest-types/__typetests__/expect.test.ts
 PASS   type-tests  packages/jest-types/__typetests__/jest.test.ts
 PASS   type-tests  packages/jest-reporters/__typetests__/jest-reporters.test.ts
 PASS   type-tests  packages/jest-resolve/__typetests__/resolver.test.ts
 PASS   type-tests  packages/jest-runner/__typetests__/jest-runner.test.ts
 PASS   type-tests  packages/jest-mock/__typetests__/mock-functions.test.ts
 PASS   type-tests  packages/jest-snapshot/__typetests__/SnapshotResolver.test.ts
 PASS   type-tests  packages/jest-snapshot/__typetests__/matchers.test.ts
 PASS   type-tests  packages/expect-utils/__typetests__/utils.test.ts
 PASS   type-tests  packages/jest-types/__typetests__/config.test.ts
 PASS   type-tests  packages/jest-expect/__typetests__/jest-expect.test.ts
 PASS   type-tests  packages/jest-mock/__typetests__/ModuleMocker.test.ts
 PASS   type-tests  packages/jest/__typetests__/jest.test.ts

Test Suites: 19 passed, 19 total
Tests:       1283 passed, 1283 total
Snapshots:   0 total
Time:        1.951 s, estimated 2 s
Ran all test suites.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrazauskas I am not managing to replicate the type error. I must be doing something wrong here. I updated the signature as I understand you suggested, can you please check again?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repo was not rebuild? Tests import from @jest/globals, not from src. That is why rebuilding is important after making changes in src.

docs/JestObjectAPI.md Outdated Show resolved Hide resolved
docs/JestObjectAPI.md Outdated Show resolved Hide resolved
Michele Mancioppi and others added 2 commits December 19, 2022 14:50
Co-authored-by: Tom Mrazauskas <tom@mrazauskas.de>
@mrazauskas
Copy link
Contributor

Thanks. The type test is passing now and the rest looks good for me.

Let’s wait for maintainers review.

@mmanciop mmanciop changed the title feat: add isolateModulesAsync [Feature]: add isolateModulesAsync Dec 19, 2022
@mmanciop
Copy link
Contributor Author

The error in test-jest-jasmine is the same that occurs in #13675 . I don't think it is this PR introducing it.

@mrazauskas
Copy link
Contributor

Yep. This odd "Segmentation fault" error appears from time to time. Not related with your PR for sure.

@mmanciop
Copy link
Contributor Author

@SimenB @jeysal WDYT?

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@SimenB SimenB merged commit abc64a6 into jestjs:main Dec 31, 2022
@mmanciop mmanciop deleted the isolateModulesAsync branch December 31, 2022 21:37
@mmanciop
Copy link
Contributor Author

mmanciop commented Jan 5, 2023

@SimenB do you have a timeline for the next release? I am very much looking forward to using this ;-)

@SimenB
Copy link
Member

SimenB commented Jan 6, 2023

Waiting for #13375

@github-actions
Copy link

github-actions bot commented Feb 6, 2023

This pull request 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 Feb 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature: jest.isolateModules accept an async function for dynamic import()s
4 participants