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(runtime): add support for async code transformation #11191

Merged
merged 4 commits into from
Mar 13, 2021

Conversation

SimenB
Copy link
Member

@SimenB SimenB commented Mar 13, 2021

Summary

Fixes #9504

Test plan

e2e test added

@netlify
Copy link

netlify bot commented Mar 13, 2021

Deploy preview for jestjs ready!

Built without sensitive environment variables with commit 1495e07

https://deploy-preview-11191--jestjs.netlify.app

@netlify
Copy link

netlify bot commented Mar 13, 2021

Deploy preview for jestjs ready!

Built without sensitive environment variables with commit 23e5f42

https://deploy-preview-11191--jestjs.netlify.app

@netlify
Copy link

netlify bot commented Mar 13, 2021

Deploy preview for jestjs ready!

Built without sensitive environment variables with commit 507c635

https://deploy-preview-11191--jestjs.netlify.app

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

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

Is it worth to add a test for async .mjs transform as well?

@codecov-io
Copy link

Codecov Report

Merging #11191 (507c635) into master (858c50b) will decrease coverage by 0.04%.
The diff coverage is 50.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #11191      +/-   ##
==========================================
- Coverage   64.31%   64.27%   -0.05%     
==========================================
  Files         307      307              
  Lines       13444    13458      +14     
  Branches     3281     3285       +4     
==========================================
+ Hits         8647     8650       +3     
- Misses       4090     4101      +11     
  Partials      707      707              
Impacted Files Coverage Δ
packages/jest-runtime/src/index.ts 54.85% <50.00%> (-0.74%) ⬇️

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 858c50b...507c635. Read the comment docs.

@SimenB
Copy link
Member Author

SimenB commented Mar 13, 2021

@thymikee this is ESM already as we cannot support async transformation for CJS, although via type: 'module' in package.json rather than mjs extension.

Or am I misunderstanding your question? 😅 Did you mean the transformer should be ESM?

@thymikee
Copy link
Collaborator

I meant the file to be my-transform.mjs and checking if a transform can be written in ESM instead of CJS. But I'm not really up to the speed with all the ESM changes, so pardon if I ask some dummy questions 🙈

@SimenB
Copy link
Member Author

SimenB commented Mar 13, 2021

Ah! I added a test for that together with the feature in #11163.

This PR is that the transformation can be async, not transformers. Should rename something...

@SimenB SimenB changed the title feat(runtime): add support for async transforms feat(runtime): add support for async code transformation Mar 13, 2021
@thymikee
Copy link
Collaborator

Perfect, thanks! I see these are separate responsibilities, could have checked it before asking :)

@SimenB
Copy link
Member Author

SimenB commented Mar 13, 2021

No problem 👍

@SimenB SimenB merged commit dd13096 into jestjs:master Mar 13, 2021
@SimenB SimenB deleted the async-runtime-transform branch March 13, 2021 15:58
@@ -65,7 +65,7 @@ export interface TransformOptions<OptionType = unknown>

export interface SyncTransformer<OptionType = unknown> {
canInstrument?: boolean;
createTransformer?: (options?: OptionType) => SyncTransformer;
createTransformer?: (options?: OptionType) => SyncTransformer<OptionType>;
Copy link
Member Author

Choose a reason for hiding this comment

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

all of the type changes here should have been in #9889, but I didn't test writing a transformer before merging it, just calling it from runtime. So snuck it in here 😀

@ahnpnl
Copy link
Contributor

ahnpnl commented Mar 16, 2021

Hmm, I observed something odd. After adopting next-5, transformer lost this value in getCacheKey and process when running with Node ESM. That doesn't occur with Node CommonJS. Is this behavior expected or is it a bug?

An example is

const path = require('path');

class MyTransformer {
    process(src, filename, config, options) {
         console.log('this value', this); // this is undefined when running Jest in ESM mode

         return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';';
    },
}

module.exports = {
  createTransformer: () => new MyTransformer(),
};

@SimenB
Copy link
Member Author

SimenB commented Mar 16, 2021

Please open up a separate issue.

Note that we've never really supported using classes, so I don't think we've ever officially supported any form of this. However, I'm surprised this changed between next.4 and next.5

@ahnpnl
Copy link
Contributor

ahnpnl commented Mar 16, 2021

oh nvm, i think something funky with my node_modules :)

@github-actions
Copy link

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 May 10, 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.

Implement async code transformations
5 participants