Skip to content

Commit 952cc87

Browse files
committed
feat(hints): warns if transform matches js without allowJs
1 parent 7e494c0 commit 952cc87

3 files changed

Lines changed: 40 additions & 6 deletions

File tree

src/ts-jest-transformer.spec.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import stringify from 'fast-json-stable-stringify'
2+
import { ParsedCommandLine } from 'typescript'
23

4+
import { logTargetMock } from './__helpers__/mocks'
35
import { TsJestTransformer } from './ts-jest-transformer'
46

57
describe('configFor', () => {
@@ -31,8 +33,12 @@ describe('lastTransformerId', () => {
3133
describe('process', () => {
3234
let tr: TsJestTransformer
3335
let babel: any
36+
let typescript: ParsedCommandLine
3437
let args: [string, string, any, any]
3538
const config = {
39+
get typescript() {
40+
return typescript
41+
},
3642
shouldStringifyContent: jest.fn(),
3743
get babelJestTransformer() {
3844
return babel
@@ -53,12 +59,12 @@ describe('process', () => {
5359
.mockImplementation(() => config)
5460
.mockClear()
5561
config.shouldStringifyContent.mockImplementation(() => false).mockClear()
56-
babel = { process: jest.fn(s => `babel:${s}`) }
62+
babel = null
5763
config.tsCompiler.compile.mockImplementation(s => `ts:${s}`).mockClear()
64+
typescript = { options: {} } as any
5865
})
5966

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

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

123+
it('should warn when trying to process js but allowJs is false', () => {
124+
args[1] = '/foo/bar.js'
125+
typescript.options.allowJs = false
126+
const logs = logTargetMock()
127+
logs.clear()
128+
expect(process()).toBe(INPUT)
129+
expect(logs.lines.warn).toMatchInlineSnapshot(`
130+
Array [
131+
"[level:40] Got a \`.js\` file to compile while \`allowJs\` option is not set to \`true\` (file: /foo/bar.js). To fix this:
132+
- if you want TypeScript to process JS files, set \`allowJs\` to \`true\` in your TypeScript config (usually tsconfig.json)
133+
- 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
134+
",
135+
]
136+
`)
137+
})
138+
116139
it('should not pass the instrument option to babel-jest', () => {
140+
babel = { process: jest.fn(s => `babel:${s}`) }
117141
args[3] = { instrument: true }
118142
expect(process()).toBe(`babel:ts:${INPUT}`)
119143
expect(config.babelJestTransformer.process.mock.calls).toMatchInlineSnapshot(`

src/ts-jest-transformer.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { TsJestGlobalOptions } from './types'
66
import { parse, stringify } from './util/json'
77
import { JsonableValue } from './util/jsonable-value'
88
import { rootLogger } from './util/logger'
9+
import { Errors, interpolate } from './util/messages'
910
import { sha1 } from './util/sha1'
1011

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

104-
// transpile TS code (source maps are included)
105-
result = filePath.endsWith('.d.ts')
106-
? '' // do not try to compile declaration files
107-
: configs.tsCompiler.compile(source, filePath)
105+
// compilation
106+
if (filePath.endsWith('.d.ts')) {
107+
// do not try to compile declaration files
108+
result = ''
109+
} else if (!configs.typescript.options.allowJs && filePath.endsWith('.js')) {
110+
// we've got a '.js' but the compiler option `allowJs` is not set or set to false
111+
this.logger.warn({ fileName: filePath }, interpolate(Errors.GotJsFileButAllowJsFalse, { path: filePath }))
112+
result = source
113+
} else {
114+
// transpile TS code (source maps are included)
115+
result = configs.tsCompiler.compile(source, filePath)
116+
}
108117

109118
// calling babel-jest transformer
110119
if (babelJest) {

src/util/messages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export enum Errors {
1212
NotMappingPathWithEmptyMap = 'Not mapping "{{path}}" because it has no target.',
1313
MappingOnlyFirstTargetOfPath = 'Mapping only to first target of "{{path}}" because it has more than one ({{count}}).',
1414
CannotPatchBabelCore6 = 'Error while trying to patch babel-core/lib/transformation/file: {{error}}',
15+
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',
1516
}
1617

1718
export enum Helps {

0 commit comments

Comments
 (0)