From 5fe3c44b03ce847e7a670119d30d54e6ea526f5f Mon Sep 17 00:00:00 2001 From: "Alexander J. Vincent" Date: Sat, 7 Jan 2023 19:56:20 -0800 Subject: [PATCH] test: add TypeScript module specs to examples This is to show existing support for TypeScript modules. --- .../js-with-babel/jest-esm-isolated.config.js | 21 ++++++++++- examples/js-with-babel/jest-esm.config.js | 21 ++++++++++- .../js-with-babel/jest-isolated.config.js | 21 ++++++++++- examples/js-with-babel/jest.config.js | 21 ++++++++++- examples/js-with-babel/mjs-resolver.ts | 35 +++++++++++++++++++ .../src/welcome-message-module.spec.mts | 11 ++++++ .../js-with-ts/jest-esm-isolated.config.js | 19 ++++++++++ examples/js-with-ts/jest-esm.config.js | 19 ++++++++++ examples/js-with-ts/jest-isolated.config.js | 21 ++++++++++- examples/js-with-ts/jest.config.js | 21 +++++++++++ examples/js-with-ts/mjs-resolver.ts | 35 +++++++++++++++++++ .../src/welcome-message-module.spec.mts | 11 ++++++ examples/ts-only/jest-esm-isolated.config.js | 21 ++++++++++- examples/ts-only/jest-esm.config.js | 21 ++++++++++- examples/ts-only/jest-isolated.config.js | 21 ++++++++++- examples/ts-only/jest.config.js | 21 +++++++++++ examples/ts-only/mjs-resolver.ts | 35 +++++++++++++++++++ .../src/welcome-message-module.spec.mts | 5 +++ .../type-module/jest-esm-isolated.config.js | 21 ++++++++++- examples/type-module/jest-esm.config.js | 21 ++++++++++- examples/type-module/jest-isolated.config.js | 21 ++++++++++- examples/type-module/jest.config.js | 21 +++++++++++ examples/type-module/mjs-resolver.ts | 35 +++++++++++++++++++ .../src/welcome-message-module.spec.mts | 5 +++ 24 files changed, 493 insertions(+), 11 deletions(-) create mode 100644 examples/js-with-babel/mjs-resolver.ts create mode 100644 examples/js-with-babel/src/welcome-message-module.spec.mts create mode 100644 examples/js-with-ts/mjs-resolver.ts create mode 100644 examples/js-with-ts/src/welcome-message-module.spec.mts create mode 100644 examples/ts-only/mjs-resolver.ts create mode 100644 examples/ts-only/src/welcome-message-module.spec.mts create mode 100644 examples/type-module/mjs-resolver.ts create mode 100644 examples/type-module/src/welcome-message-module.spec.mts diff --git a/examples/js-with-babel/jest-esm-isolated.config.js b/examples/js-with-babel/jest-esm-isolated.config.js index 21f87ed93d..36dab3e5c5 100644 --- a/examples/js-with-babel/jest-esm-isolated.config.js +++ b/examples/js-with-babel/jest-esm-isolated.config.js @@ -1,8 +1,27 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-babel-esm', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + // #endregion support for .mts files as an extension + transform: { - '^.+\\.tsx?$': [ + // m? for modules + '^.+\\.m?tsx?$': [ 'ts-jest', { babelConfig: true, diff --git a/examples/js-with-babel/jest-esm.config.js b/examples/js-with-babel/jest-esm.config.js index 03cfa1ca30..a5be61a9e3 100644 --- a/examples/js-with-babel/jest-esm.config.js +++ b/examples/js-with-babel/jest-esm.config.js @@ -1,8 +1,27 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-babel-esm', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + // #endregion support for .mts files as an extension + transform: { - '^.+\\.tsx?$': [ + // m? for modules + '^.+\\.m?tsx?$': [ 'ts-jest', { babelConfig: true, diff --git a/examples/js-with-babel/jest-isolated.config.js b/examples/js-with-babel/jest-isolated.config.js index e6c14d7ce7..26f46f31e4 100644 --- a/examples/js-with-babel/jest-isolated.config.js +++ b/examples/js-with-babel/jest-isolated.config.js @@ -1,8 +1,27 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-babel', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + // #endregion support for .mts files as an extension + transform: { - '^.+\\.tsx?$': [ + // m? for modules + '^.+\\.m?tsx?$': [ 'ts-jest', { babelConfig: true, diff --git a/examples/js-with-babel/jest.config.js b/examples/js-with-babel/jest.config.js index 83b3802871..1d6fd6e2d1 100644 --- a/examples/js-with-babel/jest.config.js +++ b/examples/js-with-babel/jest.config.js @@ -1,8 +1,27 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-babel', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + // #endregion support for .mts files as an extension + transform: { - '^.+\\.tsx?$': [ + // m? for modules + '^.+\\.m?tsx?$': [ 'ts-jest', { babelConfig: true, diff --git a/examples/js-with-babel/mjs-resolver.ts b/examples/js-with-babel/mjs-resolver.ts new file mode 100644 index 0000000000..5a41a5ac4d --- /dev/null +++ b/examples/js-with-babel/mjs-resolver.ts @@ -0,0 +1,35 @@ +void ` +type ModuleResolverOptions = { + readonly conditions: unknown + defaultResolver(path: string, options: Readonly): ModuleResolver + rootDir: unknown + + /** @see {@link https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/resolve/index.d.ts}, interface Opts */ + readonly baseDir: string + extensions: string | readonly string[] | undefined + moduleDirectory: string | undefined + paths: string | readonly string[] | undefined +} + +type ModuleResolver = ( + path: string, + options: Readonly, +) => ModuleResolverOptions['defaultResolver'] +` + +const mjsResolver /*: ModuleResolver */ = function (path, options) { + const mjsExtRegex = /\.mjs$/i + + const resolver = options.defaultResolver + if (mjsExtRegex.test(path)) { + try { + return resolver(path.replace(mjsExtRegex, '.mts'), options) + } catch { + // use default resolver + } + } + + return resolver(path, options) +} + +module.exports = mjsResolver diff --git a/examples/js-with-babel/src/welcome-message-module.spec.mts b/examples/js-with-babel/src/welcome-message-module.spec.mts new file mode 100644 index 0000000000..190c090a50 --- /dev/null +++ b/examples/js-with-babel/src/welcome-message-module.spec.mts @@ -0,0 +1,11 @@ +import { getWelcomeMessage } from './welcome-message' +import welcomePerson from './welcome-person' + +test('module: should show welcome message', () => { + expect(getWelcomeMessage()).toMatchInlineSnapshot(`"Welcome to ts-jest!!!"`) +}) + +test('module: should show welcome person message', () => { + // @ts-expect-error in ESM mode, `default` is kept after compilation + expect(welcomePerson.default ? welcomePerson.default : welcomePerson).toMatchInlineSnapshot(`"Welcome to ts-jest!!!"`) +}) diff --git a/examples/js-with-ts/jest-esm-isolated.config.js b/examples/js-with-ts/jest-esm-isolated.config.js index 08be5b806e..9832279bb8 100644 --- a/examples/js-with-ts/jest-esm-isolated.config.js +++ b/examples/js-with-ts/jest-esm-isolated.config.js @@ -1,7 +1,26 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-ts-esm', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + // #endregion support for .mts files as an extension + transform: { + // m? for modules '^.+\\.m?[tj]sx?$': [ 'ts-jest', { diff --git a/examples/js-with-ts/jest-esm.config.js b/examples/js-with-ts/jest-esm.config.js index c018ddcc62..0f3e23c693 100644 --- a/examples/js-with-ts/jest-esm.config.js +++ b/examples/js-with-ts/jest-esm.config.js @@ -1,7 +1,26 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-ts-esm', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + // #endregion support for .mts files as an extension + transform: { + // m? for modules '^.+\\.m?[tj]sx?$': [ 'ts-jest', { diff --git a/examples/js-with-ts/jest-isolated.config.js b/examples/js-with-ts/jest-isolated.config.js index f55c5555b8..05feebf1f0 100644 --- a/examples/js-with-ts/jest-isolated.config.js +++ b/examples/js-with-ts/jest-isolated.config.js @@ -1,8 +1,27 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-ts', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + // #endregion support for .mts files as an extension + transform: { - '^.+\\.[tj]sx?$': [ + // m? for modules + '^.+\\.m?[tj]sx?$': [ 'ts-jest', { isolatedModules: true, diff --git a/examples/js-with-ts/jest.config.js b/examples/js-with-ts/jest.config.js index 92763175ee..5c285cf1c2 100644 --- a/examples/js-with-ts/jest.config.js +++ b/examples/js-with-ts/jest.config.js @@ -1,4 +1,25 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { preset: 'ts-jest/presets/js-with-ts', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + + transform: { + '^.+\\.mtsx?$': ['ts-jest'], + }, + // #endregion support for .mts files as an extension } diff --git a/examples/js-with-ts/mjs-resolver.ts b/examples/js-with-ts/mjs-resolver.ts new file mode 100644 index 0000000000..5a41a5ac4d --- /dev/null +++ b/examples/js-with-ts/mjs-resolver.ts @@ -0,0 +1,35 @@ +void ` +type ModuleResolverOptions = { + readonly conditions: unknown + defaultResolver(path: string, options: Readonly): ModuleResolver + rootDir: unknown + + /** @see {@link https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/resolve/index.d.ts}, interface Opts */ + readonly baseDir: string + extensions: string | readonly string[] | undefined + moduleDirectory: string | undefined + paths: string | readonly string[] | undefined +} + +type ModuleResolver = ( + path: string, + options: Readonly, +) => ModuleResolverOptions['defaultResolver'] +` + +const mjsResolver /*: ModuleResolver */ = function (path, options) { + const mjsExtRegex = /\.mjs$/i + + const resolver = options.defaultResolver + if (mjsExtRegex.test(path)) { + try { + return resolver(path.replace(mjsExtRegex, '.mts'), options) + } catch { + // use default resolver + } + } + + return resolver(path, options) +} + +module.exports = mjsResolver diff --git a/examples/js-with-ts/src/welcome-message-module.spec.mts b/examples/js-with-ts/src/welcome-message-module.spec.mts new file mode 100644 index 0000000000..190c090a50 --- /dev/null +++ b/examples/js-with-ts/src/welcome-message-module.spec.mts @@ -0,0 +1,11 @@ +import { getWelcomeMessage } from './welcome-message' +import welcomePerson from './welcome-person' + +test('module: should show welcome message', () => { + expect(getWelcomeMessage()).toMatchInlineSnapshot(`"Welcome to ts-jest!!!"`) +}) + +test('module: should show welcome person message', () => { + // @ts-expect-error in ESM mode, `default` is kept after compilation + expect(welcomePerson.default ? welcomePerson.default : welcomePerson).toMatchInlineSnapshot(`"Welcome to ts-jest!!!"`) +}) diff --git a/examples/ts-only/jest-esm-isolated.config.js b/examples/ts-only/jest-esm-isolated.config.js index 0f6afa535c..a9c9257d90 100644 --- a/examples/ts-only/jest-esm-isolated.config.js +++ b/examples/ts-only/jest-esm-isolated.config.js @@ -1,8 +1,27 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { preset: 'ts-jest/presets/default-esm', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + // #endregion support for .mts files as an extension + transform: { - '^.+\\.tsx?$': [ + // m? for modules + '^.+\\.m?tsx?$': [ 'ts-jest', { isolatedModules: true, diff --git a/examples/ts-only/jest-esm.config.js b/examples/ts-only/jest-esm.config.js index 5ee348a6cf..8e126ffc51 100644 --- a/examples/ts-only/jest-esm.config.js +++ b/examples/ts-only/jest-esm.config.js @@ -1,8 +1,27 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { preset: 'ts-jest/presets/default-esm', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + // #endregion support for .mts files as an extension + transform: { - '^.+\\.tsx?$': [ + // m? for modules + '^.+\\.m?tsx?$': [ 'ts-jest', { tsconfig: 'tsconfig-esm.json', diff --git a/examples/ts-only/jest-isolated.config.js b/examples/ts-only/jest-isolated.config.js index 7e0e8d47e6..89b4f42be0 100644 --- a/examples/ts-only/jest-isolated.config.js +++ b/examples/ts-only/jest-isolated.config.js @@ -1,8 +1,27 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { preset: 'ts-jest', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + // #endregion support for .mts files as an extension + transform: { - '^.+\\.tsx?$': [ + // m? for modules + '^.+\\.m?tsx?$': [ 'ts-jest', { isolatedModules: true, diff --git a/examples/ts-only/jest.config.js b/examples/ts-only/jest.config.js index e83af2939d..a46f987c5a 100644 --- a/examples/ts-only/jest.config.js +++ b/examples/ts-only/jest.config.js @@ -1,4 +1,25 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { preset: 'ts-jest', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + + transform: { + '^.+\\.mtsx?$': ['ts-jest'], + }, + // #endregion support for .mts files as an extension } diff --git a/examples/ts-only/mjs-resolver.ts b/examples/ts-only/mjs-resolver.ts new file mode 100644 index 0000000000..5a41a5ac4d --- /dev/null +++ b/examples/ts-only/mjs-resolver.ts @@ -0,0 +1,35 @@ +void ` +type ModuleResolverOptions = { + readonly conditions: unknown + defaultResolver(path: string, options: Readonly): ModuleResolver + rootDir: unknown + + /** @see {@link https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/resolve/index.d.ts}, interface Opts */ + readonly baseDir: string + extensions: string | readonly string[] | undefined + moduleDirectory: string | undefined + paths: string | readonly string[] | undefined +} + +type ModuleResolver = ( + path: string, + options: Readonly, +) => ModuleResolverOptions['defaultResolver'] +` + +const mjsResolver /*: ModuleResolver */ = function (path, options) { + const mjsExtRegex = /\.mjs$/i + + const resolver = options.defaultResolver + if (mjsExtRegex.test(path)) { + try { + return resolver(path.replace(mjsExtRegex, '.mts'), options) + } catch { + // use default resolver + } + } + + return resolver(path, options) +} + +module.exports = mjsResolver diff --git a/examples/ts-only/src/welcome-message-module.spec.mts b/examples/ts-only/src/welcome-message-module.spec.mts new file mode 100644 index 0000000000..b8527fc44e --- /dev/null +++ b/examples/ts-only/src/welcome-message-module.spec.mts @@ -0,0 +1,5 @@ +import { getWelcomeMessage } from './welcome-message' + +test('module: should show welcome message', () => { + expect(getWelcomeMessage()).toMatchInlineSnapshot(`"Welcome to ts-jest!!!"`) +}) diff --git a/examples/type-module/jest-esm-isolated.config.js b/examples/type-module/jest-esm-isolated.config.js index 3330fc94f0..7405540ce2 100644 --- a/examples/type-module/jest-esm-isolated.config.js +++ b/examples/type-module/jest-esm-isolated.config.js @@ -1,8 +1,27 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ const jestConfig = { preset: 'ts-jest/presets/default-esm', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + // #endregion support for .mts files as an extension + transform: { - '^.+\\.tsx?$': [ + // m? for modules + '^.+\\.m?tsx?$': [ 'ts-jest', { isolatedModules: true, diff --git a/examples/type-module/jest-esm.config.js b/examples/type-module/jest-esm.config.js index 68888b8975..88255b3ce9 100644 --- a/examples/type-module/jest-esm.config.js +++ b/examples/type-module/jest-esm.config.js @@ -1,8 +1,27 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ const jestConfig = { preset: 'ts-jest/presets/default-esm', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + // #endregion support for .mts files as an extension + transform: { - '^.+\\.tsx?$': [ + // m? for modules + '^.+\\.m?tsx?$': [ 'ts-jest', { tsconfig: 'tsconfig.json', diff --git a/examples/type-module/jest-isolated.config.js b/examples/type-module/jest-isolated.config.js index 3a0dfa643c..9f4fb685d2 100644 --- a/examples/type-module/jest-isolated.config.js +++ b/examples/type-module/jest-isolated.config.js @@ -1,8 +1,27 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ const jestConfig = { preset: 'ts-jest', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + // #endregion support for .mts files as an extension + transform: { - '^.+\\.tsx?$': [ + // m? for modules + '^.+\\.m?tsx?$': [ 'ts-jest', { isolatedModules: true, diff --git a/examples/type-module/jest.config.js b/examples/type-module/jest.config.js index 1cd092f8ff..141f919d7f 100644 --- a/examples/type-module/jest.config.js +++ b/examples/type-module/jest.config.js @@ -1,6 +1,27 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ const jestConfig = { preset: 'ts-jest', + + // #region support for .mts files as an extension + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + moduleFileExtensions: ['js', 'ts', 'mts'], + + resolver: '/mjs-resolver.ts', + testMatch: ['**/__tests__/**/*.(m)?[jt]s?(x)', '**/?(*.)+(spec|test).(m)?[tj]s?(x)'], + + transform: { + '^.+\\.mtsx?$': ['ts-jest'], + }, + // #endregion support for .mts files as an extension } export default jestConfig diff --git a/examples/type-module/mjs-resolver.ts b/examples/type-module/mjs-resolver.ts new file mode 100644 index 0000000000..5a41a5ac4d --- /dev/null +++ b/examples/type-module/mjs-resolver.ts @@ -0,0 +1,35 @@ +void ` +type ModuleResolverOptions = { + readonly conditions: unknown + defaultResolver(path: string, options: Readonly): ModuleResolver + rootDir: unknown + + /** @see {@link https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/resolve/index.d.ts}, interface Opts */ + readonly baseDir: string + extensions: string | readonly string[] | undefined + moduleDirectory: string | undefined + paths: string | readonly string[] | undefined +} + +type ModuleResolver = ( + path: string, + options: Readonly, +) => ModuleResolverOptions['defaultResolver'] +` + +const mjsResolver /*: ModuleResolver */ = function (path, options) { + const mjsExtRegex = /\.mjs$/i + + const resolver = options.defaultResolver + if (mjsExtRegex.test(path)) { + try { + return resolver(path.replace(mjsExtRegex, '.mts'), options) + } catch { + // use default resolver + } + } + + return resolver(path, options) +} + +module.exports = mjsResolver diff --git a/examples/type-module/src/welcome-message-module.spec.mts b/examples/type-module/src/welcome-message-module.spec.mts new file mode 100644 index 0000000000..b8527fc44e --- /dev/null +++ b/examples/type-module/src/welcome-message-module.spec.mts @@ -0,0 +1,5 @@ +import { getWelcomeMessage } from './welcome-message' + +test('module: should show welcome message', () => { + expect(getWelcomeMessage()).toMatchInlineSnapshot(`"Welcome to ts-jest!!!"`) +})