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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- `[jest-snapshot]` Inline snapshots: do not indent empty lines ([#8277](https://github.com/facebook/jest/pull/8277))
- `[@jest/runtime]` Allow custom transforms for JSON dependencies ([#2578](https://github.com/facebook/jest/pull/2578))

### Chore & Maintenance

Expand Down
Original file line number Diff line number Diff line change
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'});
}));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "bar"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* 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 = source => {
const json = JSON.parse(source);
Object.keys(json).forEach(k => (json[k] = k));
return JSON.stringify(json);
};
7 changes: 1 addition & 6 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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