From fa27e9b44938b70170dd197b6ae3e698ba72b64e Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 29 Jan 2019 09:03:11 +0100 Subject: [PATCH 1/3] fix: make sure normalize accepts defaults --- CHANGELOG.md | 1 + packages/jest-config/src/ValidConfig.js | 3 ++ .../src/__tests__/normalize.test.js | 39 +++++++++---------- packages/jest-config/src/normalize.js | 4 +- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1082888a5ec0..31284a4d69a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - `[jest-runtime]` Lock down version of `write-file-atomic` ([#7725](https://github.com/facebook/jest/pull/7725)) - `[jest-cli]` Print log entries when logging happens after test environment is torn down ([#7731](https://github.com/facebook/jest/pull/7731)) - `[jest-config]` Do not use a uuid as `name` since that breaks caching ([#7746](https://github.com/facebook/jest/pull/7746)) +- `[jest-config]` Make sure `normalize` can consume `Defaults` without warnings ([#7742](https://github.com/facebook/jest/pull/7742)) ### Chore & Maintenance diff --git a/packages/jest-config/src/ValidConfig.js b/packages/jest-config/src/ValidConfig.js index 238aee5dfee8..9b1e41c07a24 100644 --- a/packages/jest-config/src/ValidConfig.js +++ b/packages/jest-config/src/ValidConfig.js @@ -40,7 +40,10 @@ export default ({ statements: 100, }, }, + cwd: '/root', dependencyExtractor: '/dependencyExtractor.js', + detectLeaks: false, + detectOpenHandles: false, displayName: 'project-name', errorOnDeprecated: false, expand: false, diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index a1d37311d373..df031aadd292 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -10,6 +10,7 @@ import crypto from 'crypto'; import path from 'path'; import {escapeStrForRegex} from 'jest-regex-util'; import normalize from '../normalize'; +import Defaults from '../Defaults'; import {DEFAULT_JS_PATTERN} from '../constants'; @@ -45,6 +46,12 @@ beforeEach(() => { expectedPathAbsAnother = path.join(root, 'another', 'abs', 'path'); require('jest-resolve').findNodeModule = findNodeModule; + + jest.spyOn(console, 'warn'); +}); + +afterEach(() => { + console.warn.mockRestore(); }); it('picks a name based on the rootDir', () => { @@ -122,8 +129,7 @@ describe('rootDir', () => { describe('automock', () => { it('falsy automock is not overwritten', () => { - const consoleWarn = console.warn; - console.warn = jest.fn(); + console.warn.mockImplementation(() => {}); const {options} = normalize( { automock: false, @@ -133,8 +139,6 @@ describe('automock', () => { ); expect(options.automock).toBe(false); - - console.warn = consoleWarn; }); }); @@ -401,21 +405,15 @@ describe('setupFilesAfterEnv', () => { describe('setupTestFrameworkScriptFile', () => { let Resolver; - let consoleWarn; beforeEach(() => { - console.warn = jest.fn(); - consoleWarn = console.warn; + console.warn.mockImplementation(() => {}); Resolver = require('jest-resolve'); Resolver.findNodeModule = jest.fn(name => name.startsWith('/') ? name : '/root/path/foo' + path.sep + name, ); }); - afterEach(() => { - console.warn = consoleWarn; - }); - it('logs a deprecation warning when `setupTestFrameworkScriptFile` is used', () => { normalize( { @@ -425,7 +423,7 @@ describe('setupTestFrameworkScriptFile', () => { {}, ); - expect(consoleWarn.mock.calls[0][0]).toMatchSnapshot(); + expect(console.warn.mock.calls[0][0]).toMatchSnapshot(); }); it('logs an error when `setupTestFrameworkScriptFile` and `setupFilesAfterEnv` are used', () => { @@ -782,11 +780,8 @@ describe('babel-jest', () => { }); describe('Upgrade help', () => { - let consoleWarn; - beforeEach(() => { - consoleWarn = console.warn; - console.warn = jest.fn(); + console.warn.mockImplementation(() => {}); const Resolver = require('jest-resolve'); Resolver.findNodeModule = jest.fn(name => { @@ -797,10 +792,6 @@ describe('Upgrade help', () => { }); }); - afterEach(() => { - console.warn = consoleWarn; - }); - it('logs a warning when `scriptPreprocessor` and/or `preprocessorIgnorePatterns` are used', () => { const {options: options, hasDeprecationWarnings} = normalize( { @@ -1469,3 +1460,11 @@ describe('moduleFileExtensions', () => { ).toThrowError("moduleFileExtensions must include 'js'"); }); }); + +describe('Defaults', () => { + it('should be accepted by normalize', () => { + normalize({...Defaults, rootDir: '/root'}, {}); + + expect(console.warn).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 6abbe34122e3..439bce480f0d 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -200,7 +200,9 @@ const normalizeCollectCoverageFrom = (options: InitialOptions, key: string) => { value = JSON.parse(options[key]); } catch (e) {} - Array.isArray(value) || (value = [options[key]]); + if (value && !Array.isArray(value)) { + value = [options[key]]; + } } else { value = options[key]; } From 910500241a919c2f7240a120792583fedc8ef28b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 29 Jan 2019 09:13:21 +0100 Subject: [PATCH 2/3] remove flags from `DefaultConfig` --- packages/jest-config/src/Defaults.js | 2 -- packages/jest-config/src/ValidConfig.js | 2 -- types/Config.js | 2 -- 3 files changed, 6 deletions(-) diff --git a/packages/jest-config/src/Defaults.js b/packages/jest-config/src/Defaults.js index 0bbdbdc7a432..f4eabb3168dc 100644 --- a/packages/jest-config/src/Defaults.js +++ b/packages/jest-config/src/Defaults.js @@ -31,8 +31,6 @@ export default ({ coverageThreshold: null, cwd: process.cwd(), dependencyExtractor: null, - detectLeaks: false, - detectOpenHandles: false, errorOnDeprecated: false, expand: false, filter: null, diff --git a/packages/jest-config/src/ValidConfig.js b/packages/jest-config/src/ValidConfig.js index 9b1e41c07a24..0b7a06adc64a 100644 --- a/packages/jest-config/src/ValidConfig.js +++ b/packages/jest-config/src/ValidConfig.js @@ -42,8 +42,6 @@ export default ({ }, cwd: '/root', dependencyExtractor: '/dependencyExtractor.js', - detectLeaks: false, - detectOpenHandles: false, displayName: 'project-name', errorOnDeprecated: false, expand: false, diff --git a/types/Config.js b/types/Config.js index d500cf4e7248..f4b14282cdfd 100644 --- a/types/Config.js +++ b/types/Config.js @@ -46,8 +46,6 @@ export type DefaultOptions = {| globalSetup: ?string, globalTeardown: ?string, haste: HasteConfig, - detectLeaks: boolean, - detectOpenHandles: boolean, moduleDirectories: Array, moduleFileExtensions: Array, moduleNameMapper: {[key: string]: string}, From 9904a83f2edcc29fa6f1d18aa534a926782072d7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 29 Jan 2019 18:55:02 +0100 Subject: [PATCH 3/3] oops --- packages/jest-config/src/normalize.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 439bce480f0d..f2304fc71791 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -200,7 +200,7 @@ const normalizeCollectCoverageFrom = (options: InitialOptions, key: string) => { value = JSON.parse(options[key]); } catch (e) {} - if (value && !Array.isArray(value)) { + if (options[key] && !Array.isArray(value)) { value = [options[key]]; } } else {