-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Support jest-preset.js files within Node modules. #6185
Changes from 8 commits
6265b89
161dc5e
fb62732
ac8f57f
42ec800
bd11903
f27a49d
c24ac99
7296a55
6786998
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. 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. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
const runJest = require('../runJest'); | ||
|
||
test('supports json preset', () => { | ||
const result = runJest('presets/json'); | ||
expect(result.status).toBe(0); | ||
}); | ||
|
||
test('supports js preset', () => { | ||
const result = runJest('presets/js'); | ||
expect(result.status).toBe(0); | ||
}); |
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 MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
'use strict'; | ||
|
||
test('load file mapped by js preset', () => { | ||
expect(require('./test.foo')).toEqual(42); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"jest": { | ||
"preset": "jest-preset-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 MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
'use strict'; | ||
|
||
test('load file mapped by json preset', () => { | ||
expect(require('./test.foo')).toEqual(42); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"jest": { | ||
"preset": "jest-preset-json" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,8 +36,8 @@ import DEPRECATED_CONFIG from './deprecated'; | |
import setFromArgv from './set_from_argv'; | ||
import VALID_CONFIG from './valid_config'; | ||
const ERROR = `${BULLET}Validation Error`; | ||
const JSON_EXTENSION = '.json'; | ||
const PRESET_NAME = 'jest-preset' + JSON_EXTENSION; | ||
const PRESET_EXTENSIONS = ['.json', '.js']; | ||
const PRESET_NAME = 'jest-preset'; | ||
|
||
const createConfigError = message => | ||
new ValidationError(ERROR, message, DOCUMENTATION_NOTE); | ||
|
@@ -64,17 +64,25 @@ const setupPreset = ( | |
let preset; | ||
const presetPath = replaceRootDirInPath(options.rootDir, optionsPreset); | ||
const presetModule = Resolver.findNodeModule( | ||
presetPath.endsWith(JSON_EXTENSION) | ||
presetPath.charAt(0) === '.' | ||
? presetPath | ||
: path.join(presetPath, PRESET_NAME), | ||
{ | ||
basedir: options.rootDir, | ||
extensions: PRESET_EXTENSIONS, | ||
}, | ||
); | ||
|
||
try { | ||
// $FlowFixMe | ||
preset = (require(presetModule): InitialOptions); | ||
|
||
// Force re-evaluation to support multiple projects | ||
try { | ||
if (presetModule) { | ||
delete require.cache[require.resolve(presetModule)]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you change this to delete before and require after? What you have here is smart, but if user code requires the preset in between, the next re-require of the preset will not actually re-evaluate the module code. |
||
} | ||
} catch (e) {} | ||
} catch (error) { | ||
if (error instanceof SyntaxError) { | ||
throw createConfigError( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do
[0]
instead ofcharAt
. We aren't running this code in IE6.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IE6 <3
You can also do
.startsWith('.')