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
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, @jest/transform]` Allow custom transforms for JSON dependencies ([#2578](https://github.com/facebook/jest/pull/2578))

### Chore & Maintenance

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,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);
};
10 changes: 9 additions & 1 deletion packages/jest-runtime/src/index.ts
Expand Up @@ -465,8 +465,16 @@ class Runtime {
moduleRegistry: ModuleRegistry,
) {
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(
stripBOM(fs.readFileSync(modulePath, 'utf8')),
MLoughry marked this conversation as resolved.
Show resolved Hide resolved
transformedFile,
);
} else if (path.extname(modulePath) === '.node') {
localModule.exports = require(modulePath);
Expand Down
22 changes: 22 additions & 0 deletions packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -405,6 +405,28 @@ export default class ScriptTransformer {
return result;
}

transformJson(
filename: Config.Path,
options: Pick<Options, 'isInternalModule' | 'isCoreModule'> | undefined,
MLoughry marked this conversation as resolved.
Show resolved Hide resolved
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