-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add TypeScript module specs to examples
This is to show existing support for TypeScript modules.
- Loading branch information
Showing
24 changed files
with
493 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
examples/js-with-babel/src/welcome-message-module.spec.mts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!!!"`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!!!"`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.