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
28 changes: 26 additions & 2 deletions src/ts-jest-transformer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import stringify from 'fast-json-stable-stringify'
import { ParsedCommandLine } from 'typescript'

import { logTargetMock } from './__helpers__/mocks'
import { TsJestTransformer } from './ts-jest-transformer'

describe('configFor', () => {
Expand Down Expand Up @@ -31,8 +33,12 @@ describe('lastTransformerId', () => {
describe('process', () => {
let tr: TsJestTransformer
let babel: any
let typescript: ParsedCommandLine
let args: [string, string, any, any]
const config = {
get typescript() {
return typescript
},
shouldStringifyContent: jest.fn(),
get babelJestTransformer() {
return babel
Expand All @@ -53,12 +59,12 @@ describe('process', () => {
.mockImplementation(() => config)
.mockClear()
config.shouldStringifyContent.mockImplementation(() => false).mockClear()
babel = { process: jest.fn(s => `babel:${s}`) }
babel = null
config.tsCompiler.compile.mockImplementation(s => `ts:${s}`).mockClear()
typescript = { options: {} } as any
})

it('should process input without babel', () => {
babel = null
expect(process()).toBe(`ts:${INPUT}`)
expect(config.shouldStringifyContent.mock.calls).toMatchInlineSnapshot(`
Array [
Expand All @@ -78,6 +84,7 @@ Array [
})

it('should process input with babel', () => {
babel = { process: jest.fn(s => `babel:${s}`) }
expect(process()).toBe(`babel:ts:${INPUT}`)
expect(config.shouldStringifyContent.mock.calls).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -113,7 +120,24 @@ Array [
expect(process()).toMatchInlineSnapshot(`"ts:module.exports=\\"export default \\\\\\"foo\\\\\\"\\""`)
})

it('should warn when trying to process js but allowJs is false', () => {
args[1] = '/foo/bar.js'
typescript.options.allowJs = false
const logs = logTargetMock()
logs.clear()
expect(process()).toBe(INPUT)
expect(logs.lines.warn).toMatchInlineSnapshot(`
Array [
"[level:40] Got a \`.js\` file to compile while \`allowJs\` option is not set to \`true\` (file: /foo/bar.js). To fix this:
- if you want TypeScript to process JS files, set \`allowJs\` to \`true\` in your TypeScript config (usually tsconfig.json)
- 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
",
]
`)
})

it('should not pass the instrument option to babel-jest', () => {
babel = { process: jest.fn(s => `babel:${s}`) }
args[3] = { instrument: true }
expect(process()).toBe(`babel:ts:${INPUT}`)
expect(config.babelJestTransformer.process.mock.calls).toMatchInlineSnapshot(`
Expand Down
17 changes: 13 additions & 4 deletions src/ts-jest-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { TsJestGlobalOptions } from './types'
import { parse, stringify } from './util/json'
import { JsonableValue } from './util/jsonable-value'
import { rootLogger } from './util/logger'
import { Errors, interpolate } from './util/messages'
import { sha1 } from './util/sha1'

/**
Expand Down Expand Up @@ -101,10 +102,18 @@ export class TsJestTransformer implements jest.Transformer {
source = `module.exports=${JSON.stringify(source)}`
}

// transpile TS code (source maps are included)
result = filePath.endsWith('.d.ts')
? '' // do not try to compile declaration files
: configs.tsCompiler.compile(source, filePath)
// compilation
if (filePath.endsWith('.d.ts')) {
// do not try to compile declaration files
result = ''
} else if (!configs.typescript.options.allowJs && filePath.endsWith('.js')) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

should this also check for .jsx?

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.

oh yeah, thanks!

// 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 {
// transpile TS code (source maps are included)
result = configs.tsCompiler.compile(source, filePath)
}

// calling babel-jest transformer
if (babelJest) {
Expand Down
1 change: 1 addition & 0 deletions src/util/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum Errors {
NotMappingPathWithEmptyMap = 'Not mapping "{{path}}" because it has no target.',
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',
}

export enum Helps {
Expand Down