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

Allow JSON transforms #8278

Merged
merged 11 commits into from Apr 11, 2019
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[@jest/runtime]` Allow custom transforms for JSON dependencies

### Chore & Maintenance

### Performance
Expand Down
Expand Up @@ -44,5 +44,29 @@ describe('Runtime', () => {
const exports = runtime.requireInternalModule(modulePath);
expect(exports()).toBe('internal-module-data');
}));

it('loads JSON modules and applies transforms', () =>
createRuntime(__filename, {
transform: {'^.+\\.json$': './test_json_preprocessor'},
}).then(runtime => {
const modulePath = path.resolve(
path.dirname(runtime.__mockRootPath),
'internal-root.json',
);
const exports = runtime.requireModule(modulePath);
expect(exports).toEqual({foo: 'foo'});
}));

it('loads internal JSON modules without applying transforms', () =>
createRuntime(__filename, {
transform: {'^.+\\.json$': './test_json_preprocessor'},
}).then(runtime => {
const modulePath = path.resolve(
path.dirname(runtime.__mockRootPath),
'internal-root.json',
);
const exports = runtime.requireInternalModule(modulePath);
expect(exports).toEqual({foo: 'bar'});
}));
});
});
@@ -0,0 +1,3 @@
{
"foo": "bar"
}
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

module.exports.process = () => `{"foo": "foo"}`;
MLoughry marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 1 addition & 6 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -27,7 +27,6 @@ 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 @@ -464,11 +463,7 @@ class Runtime {
options: InternalModuleOptions | undefined,
moduleRegistry: ModuleRegistry,
) {
if (path.extname(modulePath) === '.json') {
localModule.exports = this._environment.global.JSON.parse(
stripBOM(fs.readFileSync(modulePath, 'utf8')),
MLoughry marked this conversation as resolved.
Show resolved Hide resolved
);
} else if (path.extname(modulePath) === '.node') {
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
9 changes: 7 additions & 2 deletions packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -331,6 +331,8 @@ 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 @@ -342,11 +344,14 @@ export default class ScriptTransformer {
instrument,
);

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

return {
Expand Down