Skip to content

Commit

Permalink
Merge branch 'runtime-require-resolve-outside' into uglier-inline-sna…
Browse files Browse the repository at this point in the history
…pshots

* runtime-require-resolve-outside:
  use Symbol key for resolve option
  • Loading branch information
jeysal committed Apr 28, 2020
2 parents fc086cc + a2a05bd commit a6d6d36
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
Expand Up @@ -43,7 +43,7 @@ describe('Runtime require.resolve', () => {
);
});

describe('with the outsideJestVm option', () => {
describe('with the OUTSIDE_JEST_VM_RESOLVE_OPTION', () => {
it('forwards to the real Node require in an internal context', async () => {
const runtime = await createRuntime(__filename);
const module = runtime.requireInternalModule(
Expand Down
Expand Up @@ -7,7 +7,9 @@

'use strict';

const path = require.resolve('./create_require_module', {outsideJestVm: true});
const path = require.resolve('./create_require_module', {
[Symbol.for('OUTSIDE_JEST_VM_RESOLVE_OPTION')]: true,
});
if (typeof path !== 'string') {
throw new Error('require.resolve not spec-compliant: must return a string');
}
Expand Down
10 changes: 8 additions & 2 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -86,8 +86,11 @@ type InitialModule = Partial<Module> &
Pick<Module, 'children' | 'exports' | 'filename' | 'id' | 'loaded'>;
type ModuleRegistry = Map<string, InitialModule | Module>;

const OUTSIDE_JEST_VM_RESOLVE_OPTION = Symbol.for(
'OUTSIDE_JEST_VM_RESOLVE_OPTION',
);
type ResolveOptions = Parameters<typeof require.resolve>[1] & {
outsideJestVm?: true;
[OUTSIDE_JEST_VM_RESOLVE_OPTION]?: true;
};

type BooleanObject = Record<string, boolean>;
Expand Down Expand Up @@ -1289,7 +1292,10 @@ class Runtime {
moduleName,
resolveOptions,
);
if (resolveOptions?.outsideJestVm && options?.isInternalModule) {
if (
resolveOptions?.[OUTSIDE_JEST_VM_RESOLVE_OPTION] &&
options?.isInternalModule
) {
return createOutsideJestVmPath(resolved);
}
return resolved;
Expand Down
19 changes: 10 additions & 9 deletions packages/jest-snapshot/src/inline_snapshots.ts
Expand Up @@ -21,20 +21,21 @@ type BabelTraverse = typeof traverse;
const babelTraverse: BabelTraverse = require(require.resolve(
'@babel/traverse',
{
outsideJestVm: true,
[Symbol.for('OUTSIDE_JEST_VM_RESOLVE_OPTION')]: true,
} as any,
)).default;
const generate: typeof import('@babel/generator')['default'] = require(require.resolve(
'@babel/generator',
{outsideJestVm: true} as any,
{[Symbol.for('OUTSIDE_JEST_VM_RESOLVE_OPTION')]: true} as any,
)).default;
const {
file,
templateElement,
templateLiteral,
} = require(require.resolve('@babel/types', {outsideJestVm: true} as any));
const {file, templateElement, templateLiteral} = require(require.resolve(
'@babel/types',
{
[Symbol.for('OUTSIDE_JEST_VM_RESOLVE_OPTION')]: true,
} as any,
));
const {parseSync} = require(require.resolve('@babel/core', {
outsideJestVm: true,
[Symbol.for('OUTSIDE_JEST_VM_RESOLVE_OPTION')]: true,
} as any)) as typeof import('@babel/core');

export type InlineSnapshot = {
Expand All @@ -50,7 +51,7 @@ export function saveInlineSnapshots(
// TODO same as above
const prettier = prettierPath
? (require(require.resolve(prettierPath, {
outsideJestVm: true,
[Symbol.for('OUTSIDE_JEST_VM_RESOLVE_OPTION')]: true,
} as any)) as typeof import('prettier'))
: null;

Expand Down

0 comments on commit a6d6d36

Please sign in to comment.