Skip to content

Commit

Permalink
fix: allow .mts to be processed (#3713)
Browse files Browse the repository at this point in the history
Fixes #3702
  • Loading branch information
ahnpnl authored Jul 25, 2022
1 parent eef70f6 commit effae71
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export const LINE_FEED = '\n'
export const DECLARATION_TYPE_EXT = '.d.ts'
export const JS_JSX_EXTENSIONS = ['.js', '.jsx']
export const TS_TSX_REGEX = /\.tsx?$/
export const JS_JSX_REGEX = /\.jsx?$/
// `extensionsToTreatAsEsm` only accepts `.ts`, `.tsx` and `.jsx`. `.js`, `.cjs`, `.mjs` will throw error
export const TS_EXT_TO_TREAT_AS_ESM = ['.ts', '.tsx']
export const TS_TSX_REGEX = /\.m?tsx?$/
export const JS_JSX_REGEX = /\.m?jsx?$/
// `extensionsToTreatAsEsm` will throw error with `.mjs`
export const TS_EXT_TO_TREAT_AS_ESM = ['.ts', '.tsx', '.mts']
export const JS_EXT_TO_TREAT_AS_ESM = ['.jsx']
/**
* @internal
Expand Down
47 changes: 25 additions & 22 deletions src/legacy/ts-jest-transformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ describe('TsJestTransformer', () => {
expect(process.env.TS_JEST).toBe('1')
})

test.each(['foo.ts', 'foo.tsx'])('should process ts/tsx file', (filePath) => {
test.each(['foo.ts', 'foo.tsx', 'foo.mts', 'foo.mtsx'])('should process ts/tsx file', (filePath) => {
const fileContent = 'const foo = 1'
const output = 'var foo = 1'
tr.getCacheKey(fileContent, filePath, baseTransformOptions)
Expand All @@ -391,30 +391,33 @@ describe('TsJestTransformer', () => {
})
})

test.each(['foo.js', 'foo.jsx'])('should process js/jsx file with allowJs true', (filePath) => {
const fileContent = 'const foo = 1'
const output = 'var foo = 1'
const transformOptions = {
...baseTransformOptions,
config: {
...baseTransformOptions.config,
globals: {
'ts-jest': { tsconfig: { allowJs: true } },
test.each(['foo.js', 'foo.jsx', 'foo.mjs', 'foo.mjsx'])(
'should process js/jsx file with allowJs true',
(filePath) => {
const fileContent = 'const foo = 1'
const output = 'var foo = 1'
const transformOptions = {
...baseTransformOptions,
config: {
...baseTransformOptions.config,
globals: {
'ts-jest': { tsconfig: { allowJs: true } },
},
},
},
}
tr.getCacheKey(fileContent, filePath, transformOptions)
logTarget.clear()
jest.spyOn(TsJestCompiler.prototype, 'getCompiledOutput').mockReturnValueOnce({
code: output,
})
}
tr.getCacheKey(fileContent, filePath, transformOptions)
logTarget.clear()
jest.spyOn(TsJestCompiler.prototype, 'getCompiledOutput').mockReturnValueOnce({
code: output,
})

const result = tr.process(fileContent, filePath, transformOptions)
const result = tr.process(fileContent, filePath, transformOptions)

expect(result).toEqual({
code: output,
})
})
expect(result).toEqual({
code: output,
})
},
)

test('should process file with unknown extension and show warning message without babel-jest', () => {
const fileContent = 'foo'
Expand Down
1 change: 1 addition & 0 deletions src/presets/__snapshots__/create-jest-preset.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Object {
".jsx",
".ts",
".tsx",
".mts",
],
"moduleFileExtensions": Array [
"bar",
Expand Down

0 comments on commit effae71

Please sign in to comment.