From 3c325e8bacdc4508d48a9e1612412c8ad0f6fbc7 Mon Sep 17 00:00:00 2001 From: Huafu Gandon Date: Mon, 24 Sep 2018 22:21:08 +0200 Subject: [PATCH] fix(preset): createJestPreset fails with base and no array If no moduleFileExtensions or testMatch is given to the createJestPreset it'd fail --- src/config/create-jest-preset.spec.ts | 43 +++++++++++++++++++++++++++ src/config/create-jest-preset.ts | 10 +++---- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/config/create-jest-preset.spec.ts b/src/config/create-jest-preset.spec.ts index 82bc1f3a5f..75e06a6aea 100644 --- a/src/config/create-jest-preset.spec.ts +++ b/src/config/create-jest-preset.spec.ts @@ -31,3 +31,46 @@ it('should return correct defaults when allowJs is true', () => { }, }) }) + +it('should be able to use a base config', () => { + expect(createJestPreset(undefined, {})).toMatchInlineSnapshot(` +Object { + "moduleFileExtensions": Array [ + "js", + "json", + "jsx", + "node", + "ts", + "tsx", + ], + "testMatch": Array [ + "**/__tests__/**/*.js?(x)", + "**/?(*.)+(spec|test).js?(x)", + "**/__tests__/**/*.ts?(x)", + "**/?(*.)+(spec|test).ts?(x)", + ], + "transform": Object { + "^.+\\\\.tsx?$": "ts-jest", + }, +} +`) + expect(createJestPreset(undefined, { testMatch: ['foo'], moduleFileExtensions: ['bar'], transform: { foo: 'bar' } })) + .toMatchInlineSnapshot(` +Object { + "moduleFileExtensions": Array [ + "bar", + "ts", + "tsx", + ], + "testMatch": Array [ + "foo", + "**/__tests__/**/*.ts?(x)", + "**/?(*.)+(spec|test).ts?(x)", + ], + "transform": Object { + "^.+\\\\.tsx?$": "ts-jest", + "foo": "bar", + }, +} +`) +}) diff --git a/src/config/create-jest-preset.ts b/src/config/create-jest-preset.ts index 07a2976ab0..01334190c7 100644 --- a/src/config/create-jest-preset.ts +++ b/src/config/create-jest-preset.ts @@ -12,18 +12,16 @@ const defaults = jestConfig.defaults || { moduleFileExtensions: ['js', 'json', 'jsx', 'node'], } -export function createJestPreset( - { allowJs = false }: CreateJestPresetOptions = {}, - from: jest.InitialOptions = defaults, -) { +export function createJestPreset({ allowJs = false }: CreateJestPresetOptions = {}, from?: jest.InitialOptions) { logger.debug({ allowJs }, 'creating jest presets', allowJs ? 'handling' : 'not handling', 'JavaScript files') + from = { ...defaults, ...from } return { transform: { ...from.transform, [allowJs ? '^.+\\.[tj]sx?$' : '^.+\\.tsx?$']: 'ts-jest', }, - testMatch: dedup([...from.testMatch, '**/__tests__/**/*.ts?(x)', '**/?(*.)+(spec|test).ts?(x)']), - moduleFileExtensions: dedup([...from.moduleFileExtensions, 'ts', 'tsx']), + testMatch: dedup([...(from.testMatch || []), '**/__tests__/**/*.ts?(x)', '**/?(*.)+(spec|test).ts?(x)']), + moduleFileExtensions: dedup([...(from.moduleFileExtensions || []), 'ts', 'tsx']), } }