Skip to content

Commit

Permalink
fix(preset): createJestPreset fails with base and no array
Browse files Browse the repository at this point in the history
If no moduleFileExtensions or testMatch is given to the createJestPreset
it'd fail
  • Loading branch information
huafu committed Sep 24, 2018
1 parent e317e3b commit 3c325e8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
43 changes: 43 additions & 0 deletions src/config/create-jest-preset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
`)
})
10 changes: 4 additions & 6 deletions src/config/create-jest-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
}
}

Expand Down

0 comments on commit 3c325e8

Please sign in to comment.