Skip to content

Commit

Permalink
[jest-config] Use read-pkg to read package.json. (#2992)
Browse files Browse the repository at this point in the history
* [jest-config] Use `read-pkg` to read `package.json`.

* Lint.

* Remember my ABC's.
  • Loading branch information
wtgtybhertgeghgtwtg authored and cpojer committed Feb 27, 2017
1 parent 1567c02 commit 08b8f73
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/jest-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"jest-regex-util": "^19.0.0",
"jest-resolve": "^19.0.2",
"jest-validate": "^19.0.2",
"pretty-format": "^19.0.0"
"pretty-format": "^19.0.0",
"read-pkg": "^1.0.0"
}
}
30 changes: 30 additions & 0 deletions packages/jest-config/src/__mocks__/read-pkg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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';

const mockFiles = new Map();
function __setMockFiles(newMockFiles) {
mockFiles.clear();
Object.keys(newMockFiles).forEach(fileName => {
mockFiles.set(fileName, newMockFiles[fileName]);
});
}

function readPkg(file) {
const mockFile = mockFiles.get(file);
try {
const json = JSON.parse(mockFile);
return Promise.resolve(json);
} catch (err) {
return Promise.reject(`${file} is not valid JSON.`);
}
}

module.exports = readPkg;
module.exports.__setMockFiles = __setMockFiles;
60 changes: 60 additions & 0 deletions packages/jest-config/src/__tests__/loadFromPackage-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* 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';

jest.mock('read-pkg');

const path = require('path');
const loadFromPackage = require('../loadFromPackage');

describe('loadFromPackage', () => {
const MOCK_FILE_INFO = {
'.': `{
"jest": {
"testMatch": ["match.js"]
}
}`,
'broken': `{
"jest": {
"testMatch": ["match.js"
}
}`,
'withRootDir': `{
"jest": {
"rootDir": "testDir"
}
}`,
'withoutJest': `{
}`,
};

beforeEach(() => {
require('read-pkg').__setMockFiles(MOCK_FILE_INFO);
});

it('loads configuration from a `package.json` at `root`', async () => {
const {config} = await loadFromPackage('.', {});
expect(config.testMatch).toEqual(['match.js']);
});

it('returns a config object even if `jest` is not defined in `package.json`.', async () => {
const {config} = await loadFromPackage('withoutJest', {});
expect(config).toEqual(expect.anything());
});

it('returns null if the `package.json` at `root` cannot be parsed.', async () => {
const result = await loadFromPackage('broken', {});
expect(result).toBeNull();
});

it('resolves `rootDir` if defined.', async () => {
const {config} = await loadFromPackage('withRootDir', {});
expect(path.basename(config.rootDir)).toEqual('testDir');
});
});
2 changes: 1 addition & 1 deletion packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const readRawConfig = (argv, root) => {
return Promise.resolve(normalize(config, argv));
}

return loadFromPackage(path.join(root, 'package.json'), argv)
return loadFromPackage(root, argv)
.then(({config, hasDeprecationWarnings}) => {
if (config) {
return {
Expand Down
12 changes: 4 additions & 8 deletions packages/jest-config/src/loadFromPackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@

import type {Path} from 'types/Config';

const fs = require('fs');
const normalize = require('./normalize');
const path = require('path');
const promisify = require('./lib/promisify');
const readPkg = require('read-pkg');

function loadFromPackage(filePath: Path, argv: Object) {
return promisify(fs.access)(filePath, fs.R_OK).then(
() => {
// $FlowFixMe
const packageData = require(filePath);
function loadFromPackage(root: Path, argv: Object) {
return readPkg(root).then(
packageData => {
const config = packageData.jest || {};
const root = path.dirname(filePath);
config.rootDir = config.rootDir
? path.resolve(root, config.rootDir)
: root;
Expand Down

0 comments on commit 08b8f73

Please sign in to comment.