-
-
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
Allow configuration object in projects array #5176
Changes from 7 commits
0d4e3b7
0a4cf98
75615e6
fb2b4d4
50d01f7
b61f30e
b227bad
3e4c825
d91e999
c9d5fec
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,14 @@ | ||
import {readConfig} from '../index'; | ||
|
||
test('readConfig() throws when an object is passed without a file path', () => { | ||
expect(() => { | ||
readConfig( | ||
null /* argv */, | ||
{} /* packageRootOrConfig */, | ||
false /* skipArgvConfigOption */, | ||
null /* parentConfigPath */, | ||
); | ||
}).toThrowError( | ||
'Jest: Cannot use configuration as an object without a file path', | ||
); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,33 +8,52 @@ | |
*/ | ||
|
||
import type {Argv} from 'types/Argv'; | ||
import type {GlobalConfig, Path, ProjectConfig} from 'types/Config'; | ||
import type { | ||
GlobalConfig, | ||
InitialOptions, | ||
Path, | ||
ProjectConfig, | ||
} from 'types/Config'; | ||
|
||
import {getTestEnvironment, isJSONString} from './utils'; | ||
import path from 'path'; | ||
import {isJSONString} from './utils'; | ||
import normalize from './normalize'; | ||
import resolveConfigPath from './resolve_config_path'; | ||
import readConfigFileAndSetRootDir from './read_config_file_and_set_root_dir'; | ||
|
||
function readConfig( | ||
export {getTestEnvironment, isJSONString} from './utils'; | ||
export {default as normalize} from './normalize'; | ||
|
||
export function readConfig( | ||
argv: Argv, | ||
packageRoot: string, | ||
packageRootOrConfig: Path | InitialOptions, | ||
// Whether it needs to look into `--config` arg passed to CLI. | ||
// It only used to read initial config. If the initial config contains | ||
// `project` property, we don't want to read `--config` value and rather | ||
// read individual configs for every project. | ||
skipArgvConfigOption?: boolean, | ||
parentConfigPath: ?Path, | ||
): { | ||
configPath: ?Path, | ||
globalConfig: GlobalConfig, | ||
hasDeprecationWarnings: boolean, | ||
projectConfig: ProjectConfig, | ||
} { | ||
let rawOptions; | ||
let configPath; | ||
let configPath = null; | ||
|
||
// A JSON string was passed to `--config` argument and we can parse it | ||
// and use as is. | ||
if (isJSONString(argv.config)) { | ||
if (typeof packageRootOrConfig !== 'string') { | ||
if (parentConfigPath) { | ||
rawOptions = packageRootOrConfig; | ||
rawOptions.rootDir = path.dirname(parentConfigPath); | ||
} else { | ||
throw new Error( | ||
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 we have a test for this (mostly for coverage)? |
||
'Jest: Cannot use configuration as an object without a file path.', | ||
); | ||
} | ||
} else if (isJSONString(argv.config)) { | ||
// A JSON string was passed to `--config` argument and we can parse it | ||
// and use as is. | ||
let config; | ||
try { | ||
config = JSON.parse(argv.config); | ||
|
@@ -45,7 +64,7 @@ function readConfig( | |
} | ||
|
||
// NOTE: we might need to resolve this dir to an absolute path in the future | ||
config.rootDir = config.rootDir || packageRoot; | ||
config.rootDir = config.rootDir || packageRootOrConfig; | ||
rawOptions = config; | ||
// A string passed to `--config`, which is either a direct path to the config | ||
// or a path to directory containing `package.json` or `jest.conf.js` | ||
|
@@ -54,7 +73,7 @@ function readConfig( | |
rawOptions = readConfigFileAndSetRootDir(configPath); | ||
} else { | ||
// Otherwise just try to find config in the current rootDir. | ||
configPath = resolveConfigPath(packageRoot, process.cwd()); | ||
configPath = resolveConfigPath(packageRootOrConfig, process.cwd()); | ||
rawOptions = readConfigFileAndSetRootDir(configPath); | ||
} | ||
|
||
|
@@ -166,10 +185,3 @@ const getConfigs = ( | |
}), | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
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. is this on purpose? 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. Yes it is, you |
||
getTestEnvironment, | ||
isJSONString, | ||
normalize, | ||
readConfig, | ||
}; |
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.
Is there anywhere that requires a
configPath
to be set other thanreadConfigFileAndSetRootDir
?