-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [jest-config] Use `read-pkg` to read `package.json`. * Lint. * Remember my ABC's.
- Loading branch information
1 parent
1567c02
commit 08b8f73
Showing
5 changed files
with
97 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
60
packages/jest-config/src/__tests__/loadFromPackage-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters