Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add TypeScript module specs to examples #3963

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 20 additions & 1 deletion examples/js-with-babel/jest-esm-isolated.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/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,
Expand Down
21 changes: 20 additions & 1 deletion examples/js-with-babel/jest-esm.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/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,
Expand Down
21 changes: 20 additions & 1 deletion examples/js-with-babel/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/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,
Expand Down
21 changes: 20 additions & 1 deletion examples/js-with-babel/jest.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/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,
Expand Down
35 changes: 35 additions & 0 deletions examples/js-with-babel/mjs-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
void `
type ModuleResolverOptions = {
readonly conditions: unknown
defaultResolver(path: string, options: Readonly<ModuleResolverOptions>): 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>,
) => 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
11 changes: 11 additions & 0 deletions examples/js-with-babel/src/welcome-message-module.spec.mts
Original file line number Diff line number Diff line change
@@ -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!!!"`)
})
19 changes: 19 additions & 0 deletions examples/js-with-ts/jest-esm-isolated.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/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',
{
Expand Down
19 changes: 19 additions & 0 deletions examples/js-with-ts/jest-esm.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/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',
{
Expand Down
21 changes: 20 additions & 1 deletion examples/js-with-ts/jest-isolated.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/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,
Expand Down
21 changes: 21 additions & 0 deletions examples/js-with-ts/jest.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/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
}
35 changes: 35 additions & 0 deletions examples/js-with-ts/mjs-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
void `
type ModuleResolverOptions = {
readonly conditions: unknown
defaultResolver(path: string, options: Readonly<ModuleResolverOptions>): 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>,
) => 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
11 changes: 11 additions & 0 deletions examples/js-with-ts/src/welcome-message-module.spec.mts
Original file line number Diff line number Diff line change
@@ -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!!!"`)
})
21 changes: 20 additions & 1 deletion examples/ts-only/jest-esm-isolated.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/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,
Expand Down
21 changes: 20 additions & 1 deletion examples/ts-only/jest-esm.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/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',
Expand Down
Loading