Skip to content

Commit

Permalink
Handle JSON modules encoded with a BOM (jestjs#2564)
Browse files Browse the repository at this point in the history
* Handle JSON modules encoded with a BOM

* fixup! Handle JSON modules encoded with a BOM
  • Loading branch information
richardscarrott authored and skovhus committed Apr 29, 2017
1 parent cb7341c commit 04d7e7f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/jest-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"jest-util": "^18.1.0",
"json-stable-stringify": "^1.0.0",
"micromatch": "^2.3.11",
"strip-bom": "3.0.0",
"yargs": "^6.3.0"
},
"bin": {
Expand Down
20 changes: 20 additions & 0 deletions packages/jest-runtime/src/__tests__/Runtime-requireModule-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,24 @@ describe('Runtime requireModule', () => {
]),
);

it('finds modules encoded in UTF-8 *with BOM*', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'./UTF8WithBOM.js',
);
expect(exports).toBe('isModuleEncodedInUTF8WithBOM');
}),
);

it('finds and loads JSON files encoded in UTF-8 *with BOM*', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'./UTF8WithBOM.json',
);
expect(exports.isJSONModuleEncodedInUTF8WithBOM).toBe(true);
}),
);

});
11 changes: 11 additions & 0 deletions packages/jest-runtime/src/__tests__/test_root/UTF8WithBOM.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

module.exports = 'isModuleEncodedInUTF8WithBOM';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"isJSONModuleEncodedInUTF8WithBOM": true}
3 changes: 2 additions & 1 deletion packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const Resolver = require('jest-resolve');

const fs = require('graceful-fs');
const path = require('path');
const stripBOM = require('strip-bom');
const shouldInstrument = require('./shouldInstrument');
const transform = require('./transform');
const {
Expand Down Expand Up @@ -290,7 +291,7 @@ class Runtime {
moduleRegistry[modulePath] = localModule;
if (path.extname(modulePath) === '.json') {
localModule.exports = this._environment.global.JSON.parse(
fs.readFileSync(modulePath, 'utf8'),
stripBOM(fs.readFileSync(modulePath, 'utf8'))
);
} else if (path.extname(modulePath) === '.node') {
// $FlowFixMe
Expand Down

0 comments on commit 04d7e7f

Please sign in to comment.