diff --git a/CHANGELOG.md b/CHANGELOG.md index 45fab0ee4a6d..272a57fad2a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - `[jest-core]` Fix ability to transform dependencies required from globalSetup script [#8143](https://github.com/facebook/jest/pull/8143) - `[@jest/reporters]` Fix Cannot read property converageData of null ([#8168](https://github.com/facebook/jest/pull/8168)) - `[jest-worker]` `JEST_WORKER_ID` starts at 1 ([#8205](https://github.com/facebook/jest/pull/8205)) +- `[jest-config]` Use default cwd even if config contains a cwd property ([#7923](https://github.com/facebook/jest/pull/7923)) ### Chore & Maintenance diff --git a/packages/jest-config/src/Defaults.ts b/packages/jest-config/src/Defaults.ts index 10fed37c6da5..15bde5d04bd2 100644 --- a/packages/jest-config/src/Defaults.ts +++ b/packages/jest-config/src/Defaults.ts @@ -26,7 +26,6 @@ const defaultOptions: Config.DefaultOptions = { coveragePathIgnorePatterns: [NODE_MODULES_REGEXP], coverageReporters: ['json', 'text', 'lcov', 'clover'], coverageThreshold: null, - cwd: process.cwd(), dependencyExtractor: null, errorOnDeprecated: false, expand: false, diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts index fb25df1599bc..638d8f811572 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -38,8 +38,6 @@ const initialOptions: Config.InitialOptions = { statements: 100, }, }, - // @ts-ignore: Missing from initial options... https://github.com/facebook/jest/pull/7923 - cwd: '/root', dependencyExtractor: '/dependencyExtractor.js', displayName: 'project-name', errorOnDeprecated: false, diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 9ccb14ddb43a..e665b1e45886 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -1512,6 +1512,26 @@ describe('moduleFileExtensions', () => { }); }); +describe('cwd', () => { + it('is set to process.cwd', () => { + const {options} = normalize({rootDir: '/root/'}, {}); + expect(options.cwd).toBe(process.cwd()); + }); + + it('is not lost if the config has its own cwd property', () => { + console.warn.mockImplementation(() => {}); + const {options} = normalize( + { + rootDir: '/root/', + cwd: '/tmp/config-sets-cwd-itself', + }, + {}, + ); + expect(options.cwd).toBe(process.cwd()); + expect(console.warn).toHaveBeenCalled(); + }); +}); + describe('Defaults', () => { it('should be accepted by normalize', () => { normalize({...Defaults, rootDir: '/root'}, {}); diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index e43bd523af8a..929c3a17b182 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -486,13 +486,6 @@ export default function normalize( ...DEFAULT_CONFIG, } as unknown) as AllOptions; - try { - // try to resolve windows short paths, ignoring errors (permission errors, mostly) - newOptions.cwd = realpath(newOptions.cwd); - } catch (e) { - // ignored - } - if (options.resolver) { newOptions.resolver = resolve(null, { filePath: options.resolver, @@ -827,6 +820,13 @@ export default function normalize( return newOptions; }, newOptions); + try { + // try to resolve windows short paths, ignoring errors (permission errors, mostly) + newOptions.cwd = realpath(process.cwd()); + } catch (e) { + // ignored + } + newOptions.nonFlagArgs = argv._; newOptions.testPathPattern = buildTestPathPattern(argv); newOptions.json = !!argv.json; diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 76e4d06ef343..7cf1b81b76e3 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -45,7 +45,6 @@ export type DefaultOptions = { } | null | undefined; - cwd: Path; dependencyExtractor: string | null | undefined; errorOnDeprecated: boolean; expand: boolean;