Skip to content

Commit

Permalink
feat(preset): adds presets typings and export all presets
Browse files Browse the repository at this point in the history
All presets are now accessible with typings by importing
`ts-jest/presets`
  • Loading branch information
huafu committed Sep 25, 2018
1 parent ad8f6d1 commit f55d895
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
4 changes: 1 addition & 3 deletions presets/default/jest-preset.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
const create = require('../create')

module.exports = create()
module.exports = require('..').defaults
5 changes: 5 additions & 0 deletions presets/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { TsJestPresets } from '../dist/types'

export const defaults: TsJesPresets
export const jsWithTs: TsJesPresets
export const jsWithBabel: TsJesPresets
13 changes: 13 additions & 0 deletions presets/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const create = './create'

module.exports = {
get defaults() { return create() },
get jsWithTs() { return create({ allowJs: true }) },
get jsWithBabel() {
return create({ allowJs: false }, {
transform: {
'^.+\\.jsx?$': 'babel-jest',
},
})
},
}
8 changes: 1 addition & 7 deletions presets/js-with-babel/jest-preset.js
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
const create = require('../create')

module.exports = create({ allowJs: false }, {
transform: {
'^.+\\.jsx?$': 'babel-jest',
},
})
module.exports = require('..').jsWithBabel
4 changes: 1 addition & 3 deletions presets/js-with-ts/jest-preset.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
const create = require('../create')

module.exports = create({allowJs: true})
module.exports = require('..').jsWithTs
7 changes: 5 additions & 2 deletions src/config/create-jest-preset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as jestConfig from 'jest-config'

import { CreateJestPresetOptions } from '../types'
import { CreateJestPresetOptions, TsJestPresets } from '../types'
import { rootLogger } from '../util/logger'

const logger = rootLogger.child({ namespace: 'jest-preset' })
Expand All @@ -12,7 +12,10 @@ const defaults = jestConfig.defaults || {
moduleFileExtensions: ['js', 'json', 'jsx', 'node'],
}

export function createJestPreset({ allowJs = false }: CreateJestPresetOptions = {}, from?: jest.InitialOptions) {
export function createJestPreset(
{ allowJs = false }: CreateJestPresetOptions = {},
from?: jest.InitialOptions,
): TsJestPresets {
logger.debug({ allowJs }, 'creating jest presets', allowJs ? 'handling' : 'not handling', 'JavaScript files')
from = { ...defaults, ...from }
return {
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export interface TsJestGlobalOptions {
stringifyContentPathRegex?: string | RegExp
}

export interface TsJestPresets {
transform: Record<string, string>
testMatch: string[]
moduleFileExtensions: string[]
}

interface TsJestConfig$tsConfig$file {
kind: 'file'
value: string | undefined
Expand Down

0 comments on commit f55d895

Please sign in to comment.