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

feat: requireAndTranspileModule support ESM #11232

Merged

Conversation

WeiAnAn
Copy link
Contributor

@WeiAnAn WeiAnAn commented Mar 23, 2021

Summary

#11167

requireAndTranspileModule is called by importing globalSetup, globalTeardown, testRunner, runner, testEnvironment and createTranspilingRequire function.

Only createTranspilingRequire may need to import none default exporter, so I refactor requireAndTranspileModule, which now accept applyInteropRequireDefault option (default true) to determine return module itself or default. So does requireAndTranspileModule.

Breaking Change

  • requireAndTranspileModule always return a Promise.
  • createTranspilingRequire return function which return a Promise now.

Test plan

  • esm module e2e test
    • globalSetup
    • globalTeardown
    • testRunner
    • runner
    • testEnvironment

TODO

  • Testing
  • CHANGELOG.md

@WeiAnAn WeiAnAn force-pushed the require-and-transpile-module-support-esm branch from 83e9687 to e574c98 Compare March 24, 2021 16:41
@WeiAnAn WeiAnAn marked this pull request as ready for review March 24, 2021 17:18
@WeiAnAn WeiAnAn force-pushed the require-and-transpile-module-support-esm branch from ba420fc to ae4f650 Compare March 24, 2021 17:51
@jakobrosenberg
Copy link

Hi, does globalSetup work with esm without this PR?

@WeiAnAn WeiAnAn force-pushed the require-and-transpile-module-support-esm branch 2 times, most recently from 1010a4d to e5e9f65 Compare March 28, 2021 16:17
@WeiAnAn
Copy link
Contributor Author

WeiAnAn commented Mar 28, 2021

Hi, does globalSetup work with esm without this PR?

No.

@codecov-io
Copy link

codecov-io commented Mar 28, 2021

Codecov Report

Merging #11232 (95b7825) into master (2d96526) will decrease coverage by 0.02%.
The diff coverage is 4.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #11232      +/-   ##
==========================================
- Coverage   64.24%   64.22%   -0.03%     
==========================================
  Files         308      308              
  Lines       13499    13504       +5     
  Branches     3288     3290       +2     
==========================================
  Hits         8673     8673              
- Misses       4117     4122       +5     
  Partials      709      709              
Impacted Files Coverage Δ
packages/jest-core/src/runGlobalHook.ts 22.72% <0.00%> (+0.98%) ⬆️
packages/jest-runner/src/runTest.ts 1.98% <0.00%> (ø)
packages/jest-transform/src/ScriptTransformer.ts 73.37% <0.00%> (-0.26%) ⬇️
packages/jest-util/src/requireOrImportModule.ts 0.00% <0.00%> (ø)
packages/jest-core/src/TestScheduler.ts 68.18% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2d96526...95b7825. Read the comment docs.

e2e/test-environment-esm/__tests__/custom.test.js Outdated Show resolved Hide resolved
e2e/transform/transform-esm-runner/runner.mjs Outdated Show resolved Hide resolved
Comment on lines +9 to +10
import testResult from '@jest/test-result';

const {createEmptyTestResult} = testResult;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import testResult from '@jest/test-result';
const {createEmptyTestResult} = testResult;
import {createEmptyTestResult} from '@jest/test-result';

does this work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No.
Because ts will compile to common js modules, and export using Object.defineProperty with getter.
ESM named import from CJS module only support exports.name = value way. esm commonjs namespaces

CI error log

Choose a reason for hiding this comment

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

Tested. It worked.

packages/jest-transform/src/ScriptTransformer.ts Outdated Show resolved Hide resolved
Copy link

@hieuhuynhdev hieuhuynhdev left a comment

Choose a reason for hiding this comment

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

That looks good to me

@WeiAnAn WeiAnAn requested a review from SimenB April 10, 2021 14:19
@SimenB
Copy link
Member

SimenB commented Apr 14, 2021

@WeiAnAn thanks, this looks really good! We've gotten a few PRs from first time contributors adding support for some which overlap with some you've added here. I think I'm gonna merge those even though this PR handles it a bit cleaner, then we'll need to resolve conflicts here

@SimenB
Copy link
Member

SimenB commented Apr 30, 2021

Hey @WeiAnAn! Finally landed the other open PRs, so if you could rebase this that'd be awesome 👍

@WeiAnAn WeiAnAn force-pushed the require-and-transpile-module-support-esm branch from fccb777 to 02a697a Compare May 1, 2021 19:48
@codecov-commenter
Copy link

codecov-commenter commented May 1, 2021

Codecov Report

Merging #11232 (884c7e0) into master (26cb29a) will increase coverage by 0.07%.
The diff coverage is 3.44%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #11232      +/-   ##
==========================================
+ Coverage   64.10%   64.18%   +0.07%     
==========================================
  Files         308      308              
  Lines       13532    13516      -16     
  Branches     3297     3293       -4     
==========================================
  Hits         8675     8675              
+ Misses       4142     4126      -16     
  Partials      715      715              
Impacted Files Coverage Δ
packages/jest-core/src/runGlobalHook.ts 22.72% <0.00%> (+7.10%) ⬆️
packages/jest-runner/src/runTest.ts 1.98% <0.00%> (+0.21%) ⬆️
packages/jest-snapshot/src/SnapshotResolver.ts 96.66% <ø> (ø)
packages/jest-transform/src/ScriptTransformer.ts 73.37% <0.00%> (-0.26%) ⬇️
packages/jest-util/src/requireOrImportModule.ts 0.00% <0.00%> (ø)
packages/jest-core/src/TestScheduler.ts 68.58% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 26cb29a...884c7e0. Read the comment docs.

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! I think we can just trim the changelog a bit, and remove a test which is already covered by othe rtests

CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
e2e/__tests__/esmGlobalSetup.test.ts Outdated Show resolved Hide resolved
e2e/__tests__/esmGlobalTeardown.test.ts Outdated Show resolved Hide resolved
@@ -27,6 +27,7 @@ const customTransformDIR = path.join(
const nodeModulesDIR = path.join(tmpdir(), 'jest-global-setup-node-modules');
const rejectionDir = path.join(tmpdir(), 'jest-global-setup-rejection');
const e2eDir = path.resolve(__dirname, '../global-setup');
const esmTmpDir = path.join(tmpdir(), 'jest-global-setup-esm');
Copy link
Member

Choose a reason for hiding this comment

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

is this (and the one in teardown) used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the directory is created in setup file.

Copy link
Member

Choose a reason for hiding this comment

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

sure, but what is that directory used for? In the test it looks like it's created and deleted but never used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GlobalSetup/Teardown will read this directory and do some assertion.
If we don't clean directory before testing, it will failed in second round.

Copy link
Member

Choose a reason for hiding this comment

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

even after 884c7e0 (#11232)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.
It fix #11267 globalSetup/globalTeardown e2e test.

Copy link
Member

Choose a reason for hiding this comment

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

Aha, gotcha!

@SimenB SimenB merged commit 2abd495 into jestjs:master May 2, 2021
@github-actions
Copy link

github-actions bot commented Jun 2, 2021

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 Jun 2, 2021
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.

8 participants