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

Add support for setupFiles and snapshotSerializers #4

Merged
merged 4 commits into from
Apr 4, 2022

Conversation

fisker
Copy link
Contributor

@fisker fisker commented Apr 3, 2022

@fisker fisker marked this pull request as draft April 3, 2022 18:36
@fisker
Copy link
Contributor Author

fisker commented Apr 3, 2022

I'm going to test this on Prettier first. Done, works for us. prettier/prettier#12592

@fisker fisker force-pushed the setup branch 2 times, most recently from a09841b to dc3ea69 Compare April 3, 2022 18:50
Comment on lines 41 to 43
if (setup && setup.default) {
await setup.default;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Owner

Choose a reason for hiding this comment

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

Looking at the Jest source code (https://github.com/facebook/jest/blob/main/packages/jest-repl/src/cli/runtime-cli.ts#L112-L123=), we should do something like this:

const { default: setup } = await import(pathToFileURL(setupFile));
if (typeof setup === "function") await setup();

(technically Jest only calls setup() if it comes from a CJS file, but it's easier to always call it than to check the file type).

In Prettier's setup file, you could either:

  • rewrite it to CJS, use dynamic imports in the setup function instead of top-level import declarations, and do module.exports = setup (this also works with Jest's default runner);
  • export a function named then rather than setup, because await import(...) will automatically call then and await it since { then: function } is a promise-like object (this might also work with Jest's default runner);
  • Use export default setup (and not export default setup()), so that it will be awaited because it's a function (this will only work with this runner, since Jest only calls the function for CJS files).

Copy link
Contributor

@SimenB SimenB Apr 4, 2022

Choose a reason for hiding this comment

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

yeah. we would like to call it for ESM as well, but there's no (supported) way of getting the exported result with vm APIs. So our plan there is to tell people to use TLA for ESM and exporting a function from CJS.

@fisker fisker marked this pull request as ready for review April 3, 2022 19:07
Copy link
Owner

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Thanks! 😄

Could you also add them in the README to the "supported features" section?

@nicolo-ribaudo nicolo-ribaudo added the enhancement New feature or request label Apr 3, 2022
Comment on lines 47 to 51
let serializer = await import(pathToFileURL(snapshotSerializer));

if (serializer && serializer.default) {
serializer = serializer.default;
}
Copy link
Owner

Choose a reason for hiding this comment

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

This should be enough, since the serializer is always stored on module.exports and thus becomes the default export:

const { default: serializer } = await import(pathToFileURL(snapshotSerializer));

Also, probably we should add them in reverse order: https://github.com/facebook/jest/blob/a20bd2c31e126fc998c2407cfc6c1ecf39ead709/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts#L149-L152

@fisker
Copy link
Contributor Author

fisker commented Apr 4, 2022

All review points addressed.

@nicolo-ribaudo
Copy link
Owner

Thanks! I'll add a CI job that tests this runner both with Babel's and Prettier's test suites, then I'll release a new version.

@nicolo-ribaudo nicolo-ribaudo merged commit 1c31422 into nicolo-ribaudo:main Apr 4, 2022
@fisker fisker deleted the setup branch April 5, 2022 01:39
@fisker
Copy link
Contributor Author

fisker commented Apr 5, 2022

Thank you!

@fisker
Copy link
Contributor Author

fisker commented Apr 5, 2022

FYI: yarn test:format on next branch is what are we using.

@fisker
Copy link
Contributor Author

fisker commented Apr 5, 2022

I'm going to work on setting up Prettier's test.

@fisker fisker mentioned this pull request Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants