feat(snapshot): support snapshotResolver and snapshotSerializers written in ESM#12014
feat(snapshot): support snapshotResolver and snapshotSerializers written in ESM#12014chentsulin wants to merge 1 commit intojestjs:mainfrom
Conversation
f8aa171 to
2cab732
Compare
| it('defining tests and hooks asynchronously throws', () => { | ||
| const result = runJest('circus-declaration-errors', [ | ||
| it('defining tests and hooks asynchronously throws', async () => { | ||
| const result = await runJest('circus-declaration-errors', [ |
There was a problem hiding this comment.
This runJest function actually doesn't return a promise, so we should find a way to test this kind of stuff.
This test got failed after I put runtime.requireModule into a async function.
From:
const esm = runtime.unstable_shouldLoadAsEsm(testPath);
if (esm) {
await runtime.unstable_importModule(testPath);
} else {
runtime.requireModule(testPath);
}To:
const localRequire = async <T = unknown>(path: string): Promise<T> => {
const esm = runtime.unstable_shouldLoadAsEsm(path);
if (esm) {
return runtime.unstable_importModule(path) as any;
} else {
return runtime.requireModule(path);
}
};
await localRequire(testPath);There was a problem hiding this comment.
runJest spawns jest in a shell, so it should not be affected by any internal changes in Jest
933e7ca to
b961505
Compare
d19c3af to
ddb4eeb
Compare
| const evaluatedModule = await this.linkAndEvaluateModule(module); | ||
|
|
||
| return evaluatedModule instanceof SourceTextModule | ||
| ? evaluatedModule.namespace.default |
There was a problem hiding this comment.
I have no clue what to do here, but I found this method sometimes returns vm.SourceTextModule as result.
There was a problem hiding this comment.
There is no "result" of executing a module. Do we need access to evaluated module? We haven't needed that before... We might need an unstable_importInternalModule which essentially does what you do here (wait for evaluatedModule.status === 'evaluated', then fetch its default export).
Essentially faking an import() call
There was a problem hiding this comment.
If i didn't get it wrong, the result is used by this localRequire call to support ESM resolvers:
const custom: SnapshotResolver = await localRequire(
snapshotResolverPath,
true,
);| ).default; | ||
| const custom: SnapshotResolver = await localRequire( | ||
| snapshotResolverPath, | ||
| true, |
There was a problem hiding this comment.
Add a boolean parameter to localRequire to support interopRequireDefault here.
d65360d to
d8033b6
Compare
d8033b6 to
f7eb057
Compare
|
@chentsulin I wonder if this could be finalized or if it is waiting for a re-review from @SimenB ?... We need this here: https://github.com/Maxim-Mazurok/google-api-typings-generator/blob/062854a2021be2dd244855261a73f5172bf597c2/custom-resolver.cjs for the use-case of using one snapshot file per test. Thank you! |
|
Main issue is that we load the resolver within the tests (#12014 (comment)), which means it's pretty much blocked by full ESM support |
|
Ah, I see, thank you for the update, we are re-considering our approach now. Cheers! |
|
It might make sense to not load it within, but that means people who have written it in TS or something that requires transpilation will fail. So not sure which tradeoff is the best here |
|
It's been a year. Any updates? |
|
This PR is stale because it has been open 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
|
It's been another year. Any updates? |
|
Could this PR be rebased so we can merge it for Jest 30? |
|
Following up my old comment - I think we can just load the resolver outside of the tests. That gives ESM support, and if people wanna write it in TS they can use https://nodejs.org/en/learn/typescript/run-natively |
Summary
Part of #11167.
See the discussion on the
snapshotResolverandsnapshotSerializersconfig: #11167 (comment) #11167 (comment)Test plan
Integration test added.