Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/ts-jest-transformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Array [

it('should return stringified version of file', () => {
config.shouldStringifyContent.mockImplementation(() => true)
expect(process()).toMatchInlineSnapshot(`"ts:module.exports=\\"export default \\\\\\"foo\\\\\\"\\""`)
expect(process()).toMatchInlineSnapshot(`"module.exports=\\"export default \\\\\\"foo\\\\\\"\\""`)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was actually a tiny "bug", files which should be transformed into modules returning their content were then going thru typescript, which was not needed at all, but not failing of course

})

it('should warn when trying to process js but allowJs is false', () => {
Expand All @@ -136,6 +136,28 @@ Array [
`)
})

it('should warn when trying to process unknown file types', () => {
args[1] = '/foo/bar.jest'
const logs = logTargetMock()
logs.clear()
expect(process()).toBe(INPUT)
expect(logs.lines.warn).toMatchInlineSnapshot(`
Array [
"[level:40] Got a unknown file type to compile (file: /foo/bar.jest). To fix this, in your Jest config change the \`transform\` key which value is \`ts-jest\` so that it does not match this kind of files anymore.
",
]
`)
logs.clear()
babel = { process: jest.fn(s => `babel:${s}`) }
expect(process()).toBe(`babel:${INPUT}`)
expect(logs.lines.warn).toMatchInlineSnapshot(`
Array [
"[level:40] Got a unknown file type to compile (file: /foo/bar.jest). To fix this, in your Jest config change the \`transform\` key which value is \`ts-jest\` so that it does not match this kind of files anymore. If you still want Babel to process it, add another entry to the \`transform\` option with value \`babel-jest\` which key matches this type of files.
",
]
`)
})

it('should not pass the instrument option to babel-jest', () => {
babel = { process: jest.fn(s => `babel:${s}`) }
args[3] = { instrument: true }
Expand Down
26 changes: 17 additions & 9 deletions src/ts-jest-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,38 @@ export class TsJestTransformer implements jest.Transformer {
): jest.TransformedSource | string {
this.logger.debug({ fileName: filePath, transformOptions }, 'processing', filePath)
let result: string | jest.TransformedSource
let source: string = input
const source: string = input

const configs = this.configsFor(jestConfig)
const { hooks } = configs

const stringify = configs.shouldStringifyContent(filePath)
const babelJest = stringify ? undefined : configs.babelJestTransformer

// handles here what we should simply stringify
if (stringify) {
source = `module.exports=${JSON.stringify(source)}`
}
const isDefinitionFile = filePath.endsWith('.d.ts')
const isJsFile = !isDefinitionFile && /\.jsx?$/.test(filePath)
const isTsFile = isDefinitionFile || /\.tsx?$/.test(filePath)

// compilation
if (filePath.endsWith('.d.ts')) {
if (stringify) {
// handles here what we should simply stringify
result = `module.exports=${JSON.stringify(source)}`
} else if (isDefinitionFile) {
// do not try to compile declaration files
result = ''
} else if (!configs.typescript.options.allowJs && filePath.endsWith('.js')) {
} else if (!configs.typescript.options.allowJs && isJsFile) {
// we've got a '.js' but the compiler option `allowJs` is not set or set to false
this.logger.warn({ fileName: filePath }, interpolate(Errors.GotJsFileButAllowJsFalse, { path: filePath }))
result = source
} else {
} else if (isTsFile) {
// transpile TS code (source maps are included)
result = configs.tsCompiler.compile(source, filePath)
} else {
// we should not get called for files with other extension than js[x], ts[x] and d.ts,
// TypeScript will bail if we try to compile, and if it was to call babel, users can
// define the transform value with `babel-jest` for this extension instead
const message = babelJest ? Errors.GotUnknownFileTypeWithBabel : Errors.GotUnknownFileTypeWithoutBabel
this.logger.warn({ fileName: filePath }, interpolate(message, { path: filePath }))
result = source
}

// calling babel-jest transformer
Expand Down
2 changes: 2 additions & 0 deletions src/util/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export enum Errors {
MappingOnlyFirstTargetOfPath = 'Mapping only to first target of "{{path}}" because it has more than one ({{count}}).',
CannotPatchBabelCore6 = 'Error while trying to patch babel-core/lib/transformation/file: {{error}}',
GotJsFileButAllowJsFalse = 'Got a `.js` file to compile while `allowJs` option is not set to `true` (file: {{path}}). To fix this:\n - if you want TypeScript to process JS files, set `allowJs` to `true` in your TypeScript config (usually tsconfig.json)\n - if you do not want TypeScript to process your `.js` files, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match `.js` files anymore',
GotUnknownFileTypeWithoutBabel = 'Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore.',
GotUnknownFileTypeWithBabel = 'Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore. If you still want Babel to process it, add another entry to the `transform` option with value `babel-jest` which key matches this type of files.',
}

export enum Helps {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"sourceMap": false,
"inlineSources": false,
"inlineSourceMap": false,
"removeComments": true,
"noEmit": false,
"outDir": "dist",
"rootDir": "src",
},
Expand Down
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"declaration": true,
"declaration": false,
"noEmit": true,
"downlevelIteration": true,
"esModuleInterop": true,
"experimentalDecorators": true,
Expand All @@ -18,7 +19,7 @@
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": false,
"removeComments": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": false,
Expand Down