Skip to content

Commit

Permalink
Re-implement per @scotthovestadt's guidance
Browse files Browse the repository at this point in the history
  • Loading branch information
OWA Framework committed Apr 6, 2019
1 parent ad380b4 commit b4c5a1f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
15 changes: 14 additions & 1 deletion packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
shouldInstrument,
} from '@jest/transform';
import fs from 'graceful-fs';
import stripBOM from 'strip-bom';
import {run as cliRun} from './cli';
import {options as cliOptions} from './cli/args';
import {findSiblingsWithFileExtension} from './helpers';
Expand Down Expand Up @@ -463,7 +464,19 @@ class Runtime {
options: InternalModuleOptions | undefined,
moduleRegistry: ModuleRegistry,
) {
if (path.extname(modulePath) === '.node') {
if (path.extname(modulePath) === '.json') {
const text = stripBOM(fs.readFileSync(modulePath, 'utf8'));

const transformedFile = this._scriptTransformer.transformJson(
modulePath,
options,
text,
);

localModule.exports = this._environment.global.JSON.parse(
transformedFile,
);
} else if (path.extname(modulePath) === '.node') {
localModule.exports = require(modulePath);
} else {
// Only include the fromPath if a moduleName is given. Else treat as root.
Expand Down
31 changes: 24 additions & 7 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,6 @@ export default class ScriptTransformer {
!isInternalModule &&
!isCoreModule &&
(this.shouldTransform(filename) || instrument);
const isJson = path.extname(filename) === '.json';
const prefix = isJson ? 'module.exports = ' : '';

try {
const extraGlobals = (options && options.extraGlobals) || [];
Expand All @@ -344,14 +342,11 @@ export default class ScriptTransformer {
instrument,
);

wrappedCode = wrap(
`${prefix}${transformedSource.code}`,
...extraGlobals,
);
wrappedCode = wrap(transformedSource.code, ...extraGlobals);
sourceMapPath = transformedSource.sourceMapPath;
mapCoverage = transformedSource.mapCoverage;
} else {
wrappedCode = wrap(`${prefix}${content}`, ...extraGlobals);
wrappedCode = wrap(content, ...extraGlobals);
}

return {
Expand Down Expand Up @@ -410,6 +405,28 @@ export default class ScriptTransformer {
return result;
}

transformJson(
filename: Config.Path,
options: Pick<Options, 'isInternalModule' | 'isCoreModule'> | undefined,
fileSource: string,
): string {
const isInternalModule = !!(options && options.isInternalModule);
const isCoreModule = !!(options && options.isCoreModule);
const willTransform =
!isInternalModule && !isCoreModule && this.shouldTransform(filename);

if (willTransform) {
const {code: transformedJsonSource} = this.transformSource(
filename,
fileSource,
false,
);
return transformedJsonSource;
}

return fileSource;
}

/**
* @deprecated use `this.shouldTransform` instead
*/
Expand Down

0 comments on commit b4c5a1f

Please sign in to comment.