Skip to content

Commit 6c32a93

Browse files
committed
fix(config): jsx should also be considered as js files
1 parent 952cc87 commit 6c32a93

5 files changed

Lines changed: 46 additions & 13 deletions

File tree

src/ts-jest-transformer.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Array [
117117

118118
it('should return stringified version of file', () => {
119119
config.shouldStringifyContent.mockImplementation(() => true)
120-
expect(process()).toMatchInlineSnapshot(`"ts:module.exports=\\"export default \\\\\\"foo\\\\\\"\\""`)
120+
expect(process()).toMatchInlineSnapshot(`"module.exports=\\"export default \\\\\\"foo\\\\\\"\\""`)
121121
})
122122

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

139+
it('should warn when trying to process unknown file types', () => {
140+
args[1] = '/foo/bar.jest'
141+
const logs = logTargetMock()
142+
logs.clear()
143+
expect(process()).toBe(INPUT)
144+
expect(logs.lines.warn).toMatchInlineSnapshot(`
145+
Array [
146+
"[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.
147+
",
148+
]
149+
`)
150+
logs.clear()
151+
babel = { process: jest.fn(s => `babel:${s}`) }
152+
expect(process()).toBe(`babel:${INPUT}`)
153+
expect(logs.lines.warn).toMatchInlineSnapshot(`
154+
Array [
155+
"[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.
156+
",
157+
]
158+
`)
159+
})
160+
139161
it('should not pass the instrument option to babel-jest', () => {
140162
babel = { process: jest.fn(s => `babel:${s}`) }
141163
args[3] = { instrument: true }

src/ts-jest-transformer.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,30 +89,38 @@ export class TsJestTransformer implements jest.Transformer {
8989
): jest.TransformedSource | string {
9090
this.logger.debug({ fileName: filePath, transformOptions }, 'processing', filePath)
9191
let result: string | jest.TransformedSource
92-
let source: string = input
92+
const source: string = input
9393

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

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

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

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

118126
// calling babel-jest transformer

src/util/messages.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export enum Errors {
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}}',
1515
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',
16+
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.',
17+
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.',
1618
}
1719

1820
export enum Helps {

tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"sourceMap": false,
55
"inlineSources": false,
66
"inlineSourceMap": false,
7-
"removeComments": true,
7+
"noEmit": false,
88
"outDir": "dist",
99
"rootDir": "src",
1010
},

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compilerOptions": {
3-
"declaration": true,
3+
"declaration": false,
4+
"noEmit": true,
45
"downlevelIteration": true,
56
"esModuleInterop": true,
67
"experimentalDecorators": true,
@@ -18,7 +19,7 @@
1819
"noImplicitReturns": true,
1920
"noUnusedLocals": true,
2021
"noUnusedParameters": true,
21-
"removeComments": false,
22+
"removeComments": true,
2223
"resolveJsonModule": true,
2324
"skipLibCheck": true,
2425
"sourceMap": false,

0 commit comments

Comments
 (0)