From bbf9f4002886dd6fea70b2bc7ef61520f4268834 Mon Sep 17 00:00:00 2001 From: "anh.pham" Date: Tue, 13 Sep 2022 10:09:17 +0200 Subject: [PATCH] chore(release): 29.0.1 --- CHANGELOG.md | 14 +++ package-lock.json | 4 +- package.json | 2 +- .../versioned_docs/version-29.0/debugging.md | 8 +- .../getting-started/installation.md | 18 +-- .../version-29.0/getting-started/options.md | 65 ++++------- .../options/astTransformers.md | 59 ++++++++-- .../getting-started/options/babelConfig.md | 106 +++++++++++++++--- .../getting-started/options/compiler.md | 25 ++++- .../getting-started/options/diagnostics.md | 105 ++++++++++++++--- .../options/isolatedModules.md | 25 ++++- .../options/stringifyContentPathRegex.md | 28 +++-- .../getting-started/options/tsconfig.md | 77 +++++++++++-- .../getting-started/options/useESM.md | 25 ++++- .../getting-started/paths-mapping.md | 40 ++++++- .../version-29.0/getting-started/presets.md | 44 +++++--- .../version-29.0/guides/esm-support.md | 58 ++++++++-- .../version-29.0/guides/mock-es6-class.md | 34 +++++- .../version-29.0/guides/react-native.md | 39 ++++++- .../versioned_docs/version-29.0/migration.md | 16 +-- 20 files changed, 603 insertions(+), 189 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e59db2cfb0..886e4ddd35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,17 @@ +## [29.0.1](https://github.com/kulshekhar/ts-jest/compare/v29.0.0...v29.0.1) (2022-09-13) + + +### Bug Fixes + +* **legacy:** include existing globals config in cached config ([#3803](https://github.com/kulshekhar/ts-jest/issues/3803)) ([e79be47](https://github.com/kulshekhar/ts-jest/commit/e79be47d2b81a677d0dd39d84328a38ca0f0ffc6)) + + +### Features + +* add typings for `ts-jest` options via `transform` config ([#3805](https://github.com/kulshekhar/ts-jest/issues/3805)) ([664b0f2](https://github.com/kulshekhar/ts-jest/commit/664b0f2b446a36dd7661f4840ca3dd7722f1f6ff)) + + + # [29.0.0](https://github.com/kulshekhar/ts-jest/compare/v29.0.0-next.1...v29.0.0) (2022-09-08) diff --git a/package-lock.json b/package-lock.json index 242d71b7e7..8a03c5592a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ts-jest", - "version": "29.0.0", + "version": "29.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "ts-jest", - "version": "29.0.0", + "version": "29.0.1", "license": "MIT", "dependencies": { "bs-logger": "0.x", diff --git a/package.json b/package.json index 94af4a97fe..bd9680ebf6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-jest", - "version": "29.0.0", + "version": "29.0.1", "main": "dist/index.js", "types": "dist/index.d.ts", "bin": { diff --git a/website/versioned_docs/version-29.0/debugging.md b/website/versioned_docs/version-29.0/debugging.md index 3d814c7e52..715d77c142 100644 --- a/website/versioned_docs/version-29.0/debugging.md +++ b/website/versioned_docs/version-29.0/debugging.md @@ -17,14 +17,10 @@ export TS_JEST_LOG=ts-jest.log **Windows** -Command Prompt (cmd) - -``` +```Command Prompt tab set TS_JEST_LOG=ts-jest.log ``` -PowerShell - -``` +```PowerShell tab $env:TS_JEST_LOG = 'ts-jest.log' ``` diff --git a/website/versioned_docs/version-29.0/getting-started/installation.md b/website/versioned_docs/version-29.0/getting-started/installation.md index 62ea81eec4..ce10bbc49f 100644 --- a/website/versioned_docs/version-29.0/getting-started/installation.md +++ b/website/versioned_docs/version-29.0/getting-started/installation.md @@ -7,18 +7,10 @@ title: Installation You can install `ts-jest` and dependencies all at once with one of the following commands. -#### NPM - -```sh +```bash npm2yarn npm install --save-dev jest typescript ts-jest @types/jest ``` -#### Yarn - -```sh -yarn add --dev jest typescript ts-jest @types/jest -``` - :::tip Tip: If you get an error with the following `npm` commands such as `npx: command not found`, you can replace `npx XXX` with `node node_modules/.bin/XXX` from the root of your project. @@ -34,15 +26,11 @@ To make it transpile TypeScript with `ts-jest`, we will need to create a configu `ts-jest` can create the configuration file for you automatically: -#### NPM - -```sh +```npm tab npx ts-jest config:init ``` -#### Yarn - -```sh +```Yarn tab yarn ts-jest config:init ``` diff --git a/website/versioned_docs/version-29.0/getting-started/options.md b/website/versioned_docs/version-29.0/getting-started/options.md index 4a3b6ce6e3..7c32104afd 100644 --- a/website/versioned_docs/version-29.0/getting-started/options.md +++ b/website/versioned_docs/version-29.0/getting-started/options.md @@ -8,27 +8,8 @@ title: Options All `ts-jest` specific options can be defined in Jest `transform` config object in the `package.json` file of your project, or through a `jest.config.js`, or `jest.config.ts` file. -```json -// package.json -{ - // [...] - "jest": { - "transform": { - "": [ - "ts-jest", - { - // ts-jest configuration goes here - } - ] - } - } -} -``` - -Or through JavaScript: - -```js -// jest.config.js +```js tab +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { // [...] transform: { @@ -42,44 +23,36 @@ module.exports = { } ``` -:::tip +```ts tab +import type { JestConfigWithTsJest } from './types' -To utilize IDE suggestions, you can use `JSDoc` comments to provide suggested `ts-jest` configs for your Jest config: - -```js -/** @type {import('ts-jest').InitialOptionsTsJest} */ -module.exports = config = { +const jestConfig: JestConfigWithTsJest = { // [...] transform: { '': [ 'ts-jest', { - // ts-jest configuration goes here and your IDE will suggest which configs when typing + // ts-jest configuration goes here }, ], }, } ``` -::: - -Or through TypeScript (if `ts-node` is installed): - -```ts -// jest.config.ts -import type { InitialOptionsTsJest } from 'ts-jest' - -const config: InitialOptionsTsJest = { - transform: { - '': [ - 'ts-jest', - { - // ts-jest configuration goes here - }, - ], - }, +```JSON tab +{ + // [...] + "jest": { + "transform": { + "": [ + "ts-jest", + { + // ts-jest configuration goes here + } + ] + } + } } -export default config ``` :::important diff --git a/website/versioned_docs/version-29.0/getting-started/options/astTransformers.md b/website/versioned_docs/version-29.0/getting-started/options/astTransformers.md index 1998f7746d..d42801457e 100644 --- a/website/versioned_docs/version-29.0/getting-started/options/astTransformers.md +++ b/website/versioned_docs/version-29.0/getting-started/options/astTransformers.md @@ -16,8 +16,8 @@ The option is `astTransformers` and it allows ones to specify which 3 types of T #### Basic Transformers -```js -// jest.config.js +```js tab +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { // [...] transform: { @@ -33,8 +33,27 @@ module.exports = { } ``` -```json -// OR package.json +```ts tab +import type { JestConfigWithTsJest } from './types' + +const jestConfig: JestConfigWithTsJest = { + // [...] + transform: { + '': [ + 'ts-jest', + { + astTransformers: { + before: ['my-custom-transformer'], + }, + }, + ], + }, +} + +export default jestConfig +``` + +```JSON tab { // [...] "jest": { @@ -54,8 +73,8 @@ module.exports = { #### Configuring transformers with options -```js -// jest.config.js +```js tab +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { // [...] transform: { @@ -76,8 +95,32 @@ module.exports = { } ``` -```json -// OR package.json +```ts tab +import type { JestConfigWithTsJest } from './types' + +const jestConfig: JestConfigWithTsJest = { + // [...] + transform: { + '': [ + 'ts-jest', + { + astTransformers: { + before: [ + { + path: 'my-custom-transformer-that-needs-extra-opts', + options: {}, // extra options to pass to transformers here + }, + ], + }, + }, + ], + }, +} + +export default jestConfig +``` + +```JSON tab { // [...] "jest": { diff --git a/website/versioned_docs/version-29.0/getting-started/options/babelConfig.md b/website/versioned_docs/version-29.0/getting-started/options/babelConfig.md index 73a6ee27c4..4cd781ecab 100644 --- a/website/versioned_docs/version-29.0/getting-started/options/babelConfig.md +++ b/website/versioned_docs/version-29.0/getting-started/options/babelConfig.md @@ -14,8 +14,8 @@ The option is `babelConfig` and it works pretty much as the `tsconfig` option, e #### Use default `babelrc` file -```js -// jest.config.js +```js tab +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { // [...] transform: { @@ -29,7 +29,25 @@ module.exports = { } ``` -```json +```ts tab +import type { JestConfigWithTsJest } from './types' + +const jestConfig: JestConfigWithTsJest = { + // [...] + transform: { + '': [ + 'ts-jest', + { + babelConfig: true, + }, + ], + }, +} + +export default jestConfig +``` + +```JSON tab // OR package.json { // [...] @@ -50,8 +68,8 @@ module.exports = { The path should be relative to the current working directory where you start Jest from. You can also use `\` in the path, or use an absolute path (this last one is strongly not recommended). -```js -// jest.config.js +```js tab +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { // [...] transform: { @@ -65,23 +83,25 @@ module.exports = { } ``` -```js -// OR jest.config.js with require for babelrc -module.exports = { +```ts tab +import type { JestConfigWithTsJest } from './types' + +const jestConfig: JestConfigWithTsJest = { // [...] transform: { '': [ 'ts-jest', { - babelConfig: require('./babelrc.test.js'), + babelConfig: 'babelrc.test.js', }, ], }, } + +export default jestConfig ``` -```json -// OR package.json +```JSON tab { // [...] "jest": { @@ -97,12 +117,48 @@ module.exports = { } ``` +or importing directly the config file: + +```js tab +/** @type {import('ts-jest').JestConfigWithTsJest} */ +module.exports = { + // [...] + transform: { + '': [ + 'ts-jest', + { + babelConfig: require('./babelrc.test.js'), + }, + ], + }, +} +``` + +```ts tab +import type { JestConfigWithTsJest } from './types' +import babelConfig from './babelrc.test.js' + +const jestConfig: JestConfigWithTsJest = { + // [...] + transform: { + '': [ + 'ts-jest', + { + babelConfig, + }, + ], + }, +} + +export default jestConfig +``` + #### Inline compiler options Refer to the [Babel options](https://babeljs.io/docs/en/next/options) to know what can be used there. -```js -// jest.config.js +```js tab +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { // [...] transform: { @@ -119,8 +175,28 @@ module.exports = { } ``` -```json -// OR package.json +```ts tab +import type { JestConfigWithTsJest } from './types' + +const jestConfig: JestConfigWithTsJest = { + // [...] + transform: { + '': [ + 'ts-jest', + { + babelConfig: { + comments: false, + plugins: ['@babel/plugin-transform-for-of'], + }, + }, + ], + }, +} + +export default jestConfig +``` + +```JSON tab { // [...] "jest": { diff --git a/website/versioned_docs/version-29.0/getting-started/options/compiler.md b/website/versioned_docs/version-29.0/getting-started/options/compiler.md index 0d21700a85..85752446cc 100644 --- a/website/versioned_docs/version-29.0/getting-started/options/compiler.md +++ b/website/versioned_docs/version-29.0/getting-started/options/compiler.md @@ -11,8 +11,8 @@ If you use a custom compiler, such as `ttypescript`, make sure its API is the sa ### Example -```js -// jest.config.js +```js tab +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { // [...] transform: { @@ -26,8 +26,25 @@ module.exports = { } ``` -```json -// OR package.json +```ts tab +import type { JestConfigWithTsJest } from './types' + +const jestConfig: JestConfigWithTsJest = { + // [...] + transform: { + '` in the path to start from the project root dir. -```js -// jest.config.js +```js tab +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { // [...] transform: { @@ -29,8 +29,25 @@ module.exports = { } ``` -```json -// OR package.json +```ts tab +import type { JestConfigWithTsJest } from './types' + +const jestConfig: JestConfigWithTsJest = { + // [...] + transform: { + '/src/$1', + '^lib/(.*)$': '/common/$1', + }, +} + +export default jestConfig +``` + +```JSON tab { // [...] "jest": { @@ -54,15 +67,30 @@ module.exports = { #### Jest config (with helper) -```js -// jest.config.js +```js tab const { pathsToModuleNameMapper } = require('ts-jest') // In the following statement, replace `./tsconfig` with the path to your `tsconfig` file // which contains the path mapping (ie the `compilerOptions.paths` option): const { compilerOptions } = require('./tsconfig') +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { // [...] moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths /*, { prefix: '/' } */), } ``` + +```ts tab +import { pathsToModuleNameMapper } from 'ts-jest' +// In the following statement, replace `./tsconfig` with the path to your `tsconfig` file +// which contains the path mapping (ie the `compilerOptions.paths` option): +import { compilerOptions } from './tsconfig' +import type { JestConfigWithTsJest } from './types' + +const jestConfig: JestConfigWithTsJest = { + // [...] + moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths /*, { prefix: '/' } */), +} + +export default jestConfig +``` diff --git a/website/versioned_docs/version-29.0/getting-started/presets.md b/website/versioned_docs/version-29.0/getting-started/presets.md index 15643e11aa..53d347cc39 100644 --- a/website/versioned_docs/version-29.0/getting-started/presets.md +++ b/website/versioned_docs/version-29.0/getting-started/presets.md @@ -33,8 +33,8 @@ Starting from **v28.0.0**, `ts-jest` will gradually opt in adoption of `esbuild` In most cases, simply setting the `preset` key to the desired preset name in your Jest config should be enough to start using TypeScript with Jest (assuming you added `ts-jest` to your `devDependencies` of course): -```js -// jest.config.js +```js tab +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { // [...] // Replace `ts-jest` with the preset you want to use @@ -43,8 +43,20 @@ module.exports = { } ``` -```json -// OR package.json +```ts tab +import type { JestConfigWithTsJest } from './types' + +const jestConfig: JestConfigWithTsJest = { + // [...] + // Replace `ts-jest` with the preset you want to use + // from the above list + preset: 'ts-jest', +} + +export default jestConfig +``` + +```JSON tab { // [...] "jest": { @@ -63,8 +75,7 @@ Any preset can also be used with other options. If you're already using another preset, you might want only some specific settings from the chosen `ts-jest` preset. In this case you'll need to use the JavaScript version of Jest config (comment/uncomment according to your use case): -```js -// jest.config.js +```js tab const { defaults: tsjPreset } = require('ts-jest/presets') // const { defaultsESM: tsjPreset } = require('ts-jest/presets') // const { jsWithTs: tsjPreset } = require('ts-jest/presets') @@ -72,6 +83,7 @@ const { defaults: tsjPreset } = require('ts-jest/presets') // const { jsWithBabel: tsjPreset } = require('ts-jest/presets') // const { jsWithBabelESM: tsjPreset } = require('ts-jest/presets') +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { // [...] transform: { @@ -81,19 +93,17 @@ module.exports = { } ``` -Or through TypeScript (if `ts-node` is installed): +```ts tab +import type { JestConfigWithTsJest } from './types' -```ts -// jest.config.ts -import type { InitialOptionsTsJest } from 'ts-jest' import { defaults as tsjPreset } from 'ts-jest/presets' -// import { defaultsESM as tsjPreset } from 'ts-jest/presets' -// import { jsWithTs as tsjPreset } from 'ts-jest/presets' -// import { jsWithTsESM as tsjPreset } from 'ts-jest/presets' -// import { jsWithBabel as tsjPreset } from 'ts-jest/presets' -// import { jsWithBabelESM as tsjPreset } from 'ts-jest/presets' +// import { defaultsESM as tsjPreset } from 'ts-jest/presets'; +// import { jsWithTs as tsjPreset } from 'ts-jest/presets'; +// import { jsWithTsESM as tsjPreset } from 'ts-jest/presets'; +// import { jsWithBabel as tsjPreset } from 'ts-jest/presets'; +// import { jsWithBabelESM as tsjPreset } from 'ts-jest/presets'; -const config: InitialOptionsTsJest = { +const jestConfig: JestConfigWithTsJest = { // [...] transform: { ...tsjPreset.transform, @@ -101,5 +111,5 @@ const config: InitialOptionsTsJest = { }, } -export default config +export default jestConfig ``` diff --git a/website/versioned_docs/version-29.0/guides/esm-support.md b/website/versioned_docs/version-29.0/guides/esm-support.md index a36682263d..5e78bfa750 100644 --- a/website/versioned_docs/version-29.0/guides/esm-support.md +++ b/website/versioned_docs/version-29.0/guides/esm-support.md @@ -18,8 +18,8 @@ There are also [3 presets](../getting-started/presets.md) to work with ESM. #### Manual configuration -```js -// jest.config.js +```js tab +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { // [...] extensionsToTreatAsEsm: ['.ts'], @@ -37,8 +37,29 @@ module.exports = { } ``` -```json -// OR package.json +```ts tab +import type { JestConfigWithTsJest } from './types' + +const jestConfig: JestConfigWithTsJest = { + // [...] + extensionsToTreatAsEsm: ['.ts'], + moduleNameMapper: { + '^(\\.{1,2}/.*)\\.js$': '$1', + }, + transform: { + ' { + return jest.fn().mockImplementation(() => { + return { playSoundFile: mockPlaySoundFile } + }) +}) +``` + +```ts tab jest.mock('./sound-player', () => { return jest.fn().mockImplementation(() => { return { playSoundFile: mockPlaySoundFile } @@ -24,7 +32,19 @@ TypeError: sound_player_1.default is not a constructor because `soundPlayer.default` does not point to a function. Your mock has to return an object which has a property default that points to a function. -```js +```js tab +jest.mock('./sound-player', () => { + return { + default: jest.fn().mockImplementation(() => { + return { + playSoundFile: mockPlaySoundFile, + } + }), + } +}) +``` + +```ts tab jest.mock('./sound-player', () => { return { default: jest.fn().mockImplementation(() => { @@ -38,7 +58,15 @@ jest.mock('./sound-player', () => { For named imports, like `import { OAuth2 } from './oauth'`, replace `default` with imported module name, `OAuth2` in this example: -```js +```js tab +jest.mock('./oauth', () => { + return { + OAuth2: ... // mock here + } +}) +``` + +```ts tab jest.mock('./oauth', () => { return { OAuth2: ... // mock here diff --git a/website/versioned_docs/version-29.0/guides/react-native.md b/website/versioned_docs/version-29.0/guides/react-native.md index bbcf14776e..3e238797ea 100644 --- a/website/versioned_docs/version-29.0/guides/react-native.md +++ b/website/versioned_docs/version-29.0/guides/react-native.md @@ -11,13 +11,23 @@ After that, some little modifications will be required as follows: If you didn't yet, move any Babel config from `.babelrc` to `babel.config.js`. It should at least contain: -```js -// babel.config.js +```js tab +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { presets: ['module:metro-react-native-babel-preset'], } ``` +```ts tab +import type { JestConfigWithTsJest } from './types' + +const jestConfig: JestConfigWithTsJest = { + presets: ['module:metro-react-native-babel-preset'], +} + +export default jestConfig +``` + ### TypeScript Configuration Create a new `tsconfig.spec.json` at the root of your project with the following content @@ -36,10 +46,10 @@ Create a new `tsconfig.spec.json` at the root of your project with the following In the same way that you moved Babel config, move Jest config from `jest` key of `package.json` to `jest.config.js`. It should look like this: -```js -// jest.config.js +```js tab const { defaults: tsjPreset } = require('ts-jest/presets') +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { preset: 'react-native', transform: { @@ -54,3 +64,24 @@ module.exports = { moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], } ``` + +```ts tab +import { defaults as tsjPreset } from 'ts-jest/presets' +import type { JestConfigWithTsJest } from './types' + +const jestConfig: JestConfigWithTsJest = { + preset: 'react-native', + transform: { + '^.+\\.jsx$': 'babel-jest', + '^.+\\.tsx?$': [ + 'ts-jest', + { + tsconfig: 'tsconfig.spec.json', + }, + ], + }, + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], +} + +export default jestConfig +``` diff --git a/website/versioned_docs/version-29.0/migration.md b/website/versioned_docs/version-29.0/migration.md index c55c9ee739..5a788a6b0a 100644 --- a/website/versioned_docs/version-29.0/migration.md +++ b/website/versioned_docs/version-29.0/migration.md @@ -7,28 +7,20 @@ You can use the `config:migrate` tool of `ts-jest` CLI if you're coming from an _If you're using `jest.config.js`:_ -### NPM - -```sh +```npm tab npx ts-jest config:migrate jest.config.js ``` -### Yarn - -```sh +```Yarn tab yarn ts-jest config:migrate jest.config.js ``` _If you're using `jest` config property of `package.json`:_ -### NPM - -```sh +```npm tab npx ts-jest config:migrate package.json ``` -### Yarn - -```sh +```Yarn tab yarn ts-jest config:migrate package.json ```