From dcc71bb06a1018829a3512cefd101eb77bcd3f68 Mon Sep 17 00:00:00 2001 From: Gamote Date: Tue, 29 Sep 2020 20:29:20 +0200 Subject: [PATCH 01/38] feat: Add support for the `jest.config.ts` configuration file --- docs/Configuration.md | 19 ++++- .../__snapshots__/jest.config.ts.test.ts.snap | 36 ++++++++++ e2e/__tests__/jest.config.ts.test.ts | 71 +++++++++++++++++++ .../jest-cli/src/__tests__/cli/args.test.ts | 4 +- packages/jest-config/package.json | 3 +- .../src/__tests__/resolveConfigPath.test.ts | 2 +- packages/jest-config/src/constants.ts | 2 + packages/jest-config/src/index.ts | 2 +- .../src/readConfigFileAndSetRootDir.ts | 29 ++++++-- .../version-26.4/Configuration.md | 19 ++++- 10 files changed, 176 insertions(+), 11 deletions(-) create mode 100644 e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap create mode 100644 e2e/__tests__/jest.config.ts.test.ts diff --git a/docs/Configuration.md b/docs/Configuration.md index c1fbe728e023..828aac9f39fd 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -3,7 +3,7 @@ id: configuration title: Configuring Jest --- -Jest's configuration can be defined in the `package.json` file of your project, or through a `jest.config.js` file or through the `--config ` option. If you'd like to use your `package.json` to store Jest's config, the `"jest"` key should be used on the top level so Jest will know how to find your settings: +Jest's configuration can be defined in the `package.json` file of your project, or through a `jest.config.js` or `jest.config.ts` file or through the `--config ` option. If you'd like to use your `package.json` to store Jest's config, the `"jest"` key should be used on the top level so Jest will know how to find your settings: ```json { @@ -31,6 +31,23 @@ module.exports = async () => { }; ``` +Or through TypeScript: + +```ts +// jest.config.ts +//Sync object +export = { + verbose: true, +}; + +//Or async function +export default async (): Promise => { + return { + verbose: true, + }; +}; +``` + Please keep in mind that the resulting configuration must be JSON-serializable. When using the `--config` option, the JSON file must not contain a "jest" key: diff --git a/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap b/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap new file mode 100644 index 000000000000..231d926a36de --- /dev/null +++ b/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap @@ -0,0 +1,36 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`traverses directory tree up until it finds jest.config 1`] = ` + console.log +<>/jest.config.ts/some/nested/directory + + at Object.log (__tests__/a-giraffe.js:3:27) + +`; + +exports[`traverses directory tree up until it finds jest.config 2`] = ` +PASS ../../../__tests__/a-giraffe.js + ✓ giraffe + ✓ abc +`; + +exports[`traverses directory tree up until it finds jest.config 3`] = ` +Test Suites: 1 passed, 1 total +Tests: 2 passed, 2 total +Snapshots: 0 total +Time: <> +Ran all test suites. +`; + +exports[`works with jest.config.ts 1`] = ` +PASS __tests__/a-giraffe.js + ✓ giraffe +`; + +exports[`works with jest.config.ts 2`] = ` +Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites. +`; diff --git a/e2e/__tests__/jest.config.ts.test.ts b/e2e/__tests__/jest.config.ts.test.ts new file mode 100644 index 000000000000..ff71917137a5 --- /dev/null +++ b/e2e/__tests__/jest.config.ts.test.ts @@ -0,0 +1,71 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import * as path from 'path'; +import {wrap} from 'jest-snapshot-serializer-raw'; +import runJest from '../runJest'; +import {cleanup, extractSummary, writeFiles} from '../Utils'; + +const DIR = path.resolve(__dirname, '../jest.config.ts'); + +beforeEach(() => cleanup(DIR)); +afterAll(() => cleanup(DIR)); + +test('works with jest.config.ts', () => { + writeFiles(DIR, { + '__tests__/a-giraffe.js': `test('giraffe', () => expect(1).toBe(1));`, + 'jest.config.ts': `module.exports = {testRegex: '.*-giraffe.js'};`, + 'package.json': '{}', + }); + + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']); + const {rest, summary} = extractSummary(stderr); + expect(exitCode).toBe(0); + expect(wrap(rest)).toMatchSnapshot(); + expect(wrap(summary)).toMatchSnapshot(); +}); + +test('traverses directory tree up until it finds jest.config', () => { + writeFiles(DIR, { + '__tests__/a-giraffe.js': ` + const slash = require('slash'); + test('giraffe', () => expect(1).toBe(1)); + test('abc', () => console.log(slash(process.cwd()))); + `, + 'jest.config.ts': `module.exports = {testRegex: '.*-giraffe.js'};`, + 'package.json': '{}', + 'some/nested/directory/file.js': '// nothing special', + }); + + const {stderr, exitCode, stdout} = runJest( + path.join(DIR, 'some', 'nested', 'directory'), + ['-w=1', '--ci=false'], + {skipPkgJsonCheck: true}, + ); + + // Snapshot the console.loged `process.cwd()` and make sure it stays the same + expect( + wrap(stdout.replace(/^\W+(.*)e2e/gm, '<>')), + ).toMatchSnapshot(); + + const {rest, summary} = extractSummary(stderr); + expect(exitCode).toBe(0); + expect(wrap(rest)).toMatchSnapshot(); + expect(wrap(summary)).toMatchSnapshot(); +}); + +test('invalid JS in jest.config.ts', () => { + writeFiles(DIR, { + '__tests__/a-giraffe.js': `test('giraffe', () => expect(1).toBe(1));`, + 'jest.config.ts': `module.exports = i'll break this file yo`, + 'package.json': '{}', + }); + + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']); + expect(stderr).toMatch('Error: Jest: Failed to parse config file '); + expect(exitCode).toBe(1); +}); diff --git a/packages/jest-cli/src/__tests__/cli/args.test.ts b/packages/jest-cli/src/__tests__/cli/args.test.ts index 55fe1255671d..333b395f7880 100644 --- a/packages/jest-cli/src/__tests__/cli/args.test.ts +++ b/packages/jest-cli/src/__tests__/cli/args.test.ts @@ -82,13 +82,13 @@ describe('check', () => { it('raises an exception if config is not a valid JSON string', () => { const argv = {config: 'x:1'} as Config.Argv; expect(() => check(argv)).toThrow( - 'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .mjs, .cjs, .json', + 'The --config option requires a JSON string literal, or a file path with one of these extensions: .ts, .js, .mjs, .cjs, .json', ); }); it('raises an exception if config is not a supported file type', () => { const message = - 'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .mjs, .cjs, .json'; + 'The --config option requires a JSON string literal, or a file path with one of these extensions: .ts, .js, .mjs, .cjs, .json'; expect(() => check({config: 'jest.configjs'} as Config.Argv)).toThrow( message, diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index 0428d3611073..ed3aa8a6a4af 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -27,7 +27,8 @@ "jest-util": "^26.3.0", "jest-validate": "^26.4.2", "micromatch": "^4.0.2", - "pretty-format": "^26.4.2" + "pretty-format": "^26.4.2", + "typescript": "^4.0.3" }, "devDependencies": { "@types/babel__core": "^7.0.4", diff --git a/packages/jest-config/src/__tests__/resolveConfigPath.test.ts b/packages/jest-config/src/__tests__/resolveConfigPath.test.ts index 0fba06b58295..b90166c73877 100644 --- a/packages/jest-config/src/__tests__/resolveConfigPath.test.ts +++ b/packages/jest-config/src/__tests__/resolveConfigPath.test.ts @@ -81,7 +81,7 @@ describe.each(JEST_CONFIG_EXT_ORDER.slice(0))( writeFiles(DIR, {[relativeJestConfigPath]: ''}); - // jest.config.js takes presedence + // jest.config.js takes precedence // absolute expect( diff --git a/packages/jest-config/src/constants.ts b/packages/jest-config/src/constants.ts index 3ae6c9701ece..9873b4b344c1 100644 --- a/packages/jest-config/src/constants.ts +++ b/packages/jest-config/src/constants.ts @@ -15,9 +15,11 @@ export const JEST_CONFIG_BASE_NAME = 'jest.config'; export const JEST_CONFIG_EXT_CJS = '.cjs'; export const JEST_CONFIG_EXT_MJS = '.mjs'; export const JEST_CONFIG_EXT_JS = '.js'; +export const JEST_CONFIG_EXT_TS = '.ts'; export const JEST_CONFIG_EXT_JSON = '.json'; export const JEST_CONFIG_EXT_ORDER = Object.freeze([ JEST_CONFIG_EXT_JS, + JEST_CONFIG_EXT_TS, JEST_CONFIG_EXT_MJS, JEST_CONFIG_EXT_CJS, JEST_CONFIG_EXT_JSON, diff --git a/packages/jest-config/src/index.ts b/packages/jest-config/src/index.ts index c0cc2326a7ec..17f930260df2 100644 --- a/packages/jest-config/src/index.ts +++ b/packages/jest-config/src/index.ts @@ -74,7 +74,7 @@ export async function readConfig( config.rootDir = config.rootDir || packageRootOrConfig; rawOptions = config; // A string passed to `--config`, which is either a direct path to the config - // or a path to directory containing `package.json` or `jest.config.js` + // or a path to directory containing `package.json`, `jest.config.js` or `jest.config.ts` } else if (!skipArgvConfigOption && typeof argv.config == 'string') { configPath = resolveConfigPath(argv.config, process.cwd()); rawOptions = await readConfigFileAndSetRootDir(configPath); diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 06cbaecc10d6..1354f6e9c6cf 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -9,22 +9,43 @@ import * as path from 'path'; import {pathToFileURL} from 'url'; import * as fs from 'graceful-fs'; import type {Config} from '@jest/types'; +import {transpileModule} from 'typescript'; // @ts-expect-error: vendored import jsonlint from './vendor/jsonlint'; -import {JEST_CONFIG_EXT_JSON, PACKAGE_JSON} from './constants'; +import { + JEST_CONFIG_EXT_JSON, + JEST_CONFIG_EXT_TS, + PACKAGE_JSON, +} from './constants'; // Read the configuration and set its `rootDir` // 1. If it's a `package.json` file, we look into its "jest" property -// 2. For any other file, we just require it. If we receive an 'ERR_REQUIRE_ESM' +// 2. If it's a `jest.config.ts` file, we convert it in JS and we parse it +// 3. For any other file, we just require it. If we receive an 'ERR_REQUIRE_ESM' // from node, perform a dynamic import instead. export default async function readConfigFileAndSetRootDir( configPath: Config.Path, ): Promise { + const isTS = configPath.endsWith(JEST_CONFIG_EXT_TS); const isJSON = configPath.endsWith(JEST_CONFIG_EXT_JSON); let configObject; try { - configObject = require(configPath); + if (isTS) { + // Get the content of the TS config file + const tsConfigFileContent = fs.readFileSync(configPath, { + encoding: 'utf8', + }); + + // Convert the TS content to JS + const jsConfigFileContent = transpileModule(tsConfigFileContent, {}).outputText; + + // Execute the JS code to obtain the config + // eslint-disable-next-line no-eval + configObject = eval(jsConfigFileContent); + } else { + configObject = require(configPath); + } } catch (error) { if (error.code === 'ERR_REQUIRE_ESM') { try { @@ -49,7 +70,7 @@ export default async function readConfigFileAndSetRootDir( throw innerError; } - } else if (isJSON) { + } else if (isTS || isJSON) { throw new Error( `Jest: Failed to parse config file ${configPath}\n` + ` ${jsonlint.errors(fs.readFileSync(configPath, 'utf8'))}`, diff --git a/website/versioned_docs/version-26.4/Configuration.md b/website/versioned_docs/version-26.4/Configuration.md index 0a516470117a..bd63427e0433 100644 --- a/website/versioned_docs/version-26.4/Configuration.md +++ b/website/versioned_docs/version-26.4/Configuration.md @@ -4,7 +4,7 @@ title: Configuring Jest original_id: configuration --- -Jest's configuration can be defined in the `package.json` file of your project, or through a `jest.config.js` file or through the `--config ` option. If you'd like to use your `package.json` to store Jest's config, the `"jest"` key should be used on the top level so Jest will know how to find your settings: +Jest's configuration can be defined in the `package.json` file of your project, or through a `jest.config.js` or `jest.config.ts` file or through the `--config ` option. If you'd like to use your `package.json` to store Jest's config, the `"jest"` key should be used on the top level so Jest will know how to find your settings: ```json { @@ -32,6 +32,23 @@ module.exports = async () => { }; ``` +Or through TypeScript: + +```ts +// jest.config.ts +//Sync object +export = { + verbose: true, +}; + +//Or async function +export default async (): Promise => { + return { + verbose: true, + }; +}; +``` + Please keep in mind that the resulting configuration must be JSON-serializable. When using the `--config` option, the JSON file must not contain a "jest" key: From e7b057375a607a2865e1109426fdf680838e00f1 Mon Sep 17 00:00:00 2001 From: Gamote Date: Tue, 29 Sep 2020 20:46:46 +0200 Subject: [PATCH 02/38] chore: The `jest.config.ts` documentation was updated --- docs/Configuration.md | 4 +++- website/versioned_docs/version-26.4/Configuration.md | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 828aac9f39fd..cbf8f81d70af 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -35,10 +35,12 @@ Or through TypeScript: ```ts // jest.config.ts +import type { Config } from '@jest/types'; + //Sync object export = { verbose: true, -}; +} as Config.InitialOptions; //Or async function export default async (): Promise => { diff --git a/website/versioned_docs/version-26.4/Configuration.md b/website/versioned_docs/version-26.4/Configuration.md index bd63427e0433..72e37d61eefa 100644 --- a/website/versioned_docs/version-26.4/Configuration.md +++ b/website/versioned_docs/version-26.4/Configuration.md @@ -36,10 +36,12 @@ Or through TypeScript: ```ts // jest.config.ts +import type { Config } from '@jest/types'; + //Sync object export = { verbose: true, -}; +} as Config.InitialOptions; //Or async function export default async (): Promise => { From a2c9fdc003d485898fdd126b6161adc144e75223 Mon Sep 17 00:00:00 2001 From: Gamote Date: Tue, 29 Sep 2020 20:49:50 +0200 Subject: [PATCH 03/38] chore: The `jest.config.ts` support was specified in the CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99689b1d4169..80b45f5a618d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +- `[jest-cli, jest-config]` Add support for the `jest.config.ts` configuration file ([#10564](https://github.com/facebook/jest/pull/10564)) - `[jest-circus, jest-config, jest-runtime]` Add new `injectGlobals` config and CLI option to disable injecting global variables into the runtime ([#10484](https://github.com/facebook/jest/pull/10484)) - `[jest-each]` Fixes `.each` type to always be callable ([#10447](https://github.com/facebook/jest/pull/10447)) - `[jest-runner]` Add support for `moduleLoader`s with `default` exports ([#10541](https://github.com/facebook/jest/pull/10541)) From 2b5d97a1ca62fcf743e4ee933ab9ed6edc1299fb Mon Sep 17 00:00:00 2001 From: Gamote Date: Tue, 29 Sep 2020 21:30:58 +0200 Subject: [PATCH 04/38] chore: Documentation updated to encourage the usage of TS annotations --- docs/Configuration.md | 7 ++++--- website/versioned_docs/version-26.4/Configuration.md | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index cbf8f81d70af..cfdee2d3f768 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -38,9 +38,10 @@ Or through TypeScript: import type { Config } from '@jest/types'; //Sync object -export = { - verbose: true, -} as Config.InitialOptions; +const config: Config.InitialOptions = { + verbose: true +}; +export = config; //Or async function export default async (): Promise => { diff --git a/website/versioned_docs/version-26.4/Configuration.md b/website/versioned_docs/version-26.4/Configuration.md index 72e37d61eefa..901a5ac8fd67 100644 --- a/website/versioned_docs/version-26.4/Configuration.md +++ b/website/versioned_docs/version-26.4/Configuration.md @@ -39,9 +39,10 @@ Or through TypeScript: import type { Config } from '@jest/types'; //Sync object -export = { - verbose: true, -} as Config.InitialOptions; +const config: Config.InitialOptions = { + verbose: true +}; +export = config; //Or async function export default async (): Promise => { From 7f5a39d4425c4e3d2fb55f75f4c5b4e3448d1b52 Mon Sep 17 00:00:00 2001 From: Gamote Date: Tue, 29 Sep 2020 21:37:35 +0200 Subject: [PATCH 05/38] Errors fixed for tests and prettier --- packages/jest-cli/src/__tests__/cli/args.test.ts | 4 ++-- .../src/init/__tests__/__snapshots__/init.test.js.snap | 9 +++++++++ .../fixtures/has_jest_config_file_ts/jest.config.ts | 8 ++++++++ .../fixtures/has_jest_config_file_ts/package.json | 1 + packages/jest-config/src/readConfigFileAndSetRootDir.ts | 3 ++- 5 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file_ts/jest.config.ts create mode 100644 packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file_ts/package.json diff --git a/packages/jest-cli/src/__tests__/cli/args.test.ts b/packages/jest-cli/src/__tests__/cli/args.test.ts index 333b395f7880..5d2e5e5570e4 100644 --- a/packages/jest-cli/src/__tests__/cli/args.test.ts +++ b/packages/jest-cli/src/__tests__/cli/args.test.ts @@ -82,13 +82,13 @@ describe('check', () => { it('raises an exception if config is not a valid JSON string', () => { const argv = {config: 'x:1'} as Config.Argv; expect(() => check(argv)).toThrow( - 'The --config option requires a JSON string literal, or a file path with one of these extensions: .ts, .js, .mjs, .cjs, .json', + 'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .ts, .mjs, .cjs, .json', ); }); it('raises an exception if config is not a supported file type', () => { const message = - 'The --config option requires a JSON string literal, or a file path with one of these extensions: .ts, .js, .mjs, .cjs, .json'; + 'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .ts, .mjs, .cjs, .json'; expect(() => check({config: 'jest.configjs'} as Config.Argv)).toThrow( message, diff --git a/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap b/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap index 70e36b545cad..0e8040cfade6 100644 --- a/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap +++ b/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap @@ -45,6 +45,15 @@ Object { } `; +exports[`init has-jest-config-file-ts ask the user whether to override config or not user answered with "Yes" 1`] = ` +Object { + "initial": true, + "message": "It seems that you already have a jest configuration, do you want to override it?", + "name": "continue", + "type": "confirm", +} +`; + exports[`init project with package.json and no jest config all questions answered with answer: "No" should return the default configuration (an empty config) 1`] = ` "// For a detailed explanation regarding each configuration property, visit: // https://jestjs.io/docs/en/configuration.html diff --git a/packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file_ts/jest.config.ts b/packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file_ts/jest.config.ts new file mode 100644 index 000000000000..2da8322a7f9b --- /dev/null +++ b/packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file_ts/jest.config.ts @@ -0,0 +1,8 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export = {}; diff --git a/packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file_ts/package.json b/packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file_ts/package.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file_ts/package.json @@ -0,0 +1 @@ +{} diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 1354f6e9c6cf..f00bd2b40495 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -38,7 +38,8 @@ export default async function readConfigFileAndSetRootDir( }); // Convert the TS content to JS - const jsConfigFileContent = transpileModule(tsConfigFileContent, {}).outputText; + const jsConfigFileContent = transpileModule(tsConfigFileContent, {}) + .outputText; // Execute the JS code to obtain the config // eslint-disable-next-line no-eval From 0b680526e964fdc0877296a299ad79ac75698828 Mon Sep 17 00:00:00 2001 From: Gamote Date: Wed, 30 Sep 2020 00:59:00 +0200 Subject: [PATCH 06/38] chore: Use 'ts-node' as peer dependency instead of 'typescript' --- .../__snapshots__/jest.config.ts.test.ts.snap | 2 + e2e/__tests__/jest.config.ts.test.ts | 10 ++-- package.json | 1 + packages/jest-config/package.json | 6 ++- .../src/readConfigFileAndSetRootDir.ts | 52 ++++++++++++++----- packages/jest-config/src/utils.ts | 4 ++ yarn.lock | 46 +++++++++++++++- 7 files changed, 100 insertions(+), 21 deletions(-) diff --git a/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap b/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap index 231d926a36de..759244e10052 100644 --- a/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap +++ b/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap @@ -1,6 +1,8 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`traverses directory tree up until it finds jest.config 1`] = ` +Jest: Loading the TS config file: /Users/david/Projects/Gamote/Libraries/jest/e2e/jest.config.ts/jest.config.ts +Jest: Loading the TS config file: /Users/david/Projects/Gamote/Libraries/jest/e2e/jest.config.ts/jest.config.ts console.log <>/jest.config.ts/some/nested/directory diff --git a/e2e/__tests__/jest.config.ts.test.ts b/e2e/__tests__/jest.config.ts.test.ts index ff71917137a5..fef7250ac3b3 100644 --- a/e2e/__tests__/jest.config.ts.test.ts +++ b/e2e/__tests__/jest.config.ts.test.ts @@ -18,7 +18,7 @@ afterAll(() => cleanup(DIR)); test('works with jest.config.ts', () => { writeFiles(DIR, { '__tests__/a-giraffe.js': `test('giraffe', () => expect(1).toBe(1));`, - 'jest.config.ts': `module.exports = {testRegex: '.*-giraffe.js'};`, + 'jest.config.ts': `export default {testRegex: '.*-giraffe.js'};`, 'package.json': '{}', }); @@ -36,7 +36,7 @@ test('traverses directory tree up until it finds jest.config', () => { test('giraffe', () => expect(1).toBe(1)); test('abc', () => console.log(slash(process.cwd()))); `, - 'jest.config.ts': `module.exports = {testRegex: '.*-giraffe.js'};`, + 'jest.config.ts': `export default {testRegex: '.*-giraffe.js'};`, 'package.json': '{}', 'some/nested/directory/file.js': '// nothing special', }); @@ -61,11 +61,13 @@ test('traverses directory tree up until it finds jest.config', () => { test('invalid JS in jest.config.ts', () => { writeFiles(DIR, { '__tests__/a-giraffe.js': `test('giraffe', () => expect(1).toBe(1));`, - 'jest.config.ts': `module.exports = i'll break this file yo`, + 'jest.config.ts': `export default i'll break this file yo`, 'package.json': '{}', }); const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']); - expect(stderr).toMatch('Error: Jest: Failed to parse config file '); + expect(stderr).toMatch( + 'Error: Jest: Failed to parse the TypeScript config file ', + ); expect(exitCode).toBe(1); }); diff --git a/package.json b/package.json index 3af4c8c57840..e28af6c83b9c 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "strip-ansi": "^6.0.0", "tempy": "^0.6.0", "throat": "^5.0.0", + "ts-node": "^9.0.0", "type-fest": "^0.16.0", "typescript": "^4.0.2", "which": "^2.0.1" diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index ed3aa8a6a4af..a6da1009aa21 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -9,6 +9,9 @@ "license": "MIT", "main": "build/index.js", "types": "build/index.d.ts", + "peerDependencies": { + "ts-node": "^9.0.0" + }, "dependencies": { "@babel/core": "^7.1.0", "@jest/test-sequencer": "^26.4.2", @@ -27,8 +30,7 @@ "jest-util": "^26.3.0", "jest-validate": "^26.4.2", "micromatch": "^4.0.2", - "pretty-format": "^26.4.2", - "typescript": "^4.0.3" + "pretty-format": "^26.4.2" }, "devDependencies": { "@types/babel__core": "^7.0.4", diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index f00bd2b40495..e9548efcfeeb 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -9,7 +9,7 @@ import * as path from 'path'; import {pathToFileURL} from 'url'; import * as fs from 'graceful-fs'; import type {Config} from '@jest/types'; -import {transpileModule} from 'typescript'; +import {interopRequireDefault} from './utils'; // @ts-expect-error: vendored import jsonlint from './vendor/jsonlint'; import { @@ -32,18 +32,7 @@ export default async function readConfigFileAndSetRootDir( try { if (isTS) { - // Get the content of the TS config file - const tsConfigFileContent = fs.readFileSync(configPath, { - encoding: 'utf8', - }); - - // Convert the TS content to JS - const jsConfigFileContent = transpileModule(tsConfigFileContent, {}) - .outputText; - - // Execute the JS code to obtain the config - // eslint-disable-next-line no-eval - configObject = eval(jsConfigFileContent); + configObject = loadTSConfigFile(configPath); } else { configObject = require(configPath); } @@ -71,11 +60,16 @@ export default async function readConfigFileAndSetRootDir( throw innerError; } - } else if (isTS || isJSON) { + } else if (isJSON) { throw new Error( `Jest: Failed to parse config file ${configPath}\n` + ` ${jsonlint.errors(fs.readFileSync(configPath, 'utf8'))}`, ); + } else if (isTS) { + throw new Error( + `Jest: Failed to parse the TypeScript config file ${configPath}\n` + + ` ${error}`, + ); } else { throw error; } @@ -103,3 +97,33 @@ export default async function readConfigFileAndSetRootDir( return configObject; } + +// Load the TypeScript configuration +const loadTSConfigFile = (configPath: Config.Path): Config.InitialOptions => { + console.info(`Jest: Loading the TS config file: ${configPath}`); + + let registerer; + + // Register TypeScript compiler instance + try { + registerer = require('ts-node').register(); + } catch (e) { + if (e.code === 'MODULE_NOT_FOUND') { + throw new Error( + `Jest: 'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${e.message}`, + ); + } + + throw new Error( + `Jest: Could not register 'ts-node' to read the TypeScript file: ${configPath}`, + ); + } + + registerer.enabled(true); + + const configObject = interopRequireDefault(require(configPath)).default; + + registerer.enabled(false); + + return configObject; +}; diff --git a/packages/jest-config/src/utils.ts b/packages/jest-config/src/utils.ts index baa834fffcf9..ad69663235e0 100644 --- a/packages/jest-config/src/utils.ts +++ b/packages/jest-config/src/utils.ts @@ -248,3 +248,7 @@ export const getSequencer = ( prefix: 'jest-sequencer-', rootDir, }); + +// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types +export const interopRequireDefault = (obj: any): {default: any} => + obj && obj.__esModule ? obj : {default: obj}; diff --git a/yarn.lock b/yarn.lock index d39891853c33..892fe2152b44 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4367,6 +4367,13 @@ __metadata: languageName: node linkType: hard +"arg@npm:^4.1.0": + version: 4.1.3 + resolution: "arg@npm:4.1.3" + checksum: 81b3b40b1529c4fbf75b12f7c3e6fb2dcce9e78072063babc169de9b4f40777788f3d2b04380f659ef676a756e03ccfbfe78adf4477353bda906295fa69dab89 + languageName: node + linkType: hard + "argparse@npm:^1.0.10, argparse@npm:^1.0.7": version: 1.0.10 resolution: "argparse@npm:1.0.10" @@ -11445,6 +11452,8 @@ fsevents@^1.2.7: jest-validate: ^26.4.2 micromatch: ^4.0.2 pretty-format: ^26.4.2 + peerDependencies: + ts-node: ^9.0.0 languageName: unknown linkType: soft @@ -13029,6 +13038,13 @@ fsevents@^1.2.7: languageName: node linkType: hard +"make-error@npm:^1.1.1": + version: 1.3.6 + resolution: "make-error@npm:1.3.6" + checksum: 2c780bab8409b865e8ee86697c599a2bf2765ec64d21eb67ccda27050e039f983feacad05a0d43aba3c966ea03d305d2612e94fec45474bcbc61181f57c5bb88 + languageName: node + linkType: hard + "make-fetch-happen@npm:^5.0.0": version: 5.0.2 resolution: "make-fetch-happen@npm:5.0.2" @@ -17097,6 +17113,7 @@ fsevents@^1.2.7: strip-ansi: ^6.0.0 tempy: ^0.6.0 throat: ^5.0.0 + ts-node: ^9.0.0 type-fest: ^0.16.0 typescript: ^4.0.2 which: ^2.0.1 @@ -17732,7 +17749,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"source-map-support@npm:^0.5.16": +"source-map-support@npm:^0.5.16, source-map-support@npm:^0.5.17": version: 0.5.19 resolution: "source-map-support@npm:0.5.19" dependencies: @@ -18908,6 +18925,26 @@ fsevents@^1.2.7: languageName: node linkType: hard +"ts-node@npm:^9.0.0": + version: 9.0.0 + resolution: "ts-node@npm:9.0.0" + dependencies: + arg: ^4.1.0 + diff: ^4.0.1 + make-error: ^1.1.1 + source-map-support: ^0.5.17 + yn: 3.1.1 + peerDependencies: + typescript: ">=2.7" + bin: + ts-node: dist/bin.js + ts-node-script: dist/bin-script.js + ts-node-transpile-only: dist/bin-transpile.js + ts-script: dist/bin-script-deprecated.js + checksum: 49d2ab087fc4b93f0a292e426ea6d242a80d1a4809ac008758bb0170aea1a85cd4182792c1d70889285fe5328cd8bd1ffb1e5108d1c4f31c39dbe9151ea4cabe + languageName: node + linkType: hard + "tsconfig-paths@npm:^3.9.0": version: 3.9.0 resolution: "tsconfig-paths@npm:3.9.0" @@ -20217,6 +20254,13 @@ fsevents@^1.2.7: languageName: node linkType: hard +"yn@npm:3.1.1": + version: 3.1.1 + resolution: "yn@npm:3.1.1" + checksum: bff63b80568d80c711670935427494dde47cdf97e8b04196b140ce0af519c81c5ee857eddad0caa8b422dd65aea0157bbfaacbb1546bebba623f0f383d5d9ae5 + languageName: node + linkType: hard + "zone.js@npm:~0.10.2": version: 0.10.3 resolution: "zone.js@npm:0.10.3" From 2ef46e2370d2699b61345eb8e4521caebdcb39e4 Mon Sep 17 00:00:00 2001 From: Gamote Date: Wed, 30 Sep 2020 01:00:12 +0200 Subject: [PATCH 07/38] chore: Documentation updated to specify the 'ts-node' peer dependency of the `jest.config.ts` functionality --- docs/Configuration.md | 6 ++++-- website/versioned_docs/version-26.4/Configuration.md | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index cfdee2d3f768..c23c73b612ce 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -3,7 +3,7 @@ id: configuration title: Configuring Jest --- -Jest's configuration can be defined in the `package.json` file of your project, or through a `jest.config.js` or `jest.config.ts` file or through the `--config ` option. If you'd like to use your `package.json` to store Jest's config, the `"jest"` key should be used on the top level so Jest will know how to find your settings: +Jest's configuration can be defined in the `package.json` file of your project, or through a `jest.config.js`, or `jest.config.ts` file or through the `--config ` option. If you'd like to use your `package.json` to store Jest's config, the `"jest"` key should be used on the top level so Jest will know how to find your settings: ```json { @@ -33,6 +33,8 @@ module.exports = async () => { Or through TypeScript: +You can enable support for the TypeScript configuration file by installing the `ts-node` plugin in your project. + ```ts // jest.config.ts import type { Config } from '@jest/types'; @@ -41,7 +43,7 @@ import type { Config } from '@jest/types'; const config: Config.InitialOptions = { verbose: true }; -export = config; +export default config; //Or async function export default async (): Promise => { diff --git a/website/versioned_docs/version-26.4/Configuration.md b/website/versioned_docs/version-26.4/Configuration.md index 901a5ac8fd67..96dd6d2eb75d 100644 --- a/website/versioned_docs/version-26.4/Configuration.md +++ b/website/versioned_docs/version-26.4/Configuration.md @@ -4,7 +4,7 @@ title: Configuring Jest original_id: configuration --- -Jest's configuration can be defined in the `package.json` file of your project, or through a `jest.config.js` or `jest.config.ts` file or through the `--config ` option. If you'd like to use your `package.json` to store Jest's config, the `"jest"` key should be used on the top level so Jest will know how to find your settings: +Jest's configuration can be defined in the `package.json` file of your project, or through a `jest.config.js`, or `jest.config.ts` file or through the `--config ` option. If you'd like to use your `package.json` to store Jest's config, the `"jest"` key should be used on the top level so Jest will know how to find your settings: ```json { @@ -34,6 +34,8 @@ module.exports = async () => { Or through TypeScript: +You can enable support for the TypeScript configuration file by installing the `ts-node` plugin in your project. + ```ts // jest.config.ts import type { Config } from '@jest/types'; @@ -42,7 +44,7 @@ import type { Config } from '@jest/types'; const config: Config.InitialOptions = { verbose: true }; -export = config; +export default config; //Or async function export default async (): Promise => { From 9bc45043d889e34577077c383fd9f6e7678306f9 Mon Sep 17 00:00:00 2001 From: Gamote Date: Wed, 30 Sep 2020 01:17:07 +0200 Subject: [PATCH 08/38] fix: Prettier errors --- docs/Configuration.md | 4 ++-- website/versioned_docs/version-26.4/Configuration.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index c23c73b612ce..ee5980732aee 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -37,11 +37,11 @@ You can enable support for the TypeScript configuration file by installing the ` ```ts // jest.config.ts -import type { Config } from '@jest/types'; +import type {Config} from '@jest/types'; //Sync object const config: Config.InitialOptions = { - verbose: true + verbose: true, }; export default config; diff --git a/website/versioned_docs/version-26.4/Configuration.md b/website/versioned_docs/version-26.4/Configuration.md index 96dd6d2eb75d..7b4597a0cc2f 100644 --- a/website/versioned_docs/version-26.4/Configuration.md +++ b/website/versioned_docs/version-26.4/Configuration.md @@ -38,11 +38,11 @@ You can enable support for the TypeScript configuration file by installing the ` ```ts // jest.config.ts -import type { Config } from '@jest/types'; +import type {Config} from '@jest/types'; //Sync object const config: Config.InitialOptions = { - verbose: true + verbose: true, }; export default config; From 5674532cdc0be5952ca8f0a01827c1ecd12ad863 Mon Sep 17 00:00:00 2001 From: Gamote Date: Wed, 30 Sep 2020 01:22:10 +0200 Subject: [PATCH 09/38] fix: Remove console logs to avoid them to be captured in snapshots --- packages/jest-config/src/readConfigFileAndSetRootDir.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index e9548efcfeeb..756a0193cd22 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -100,8 +100,6 @@ export default async function readConfigFileAndSetRootDir( // Load the TypeScript configuration const loadTSConfigFile = (configPath: Config.Path): Config.InitialOptions => { - console.info(`Jest: Loading the TS config file: ${configPath}`); - let registerer; // Register TypeScript compiler instance From de5766ba6675e5b762546fc464dcb5745f064a36 Mon Sep 17 00:00:00 2001 From: Gamote Date: Wed, 30 Sep 2020 01:52:50 +0200 Subject: [PATCH 10/38] fix: Remove console logs to avoid them to be captured in snapshots --- e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap | 2 -- 1 file changed, 2 deletions(-) diff --git a/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap b/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap index 759244e10052..231d926a36de 100644 --- a/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap +++ b/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap @@ -1,8 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`traverses directory tree up until it finds jest.config 1`] = ` -Jest: Loading the TS config file: /Users/david/Projects/Gamote/Libraries/jest/e2e/jest.config.ts/jest.config.ts -Jest: Loading the TS config file: /Users/david/Projects/Gamote/Libraries/jest/e2e/jest.config.ts/jest.config.ts console.log <>/jest.config.ts/some/nested/directory From c9069fdb25b5afb6abf0747b7e991cb2100634d2 Mon Sep 17 00:00:00 2001 From: Gamote Date: Wed, 30 Sep 2020 02:23:36 +0200 Subject: [PATCH 11/38] fix: Added 'testEnvironment' in the `jest.config.ts` tests to fix the 'ReferenceError: globalThis is not defined' on Node 10.x (https://github.com/nrwl/nx/issues/3776#issuecomment-699013760) --- e2e/__tests__/jest.config.ts.test.ts | 4 +- package.json | 1 + yarn.lock | 313 +++++++++++++++++++++++++-- 3 files changed, 294 insertions(+), 24 deletions(-) diff --git a/e2e/__tests__/jest.config.ts.test.ts b/e2e/__tests__/jest.config.ts.test.ts index fef7250ac3b3..07d20129cd41 100644 --- a/e2e/__tests__/jest.config.ts.test.ts +++ b/e2e/__tests__/jest.config.ts.test.ts @@ -18,7 +18,7 @@ afterAll(() => cleanup(DIR)); test('works with jest.config.ts', () => { writeFiles(DIR, { '__tests__/a-giraffe.js': `test('giraffe', () => expect(1).toBe(1));`, - 'jest.config.ts': `export default {testRegex: '.*-giraffe.js'};`, + 'jest.config.ts': `export default {testEnvironment: 'jest-environment-jsdom-fifteen', testRegex: '.*-giraffe.js'};`, 'package.json': '{}', }); @@ -36,7 +36,7 @@ test('traverses directory tree up until it finds jest.config', () => { test('giraffe', () => expect(1).toBe(1)); test('abc', () => console.log(slash(process.cwd()))); `, - 'jest.config.ts': `export default {testRegex: '.*-giraffe.js'};`, + 'jest.config.ts': `export default {testEnvironment: 'jest-environment-jsdom-fifteen', testRegex: '.*-giraffe.js'};`, 'package.json': '{}', 'some/nested/directory/file.js': '// nothing special', }); diff --git a/package.json b/package.json index e28af6c83b9c..d9297a81075b 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "istanbul-lib-report": "^3.0.0", "istanbul-reports": "^3.0.0", "jest": "workspace:packages/jest", + "jest-environment-jsdom-fifteen": "^1.0.2", "jest-junit": "^11.0.1", "jest-runner-tsd": "^1.1.0", "jest-silent-reporter": "^0.2.1", diff --git a/yarn.lock b/yarn.lock index 892fe2152b44..b42a95a0fd61 100644 --- a/yarn.lock +++ b/yarn.lock @@ -135,7 +135,7 @@ __metadata: languageName: node linkType: hard -"@babel/generator@npm:^7.11.5, @babel/generator@npm:^7.11.6, @babel/generator@npm:^7.5.0": +"@babel/generator@npm:^7.11.5, @babel/generator@npm:^7.11.6, @babel/generator@npm:^7.4.0, @babel/generator@npm:^7.5.0": version: 7.11.6 resolution: "@babel/generator@npm:7.11.6" dependencies: @@ -430,7 +430,7 @@ __metadata: languageName: node linkType: hard -"@babel/parser@npm:^7.0.0, @babel/parser@npm:^7.1.0, @babel/parser@npm:^7.10.4, @babel/parser@npm:^7.11.5, @babel/parser@npm:^7.7.0": +"@babel/parser@npm:^7.0.0, @babel/parser@npm:^7.1.0, @babel/parser@npm:^7.10.4, @babel/parser@npm:^7.11.5, @babel/parser@npm:^7.4.3, @babel/parser@npm:^7.7.0": version: 7.11.5 resolution: "@babel/parser@npm:7.11.5" bin: @@ -1523,7 +1523,7 @@ __metadata: languageName: node linkType: hard -"@babel/template@npm:^7.0.0, @babel/template@npm:^7.10.4, @babel/template@npm:^7.3.3": +"@babel/template@npm:^7.0.0, @babel/template@npm:^7.10.4, @babel/template@npm:^7.3.3, @babel/template@npm:^7.4.0": version: 7.10.4 resolution: "@babel/template@npm:7.10.4" dependencies: @@ -1534,7 +1534,7 @@ __metadata: languageName: node linkType: hard -"@babel/traverse@npm:^7.0.0, @babel/traverse@npm:^7.1.0, @babel/traverse@npm:^7.10.4, @babel/traverse@npm:^7.11.5, @babel/traverse@npm:^7.3.4, @babel/traverse@npm:^7.7.0, @babel/traverse@npm:^7.9.0": +"@babel/traverse@npm:^7.0.0, @babel/traverse@npm:^7.1.0, @babel/traverse@npm:^7.10.4, @babel/traverse@npm:^7.11.5, @babel/traverse@npm:^7.3.4, @babel/traverse@npm:^7.4.3, @babel/traverse@npm:^7.7.0, @babel/traverse@npm:^7.9.0": version: 7.11.5 resolution: "@babel/traverse@npm:7.11.5" dependencies: @@ -1551,7 +1551,7 @@ __metadata: languageName: node linkType: hard -"@babel/types@npm:^7.0.0, @babel/types@npm:^7.10.4, @babel/types@npm:^7.10.5, @babel/types@npm:^7.11.0, @babel/types@npm:^7.11.5, @babel/types@npm:^7.3.0, @babel/types@npm:^7.3.3, @babel/types@npm:^7.4.4, @babel/types@npm:^7.7.0, @babel/types@npm:^7.9.0": +"@babel/types@npm:^7.0.0, @babel/types@npm:^7.10.4, @babel/types@npm:^7.10.5, @babel/types@npm:^7.11.0, @babel/types@npm:^7.11.5, @babel/types@npm:^7.3.0, @babel/types@npm:^7.3.3, @babel/types@npm:^7.4.0, @babel/types@npm:^7.4.4, @babel/types@npm:^7.7.0, @babel/types@npm:^7.9.0": version: 7.11.5 resolution: "@babel/types@npm:7.11.5" dependencies: @@ -1819,6 +1819,18 @@ __metadata: languageName: unknown linkType: soft +"@jest/environment@npm:^24.3.0": + version: 24.9.0 + resolution: "@jest/environment@npm:24.9.0" + dependencies: + "@jest/fake-timers": ^24.9.0 + "@jest/transform": ^24.9.0 + "@jest/types": ^24.9.0 + jest-mock: ^24.9.0 + checksum: 77f7313e1b913253b63edc5742aa9fa5e07f38d39b703d5f6246e4dd9778718b99313514c6245fe37791e64fd98fc7cc2fd12c98c75b05d916ec67a877d3943c + languageName: node + linkType: hard + "@jest/fake-timers@^26.3.0, @jest/fake-timers@workspace:packages/jest-fake-timers": version: 0.0.0-use.local resolution: "@jest/fake-timers@workspace:packages/jest-fake-timers" @@ -1833,7 +1845,7 @@ __metadata: languageName: unknown linkType: soft -"@jest/fake-timers@npm:^24.9.0": +"@jest/fake-timers@npm:^24.3.0, @jest/fake-timers@npm:^24.9.0": version: 24.9.0 resolution: "@jest/fake-timers@npm:24.9.0" dependencies: @@ -2000,6 +2012,30 @@ __metadata: languageName: unknown linkType: soft +"@jest/transform@npm:^24.9.0": + version: 24.9.0 + resolution: "@jest/transform@npm:24.9.0" + dependencies: + "@babel/core": ^7.1.0 + "@jest/types": ^24.9.0 + babel-plugin-istanbul: ^5.1.0 + chalk: ^2.0.1 + convert-source-map: ^1.4.0 + fast-json-stable-stringify: ^2.0.0 + graceful-fs: ^4.1.15 + jest-haste-map: ^24.9.0 + jest-regex-util: ^24.9.0 + jest-util: ^24.9.0 + micromatch: ^3.1.10 + pirates: ^4.0.1 + realpath-native: ^1.1.0 + slash: ^2.0.0 + source-map: ^0.6.1 + write-file-atomic: 2.4.1 + checksum: 73c5ad0ae6bae5c60261b6b256b995f099f84a964580537154293edc63ab0e9fb6e3dda737c04aafd9daa815f19b6fb437e611f4f811f8041bd37e8192709650 + languageName: node + linkType: hard + "@jest/types@^26.3.0, @jest/types@workspace:packages/jest-types": version: 0.0.0-use.local resolution: "@jest/types@workspace:packages/jest-types" @@ -2012,7 +2048,7 @@ __metadata: languageName: unknown linkType: soft -"@jest/types@npm:^24.9.0": +"@jest/types@npm:^24.3.0, @jest/types@npm:^24.9.0": version: 24.9.0 resolution: "@jest/types@npm:24.9.0" dependencies: @@ -3962,7 +3998,7 @@ __metadata: languageName: node linkType: hard -"abab@npm:^2.0.3": +"abab@npm:^2.0.0, abab@npm:^2.0.3": version: 2.0.5 resolution: "abab@npm:2.0.5" checksum: a42b91bd9dd2451a3fc6996bc8953139904ff7b1a793719205041148da892337afc97ed0589ef2c44765c4da3d688eed145781db1623b611621d805294c367a3 @@ -4002,6 +4038,16 @@ __metadata: languageName: node linkType: hard +"acorn-globals@npm:^4.3.2": + version: 4.3.4 + resolution: "acorn-globals@npm:4.3.4" + dependencies: + acorn: ^6.0.1 + acorn-walk: ^6.0.1 + checksum: 6c3511f40d25daefda449b803f9d651c1b2427009d5dc74ae485efe5b704be0ce17983ac9571df3f5344a6ab1df77a29cb4e19c5f34796cec3c1c049f3ad5951 + languageName: node + linkType: hard + "acorn-globals@npm:^6.0.0": version: 6.0.0 resolution: "acorn-globals@npm:6.0.0" @@ -4021,6 +4067,13 @@ __metadata: languageName: node linkType: hard +"acorn-walk@npm:^6.0.1": + version: 6.2.0 + resolution: "acorn-walk@npm:6.2.0" + checksum: 3bd8415090ecfcf0a40e9bdde722993104d209d8e7721b48d9c77c46fb1dd261cc29ae0ee47e6532db9fbfe96d911b19ec0d72a383b20ed331364ab18d35b75b + languageName: node + linkType: hard + "acorn-walk@npm:^7.1.1": version: 7.2.0 resolution: "acorn-walk@npm:7.2.0" @@ -4028,7 +4081,16 @@ __metadata: languageName: node linkType: hard -"acorn@npm:^7.1.1, acorn@npm:^7.4.0": +"acorn@npm:^6.0.1": + version: 6.4.1 + resolution: "acorn@npm:6.4.1" + bin: + acorn: bin/acorn + checksum: 7aa4623c6d2705e9a26057ccfdd409154f8b634973ce109a63fa2c7e679af689bb50378379610794ec9744975db7a3a3b97e2b83f87fab1b635ad19b6c0ac3be + languageName: node + linkType: hard + +"acorn@npm:^7.1.0, acorn@npm:^7.1.1, acorn@npm:^7.4.0": version: 7.4.0 resolution: "acorn@npm:7.4.0" bin: @@ -4445,6 +4507,13 @@ __metadata: languageName: node linkType: hard +"array-equal@npm:^1.0.0": + version: 1.0.0 + resolution: "array-equal@npm:1.0.0" + checksum: ad82ed549385a7cacb7ed3a2be9cef73ccc0ebf371e4a30635bfc5737464f7fd5c70433e25c1bbdeec3d230d41be13e46b778e5a373300655531b4b7eff1f447 + languageName: node + linkType: hard + "array-filter@npm:^1.0.0": version: 1.0.0 resolution: "array-filter@npm:1.0.0" @@ -4781,6 +4850,18 @@ __metadata: languageName: node linkType: hard +"babel-plugin-istanbul@npm:^5.1.0": + version: 5.2.0 + resolution: "babel-plugin-istanbul@npm:5.2.0" + dependencies: + "@babel/helper-plugin-utils": ^7.0.0 + find-up: ^3.0.0 + istanbul-lib-instrument: ^3.3.0 + test-exclude: ^5.2.3 + checksum: e94429f5c2fbc6b098f8ded77addabe5d229a8c4c8d449b746396c9f05e419ef41e7582aa19f8c1674c6774f9029f686653796e15de494f63ceef40d1f60e083 + languageName: node + linkType: hard + "babel-plugin-istanbul@npm:^6.0.0": version: 6.0.0 resolution: "babel-plugin-istanbul@npm:6.0.0" @@ -6709,7 +6790,7 @@ __metadata: languageName: node linkType: hard -"cssom@npm:^0.4.4": +"cssom@npm:^0.4.1, cssom@npm:^0.4.4": version: 0.4.4 resolution: "cssom@npm:0.4.4" checksum: db81cac44219b20d76b06f51d2614cead098478d1323b2df5e4b5d25bdc3f16d8474c3d45ae28f594a0933691c774fc2102837df66ccf375e280b0728ad53c5f @@ -6723,7 +6804,7 @@ __metadata: languageName: node linkType: hard -"cssstyle@npm:^2.2.0": +"cssstyle@npm:^2.0.0, cssstyle@npm:^2.2.0": version: 2.3.0 resolution: "cssstyle@npm:2.3.0" dependencies: @@ -6780,6 +6861,17 @@ __metadata: languageName: node linkType: hard +"data-urls@npm:^1.1.0": + version: 1.1.0 + resolution: "data-urls@npm:1.1.0" + dependencies: + abab: ^2.0.0 + whatwg-mimetype: ^2.2.0 + whatwg-url: ^7.0.0 + checksum: 04d211e1e9f83bab75450487da34b124b32beacd1ad0df96e3a747b705c24c65579833a04a6ea30a528ea5b99d5247660408c513b38905bf855f2de585b9e91c + languageName: node + linkType: hard + "data-urls@npm:^2.0.0": version: 2.0.0 resolution: "data-urls@npm:2.0.0" @@ -7317,6 +7409,15 @@ __metadata: languageName: node linkType: hard +"domexception@npm:^1.0.1": + version: 1.0.1 + resolution: "domexception@npm:1.0.1" + dependencies: + webidl-conversions: ^4.0.2 + checksum: 0a678e600248b8a6f0149cb6a6ddae77d698d16a6fcf39d4228b933d5ac2b9ee657a36b2cd08ea82ec6196da756535bd30b8362f697cc9e564d969e52437fcd8 + languageName: node + linkType: hard + "domexception@npm:^2.0.1": version: 2.0.1 resolution: "domexception@npm:2.0.1" @@ -7785,7 +7886,7 @@ __metadata: languageName: node linkType: hard -"escodegen@npm:^1.14.1": +"escodegen@npm:^1.11.1, escodegen@npm:^1.14.1": version: 1.14.3 resolution: "escodegen@npm:1.14.3" dependencies: @@ -10070,6 +10171,15 @@ fsevents@^1.2.7: languageName: node linkType: hard +"html-encoding-sniffer@npm:^1.0.2": + version: 1.0.2 + resolution: "html-encoding-sniffer@npm:1.0.2" + dependencies: + whatwg-encoding: ^1.0.1 + checksum: fff1462d9845f08315b41a19b3deaeebf465b4abc44c12218ee2be42a4655dec18b8ca4ae2ea72270d564164a3092b9a72701c1c529777e378036a49c4f6bc80 + languageName: node + linkType: hard + "html-encoding-sniffer@npm:^2.0.1": version: 2.0.1 resolution: "html-encoding-sniffer@npm:2.0.1" @@ -11290,6 +11400,13 @@ fsevents@^1.2.7: languageName: node linkType: hard +"istanbul-lib-coverage@npm:^2.0.5": + version: 2.0.5 + resolution: "istanbul-lib-coverage@npm:2.0.5" + checksum: 72737ebc48c31a45ab80fb1161b4c79a7d035d3088007ec55ec7a53b8bf6ae107a8222335e018978720270d71f2036abe73e150da4733f573be32398ad6aedd1 + languageName: node + linkType: hard + "istanbul-lib-coverage@npm:^3.0.0": version: 3.0.0 resolution: "istanbul-lib-coverage@npm:3.0.0" @@ -11297,6 +11414,21 @@ fsevents@^1.2.7: languageName: node linkType: hard +"istanbul-lib-instrument@npm:^3.3.0": + version: 3.3.0 + resolution: "istanbul-lib-instrument@npm:3.3.0" + dependencies: + "@babel/generator": ^7.4.0 + "@babel/parser": ^7.4.3 + "@babel/template": ^7.4.0 + "@babel/traverse": ^7.4.3 + "@babel/types": ^7.4.0 + istanbul-lib-coverage: ^2.0.5 + semver: ^6.0.0 + checksum: d7a7dae5db459ac4365cea3ecdaf0586c79bfb850059e2fc2364c060ca6bcbbf686675d8944d6490a52f0d018781403ec5902523430e7a404d4f2b2ad82e1aef + languageName: node + linkType: hard + "istanbul-lib-instrument@npm:^4.0.0, istanbul-lib-instrument@npm:^4.0.3": version: 4.0.3 resolution: "istanbul-lib-instrument@npm:4.0.3" @@ -11491,6 +11623,20 @@ fsevents@^1.2.7: languageName: unknown linkType: soft +"jest-environment-jsdom-fifteen@npm:^1.0.2": + version: 1.0.2 + resolution: "jest-environment-jsdom-fifteen@npm:1.0.2" + dependencies: + "@jest/environment": ^24.3.0 + "@jest/fake-timers": ^24.3.0 + "@jest/types": ^24.3.0 + jest-mock: ^24.0.0 + jest-util: ^24.0.0 + jsdom: ^15.2.1 + checksum: 9951594e15af8c9ab1ef8bb2490103d115728cdd1ef423b10a2b3ee3ab8569d1c6ba5c7cf21b3424d123ef14159f2b7bcd4d32a703c8470245ea94b6137c2cc9 + languageName: node + linkType: hard + "jest-environment-jsdom@^26.3.0, jest-environment-jsdom@workspace:packages/jest-environment-jsdom": version: 0.0.0-use.local resolution: "jest-environment-jsdom@workspace:packages/jest-environment-jsdom" @@ -11562,7 +11708,7 @@ fsevents@^1.2.7: languageName: unknown linkType: soft -"jest-haste-map@npm:^24.7.1": +"jest-haste-map@npm:^24.7.1, jest-haste-map@npm:^24.9.0": version: 24.9.0 resolution: "jest-haste-map@npm:24.9.0" dependencies: @@ -11691,7 +11837,7 @@ fsevents@^1.2.7: languageName: unknown linkType: soft -"jest-mock@npm:^24.9.0": +"jest-mock@npm:^24.0.0, jest-mock@npm:^24.9.0": version: 24.9.0 resolution: "jest-mock@npm:24.9.0" dependencies: @@ -11728,6 +11874,13 @@ fsevents@^1.2.7: languageName: unknown linkType: soft +"jest-regex-util@npm:^24.9.0": + version: 24.9.0 + resolution: "jest-regex-util@npm:24.9.0" + checksum: 3a30d04bcfd779397d38c6b663c0e45492bba83908c57d3498bd682aa4dd299565edb7620e38de2225c19bc06ad8febfb268101494caee39b08c1d1493050a8d + languageName: node + linkType: hard + "jest-repl@workspace:packages/jest-repl": version: 0.0.0-use.local resolution: "jest-repl@workspace:packages/jest-repl" @@ -12169,6 +12322,45 @@ fsevents@^1.2.7: languageName: node linkType: hard +"jsdom@npm:^15.2.1": + version: 15.2.1 + resolution: "jsdom@npm:15.2.1" + dependencies: + abab: ^2.0.0 + acorn: ^7.1.0 + acorn-globals: ^4.3.2 + array-equal: ^1.0.0 + cssom: ^0.4.1 + cssstyle: ^2.0.0 + data-urls: ^1.1.0 + domexception: ^1.0.1 + escodegen: ^1.11.1 + html-encoding-sniffer: ^1.0.2 + nwsapi: ^2.2.0 + parse5: 5.1.0 + pn: ^1.1.0 + request: ^2.88.0 + request-promise-native: ^1.0.7 + saxes: ^3.1.9 + symbol-tree: ^3.2.2 + tough-cookie: ^3.0.1 + w3c-hr-time: ^1.0.1 + w3c-xmlserializer: ^1.1.2 + webidl-conversions: ^4.0.2 + whatwg-encoding: ^1.0.5 + whatwg-mimetype: ^2.3.0 + whatwg-url: ^7.0.0 + ws: ^7.0.0 + xml-name-validator: ^3.0.0 + peerDependencies: + canvas: ^2.5.0 + peerDependenciesMeta: + canvas: + optional: true + checksum: 706d227c378e88fb13528252cc83fa18ec6d9810ee081478da3c392f9d2a543a504f23aa5d811943c9ef545cdf17850fc48db8e8aa62e5b8162f2916bc2bb008 + languageName: node + linkType: hard + "jsdom@npm:^16.2.2": version: 16.4.0 resolution: "jsdom@npm:16.4.0" @@ -15106,6 +15298,13 @@ fsevents@^1.2.7: languageName: node linkType: hard +"parse5@npm:5.1.0": + version: 5.1.0 + resolution: "parse5@npm:5.1.0" + checksum: f82ab2581011704c1dd3f56fa9509904a169d06bee8d4154d40a774335ad158bc59693c6620d29093252ad120521302ff25b257bcc9aebbe12453f74659a5d65 + languageName: node + linkType: hard + "parse5@npm:5.1.1": version: 5.1.1 resolution: "parse5@npm:5.1.1" @@ -15380,6 +15579,13 @@ fsevents@^1.2.7: languageName: node linkType: hard +"pn@npm:^1.1.0": + version: 1.1.0 + resolution: "pn@npm:1.1.0" + checksum: 7df19be13c86dfab22e8484590480e49d496b270430a731be0bb40cea8a16c29e45188a7303d7c57b7140754f807877b0c10aa95400ad30a7ad4fb3f7d132381 + languageName: node + linkType: hard + "portfinder@npm:^1.0.25": version: 1.0.28 resolution: "portfinder@npm:1.0.28" @@ -16398,6 +16604,16 @@ fsevents@^1.2.7: languageName: node linkType: hard +"read-pkg-up@npm:^4.0.0": + version: 4.0.0 + resolution: "read-pkg-up@npm:4.0.0" + dependencies: + find-up: ^3.0.0 + read-pkg: ^3.0.0 + checksum: e611538e096723fa15f36960a293b26704145d646a3ddae6a206fa50ddba18f655b2901581ef06943758cebe8660bbf6b3b07bad645f2256cf2f775e64867ea5 + languageName: node + linkType: hard + "read-pkg-up@npm:^7.0.0, read-pkg-up@npm:^7.0.1": version: 7.0.1 resolution: "read-pkg-up@npm:7.0.1" @@ -16521,6 +16737,15 @@ fsevents@^1.2.7: languageName: node linkType: hard +"realpath-native@npm:^1.1.0": + version: 1.1.0 + resolution: "realpath-native@npm:1.1.0" + dependencies: + util.promisify: ^1.0.0 + checksum: 67ce6bdaf8f8dd2a85e771b7b79b74b8a47299315a0a3553947df1ab4117de80d1910a2ba856a480d9e4284172cf8d7df209117f5522475e30bb7ecdee63b75b + languageName: node + linkType: hard + "rechoir@npm:^0.6.2": version: 0.6.2 resolution: "rechoir@npm:0.6.2" @@ -16794,7 +17019,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"request-promise-native@npm:^1.0.8": +"request-promise-native@npm:^1.0.7, request-promise-native@npm:^1.0.8": version: 1.0.9 resolution: "request-promise-native@npm:1.0.9" dependencies: @@ -17091,6 +17316,7 @@ fsevents@^1.2.7: istanbul-lib-report: ^3.0.0 istanbul-reports: ^3.0.0 jest: "workspace:packages/jest" + jest-environment-jsdom-fifteen: ^1.0.2 jest-junit: ^11.0.1 jest-runner-tsd: ^1.1.0 jest-silent-reporter: ^0.2.1 @@ -17266,6 +17492,15 @@ fsevents@^1.2.7: languageName: node linkType: hard +"saxes@npm:^3.1.9": + version: 3.1.11 + resolution: "saxes@npm:3.1.11" + dependencies: + xmlchars: ^2.1.1 + checksum: dbdbd14f903e2a18c3efb422401ad0630dd25e4ed6a52fd01e42b205508ee70e5170da4d39ab2957eca54dc2934b9c8fa6f2f90292b136bfa935db7877177a08 + languageName: node + linkType: hard + "saxes@npm:^5.0.0": version: 5.0.1 resolution: "saxes@npm:5.0.1" @@ -18397,7 +18632,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"symbol-tree@npm:^3.2.4": +"symbol-tree@npm:^3.2.2, symbol-tree@npm:^3.2.4": version: 3.2.4 resolution: "symbol-tree@npm:3.2.4" checksum: 0b9af4e5f005f9f0b9c916d91a1b654422ffa49ef09c5c4b6efa7a778f63976be9f410e57db1e9ea7576eea0631a34b69a5622674aa92a60a896ccf2afca87a7 @@ -18580,6 +18815,18 @@ fsevents@^1.2.7: languageName: node linkType: hard +"test-exclude@npm:^5.2.3": + version: 5.2.3 + resolution: "test-exclude@npm:5.2.3" + dependencies: + glob: ^7.1.3 + minimatch: ^3.0.4 + read-pkg-up: ^4.0.0 + require-main-filename: ^2.0.0 + checksum: d441f2531cf102d267de7f4ceecb4eacc8de2a6703abbab20591d0e8b30877a0e4cdcb88f88bd292f36950feda87b25e159e2fd407c275b13cce15a2a56eefaf + languageName: node + linkType: hard + "test-exclude@npm:^6.0.0": version: 6.0.0 resolution: "test-exclude@npm:6.0.0" @@ -19493,7 +19740,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"util.promisify@npm:~1.0.0": +"util.promisify@npm:^1.0.0, util.promisify@npm:~1.0.0": version: 1.0.1 resolution: "util.promisify@npm:1.0.1" dependencies: @@ -19627,7 +19874,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"w3c-hr-time@npm:^1.0.2": +"w3c-hr-time@npm:^1.0.1, w3c-hr-time@npm:^1.0.2": version: 1.0.2 resolution: "w3c-hr-time@npm:1.0.2" dependencies: @@ -19636,6 +19883,17 @@ fsevents@^1.2.7: languageName: node linkType: hard +"w3c-xmlserializer@npm:^1.1.2": + version: 1.1.2 + resolution: "w3c-xmlserializer@npm:1.1.2" + dependencies: + domexception: ^1.0.1 + webidl-conversions: ^4.0.2 + xml-name-validator: ^3.0.0 + checksum: 9a7b5c7e32d4fa3d272a38e62595ff43169a9aa1b000d27a6b2613df759071034a8e870f7e6ebae8d0024d3056eeff1cad0fdab118ad4430c3d1cac3384dcd29 + languageName: node + linkType: hard + "w3c-xmlserializer@npm:^2.0.0": version: 2.0.0 resolution: "w3c-xmlserializer@npm:2.0.0" @@ -19714,7 +19972,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"whatwg-encoding@npm:^1.0.5": +"whatwg-encoding@npm:^1.0.1, whatwg-encoding@npm:^1.0.5": version: 1.0.5 resolution: "whatwg-encoding@npm:1.0.5" dependencies: @@ -19737,7 +19995,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"whatwg-mimetype@npm:^2.3.0": +"whatwg-mimetype@npm:^2.2.0, whatwg-mimetype@npm:^2.3.0": version: 2.3.0 resolution: "whatwg-mimetype@npm:2.3.0" checksum: 926e6ef8c7e53d158e501ce5e3c0e491d343c3c97e71b3d30451ffe4b1d6f81844c336b46a446a0b4f3fe4f327d76e3451d53ee8055344a0f5f2f35b84518011 @@ -19901,6 +20159,17 @@ fsevents@^1.2.7: languageName: node linkType: hard +"write-file-atomic@npm:2.4.1": + version: 2.4.1 + resolution: "write-file-atomic@npm:2.4.1" + dependencies: + graceful-fs: ^4.1.11 + imurmurhash: ^0.1.4 + signal-exit: ^3.0.2 + checksum: d5a00706d00cb4a13bca748d85d4d149b9a997201cdbedc9162810c9ac04188e191b1b06ca868df670db972ae9dbd4022a4eff2aec0c7dce73376dccb6d4efab + languageName: node + linkType: hard + "write-file-atomic@npm:^1.2.0": version: 1.3.4 resolution: "write-file-atomic@npm:1.3.4" @@ -19992,7 +20261,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"ws@npm:^7, ws@npm:^7.2.3": +"ws@npm:^7, ws@npm:^7.0.0, ws@npm:^7.2.3": version: 7.3.1 resolution: "ws@npm:7.3.1" peerDependencies: @@ -20070,7 +20339,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"xmlchars@npm:^2.2.0": +"xmlchars@npm:^2.1.1, xmlchars@npm:^2.2.0": version: 2.2.0 resolution: "xmlchars@npm:2.2.0" checksum: 69bbb61e8d939873c8aa7d006d082944de2eb6f12f55e53fdfc670d544e677736b59e498ece303f264bd1dc39b77557eef1c1c9bfb09eb5e1e30ac552420d81e From 2cf29337773afbc2e92a37512cbe8b13cfadd557 Mon Sep 17 00:00:00 2001 From: Gamote Date: Wed, 30 Sep 2020 09:21:06 +0200 Subject: [PATCH 12/38] chore: `interopRequireDefault` was moved to save an import --- packages/jest-config/src/readConfigFileAndSetRootDir.ts | 5 ++++- packages/jest-config/src/utils.ts | 4 ---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 756a0193cd22..4c1a0114b4dd 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -9,7 +9,6 @@ import * as path from 'path'; import {pathToFileURL} from 'url'; import * as fs from 'graceful-fs'; import type {Config} from '@jest/types'; -import {interopRequireDefault} from './utils'; // @ts-expect-error: vendored import jsonlint from './vendor/jsonlint'; import { @@ -100,6 +99,10 @@ export default async function readConfigFileAndSetRootDir( // Load the TypeScript configuration const loadTSConfigFile = (configPath: Config.Path): Config.InitialOptions => { + // Helper to normalize module's return value + const interopRequireDefault = (obj: any): {default: any} => + obj && obj.__esModule ? obj : {default: obj}; + let registerer; // Register TypeScript compiler instance diff --git a/packages/jest-config/src/utils.ts b/packages/jest-config/src/utils.ts index ad69663235e0..baa834fffcf9 100644 --- a/packages/jest-config/src/utils.ts +++ b/packages/jest-config/src/utils.ts @@ -248,7 +248,3 @@ export const getSequencer = ( prefix: 'jest-sequencer-', rootDir, }); - -// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types -export const interopRequireDefault = (obj: any): {default: any} => - obj && obj.__esModule ? obj : {default: obj}; From 144c341a8decc500ffeb86f13be5eea69b13b649 Mon Sep 17 00:00:00 2001 From: Gamote Date: Wed, 30 Sep 2020 09:23:13 +0200 Subject: [PATCH 13/38] chore: better comments for `readConfigFileAndSetRootDir` --- packages/jest-config/src/readConfigFileAndSetRootDir.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 4c1a0114b4dd..6fac36aebd92 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -19,7 +19,7 @@ import { // Read the configuration and set its `rootDir` // 1. If it's a `package.json` file, we look into its "jest" property -// 2. If it's a `jest.config.ts` file, we convert it in JS and we parse it +// 2. // 2. If it's a `jest.config.ts` file, we use `ts-node` to transpile & require it // 3. For any other file, we just require it. If we receive an 'ERR_REQUIRE_ESM' // from node, perform a dynamic import instead. export default async function readConfigFileAndSetRootDir( From 8fa7b6190f41169f6c501466f766361878b3a14c Mon Sep 17 00:00:00 2001 From: Gamote Date: Wed, 30 Sep 2020 09:26:07 +0200 Subject: [PATCH 14/38] chore: indicate that `ts-node` is optional via 'peerDependenciesMeta' --- packages/jest-config/package.json | 5 +++++ yarn.lock | 3 +++ 2 files changed, 8 insertions(+) diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index a6da1009aa21..2fe3997e2d65 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -12,6 +12,11 @@ "peerDependencies": { "ts-node": "^9.0.0" }, + "peerDependenciesMeta": { + "ts-node": { + "optional": true + } + }, "dependencies": { "@babel/core": "^7.1.0", "@jest/test-sequencer": "^26.4.2", diff --git a/yarn.lock b/yarn.lock index b42a95a0fd61..ed9107e76989 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11586,6 +11586,9 @@ fsevents@^1.2.7: pretty-format: ^26.4.2 peerDependencies: ts-node: ^9.0.0 + peerDependenciesMeta: + ts-node: + optional: true languageName: unknown linkType: soft From d8724e64f108e3be26abad895b6b4b77e8a76374 Mon Sep 17 00:00:00 2001 From: Gamote Date: Wed, 30 Sep 2020 09:30:14 +0200 Subject: [PATCH 15/38] chore: Updated documentation --- docs/Configuration.md | 4 +--- website/versioned_docs/version-26.4/Configuration.md | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index ee5980732aee..552c7b28310f 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -31,9 +31,7 @@ module.exports = async () => { }; ``` -Or through TypeScript: - -You can enable support for the TypeScript configuration file by installing the `ts-node` plugin in your project. +Or through TypeScript (if `ts-node` is installed): ```ts // jest.config.ts diff --git a/website/versioned_docs/version-26.4/Configuration.md b/website/versioned_docs/version-26.4/Configuration.md index 7b4597a0cc2f..fa7d003bdead 100644 --- a/website/versioned_docs/version-26.4/Configuration.md +++ b/website/versioned_docs/version-26.4/Configuration.md @@ -32,9 +32,7 @@ module.exports = async () => { }; ``` -Or through TypeScript: - -You can enable support for the TypeScript configuration file by installing the `ts-node` plugin in your project. +Or through TypeScript (if `ts-node` is installed): ```ts // jest.config.ts From a5033aac1ddb6f292c7a2a102da970cf8ea53b30 Mon Sep 17 00:00:00 2001 From: Gamote Date: Fri, 2 Oct 2020 09:54:36 +0200 Subject: [PATCH 16/38] chore: tests was added to be sure that the types are checked in the `jest.config.ts` file --- e2e/__tests__/jest.config.ts.test.ts | 12 ++++++++++++ .../jest-config/src/readConfigFileAndSetRootDir.ts | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/e2e/__tests__/jest.config.ts.test.ts b/e2e/__tests__/jest.config.ts.test.ts index 07d20129cd41..13f18406eaf2 100644 --- a/e2e/__tests__/jest.config.ts.test.ts +++ b/e2e/__tests__/jest.config.ts.test.ts @@ -58,6 +58,18 @@ test('traverses directory tree up until it finds jest.config', () => { expect(wrap(summary)).toMatchSnapshot(); }); +test('it does type check the config', () => { + writeFiles(DIR, { + '__tests__/a-giraffe.js': `test('giraffe', () => expect(1).toBe(1));`, + 'jest.config.ts': `export default { testTimeout: "10000" }`, + 'package.json': '{}', + }); + + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']); + expect(stderr).toMatch('must be of type'); + expect(exitCode).toBe(1); +}); + test('invalid JS in jest.config.ts', () => { writeFiles(DIR, { '__tests__/a-giraffe.js': `test('giraffe', () => expect(1).toBe(1));`, diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 6fac36aebd92..0186fc7852f2 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -19,7 +19,7 @@ import { // Read the configuration and set its `rootDir` // 1. If it's a `package.json` file, we look into its "jest" property -// 2. // 2. If it's a `jest.config.ts` file, we use `ts-node` to transpile & require it +// 2. If it's a `jest.config.ts` file, we use `ts-node` to transpile & require it // 3. For any other file, we just require it. If we receive an 'ERR_REQUIRE_ESM' // from node, perform a dynamic import instead. export default async function readConfigFileAndSetRootDir( From 4d49437de6482104221fea7e9a38533574513045 Mon Sep 17 00:00:00 2001 From: Gamote Date: Fri, 2 Oct 2020 13:29:11 +0200 Subject: [PATCH 17/38] chore: Update the fixture for `jest.config.ts` to use 'default' --- .../__tests__/fixtures/has_jest_config_file_ts/jest.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file_ts/jest.config.ts b/packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file_ts/jest.config.ts index 2da8322a7f9b..d291155b7a10 100644 --- a/packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file_ts/jest.config.ts +++ b/packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file_ts/jest.config.ts @@ -5,4 +5,4 @@ * LICENSE file in the root directory of this source tree. */ -export = {}; +export default {}; From e0b52706d89441254ac6690106303bf861c07cb0 Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 09:48:51 +0200 Subject: [PATCH 18/38] chore: (ts-node) allow for majors higher than 9 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f214845d4bee..04a813ccb4d6 100644 --- a/package.json +++ b/package.json @@ -78,7 +78,7 @@ "strip-ansi": "^6.0.0", "tempy": "^0.6.0", "throat": "^5.0.0", - "ts-node": "^9.0.0", + "ts-node": ">=9.0.0", "type-fest": "^0.16.0", "typescript": "^4.0.2", "which": "^2.0.1" From 0d6f2ca4c656064b0ff6d84bd9c4b7947b85c2d1 Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 10:12:33 +0200 Subject: [PATCH 19/38] chore: revert changes in the 26.4 documentation --- .../version-26.4/Configuration.md | 22 +------------------ 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/website/versioned_docs/version-26.4/Configuration.md b/website/versioned_docs/version-26.4/Configuration.md index 67a4f19a57bb..15b255b04fee 100644 --- a/website/versioned_docs/version-26.4/Configuration.md +++ b/website/versioned_docs/version-26.4/Configuration.md @@ -4,7 +4,7 @@ title: Configuring Jest original_id: configuration --- -Jest's configuration can be defined in the `package.json` file of your project, or through a `jest.config.js`, or `jest.config.ts` file or through the `--config ` option. If you'd like to use your `package.json` to store Jest's config, the `"jest"` key should be used on the top level so Jest will know how to find your settings: +Jest's configuration can be defined in the `package.json` file of your project, or through a `jest.config.js` file or through the `--config ` option. If you'd like to use your `package.json` to store Jest's config, the `"jest"` key should be used on the top level so Jest will know how to find your settings: ```json { @@ -32,26 +32,6 @@ module.exports = async () => { }; ``` -Or through TypeScript (if `ts-node` is installed): - -```ts -// jest.config.ts -import type {Config} from '@jest/types'; - -//Sync object -const config: Config.InitialOptions = { - verbose: true, -}; -export default config; - -//Or async function -export default async (): Promise => { - return { - verbose: true, - }; -}; -``` - Please keep in mind that the resulting configuration must be JSON-serializable. When using the `--config` option, the JSON file must not contain a "jest" key: From a291af25ba7b31510330fc512c0b51aabb18bc90 Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 10:14:07 +0200 Subject: [PATCH 20/38] chore: updated the return of 'loadTSConfigFile' --- packages/jest-config/src/readConfigFileAndSetRootDir.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 0186fc7852f2..27b2663a7807 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -98,7 +98,9 @@ export default async function readConfigFileAndSetRootDir( } // Load the TypeScript configuration -const loadTSConfigFile = (configPath: Config.Path): Config.InitialOptions => { +const loadTSConfigFile = ( + configPath: Config.Path, +): Config.InitialOptions | Promise => { // Helper to normalize module's return value const interopRequireDefault = (obj: any): {default: any} => obj && obj.__esModule ? obj : {default: obj}; From 5d4065eceef8362a1b1895d6b25219894aff42ac Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 10:17:19 +0200 Subject: [PATCH 21/38] chore: replace local 'interopRequireDefault' with the one defined in 'jest-util' --- packages/jest-config/src/readConfigFileAndSetRootDir.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 27b2663a7807..2745a1accdf7 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -9,6 +9,7 @@ import * as path from 'path'; import {pathToFileURL} from 'url'; import * as fs from 'graceful-fs'; import type {Config} from '@jest/types'; +import {interopRequireDefault} from 'jest-util'; // @ts-expect-error: vendored import jsonlint from './vendor/jsonlint'; import { @@ -101,10 +102,6 @@ export default async function readConfigFileAndSetRootDir( const loadTSConfigFile = ( configPath: Config.Path, ): Config.InitialOptions | Promise => { - // Helper to normalize module's return value - const interopRequireDefault = (obj: any): {default: any} => - obj && obj.__esModule ? obj : {default: obj}; - let registerer; // Register TypeScript compiler instance From ee9569bd327628ff9bbc0dbdd29683d940aab1a1 Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 10:35:47 +0200 Subject: [PATCH 22/38] chore: handle the case in which config is a function which imports more Typescript code --- .../jest-config/src/readConfigFileAndSetRootDir.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 2745a1accdf7..56652cbf49bf 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -99,9 +99,9 @@ export default async function readConfigFileAndSetRootDir( } // Load the TypeScript configuration -const loadTSConfigFile = ( +const loadTSConfigFile = async ( configPath: Config.Path, -): Config.InitialOptions | Promise => { +): Promise => { let registerer; // Register TypeScript compiler instance @@ -121,7 +121,12 @@ const loadTSConfigFile = ( registerer.enabled(true); - const configObject = interopRequireDefault(require(configPath)).default; + let configObject = interopRequireDefault(require(configPath)).default; + + // In case the config is a function which imports more Typescript code + if (typeof configObject === 'function') { + configObject = await configObject(); + } registerer.enabled(false); From 91de77a358daa1424102156a52a62a971398712a Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 10:37:50 +0200 Subject: [PATCH 23/38] chore: let the consumers to deal with ts-node errors --- packages/jest-config/src/readConfigFileAndSetRootDir.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 56652cbf49bf..65a341093b87 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -114,9 +114,7 @@ const loadTSConfigFile = async ( ); } - throw new Error( - `Jest: Could not register 'ts-node' to read the TypeScript file: ${configPath}`, - ); + throw e; } registerer.enabled(true); From 2f165448908752ce2ab068b42646b0344bb223e3 Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 10:39:35 +0200 Subject: [PATCH 24/38] chore: docs comments were updated --- docs/Configuration.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index eca8a86bb22b..6daa5f26ceab 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -18,12 +18,12 @@ Or through JavaScript: ```js // jest.config.js -//Sync object +// Sync object module.exports = { verbose: true, }; -//Or async function +// Or async function module.exports = async () => { return { verbose: true, @@ -37,13 +37,13 @@ Or through TypeScript (if `ts-node` is installed): // jest.config.ts import type {Config} from '@jest/types'; -//Sync object +// Sync object const config: Config.InitialOptions = { verbose: true, }; export default config; -//Or async function +// Or async function export default async (): Promise => { return { verbose: true, From b551b29acf88578300e335ba0289905d6860f3d6 Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 11:23:20 +0200 Subject: [PATCH 25/38] fix: tests were fixed after 'readConfigFileAndSetRootDir' updates --- e2e/__tests__/jest.config.ts.test.ts | 4 +--- packages/jest-config/src/readConfigFileAndSetRootDir.ts | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/e2e/__tests__/jest.config.ts.test.ts b/e2e/__tests__/jest.config.ts.test.ts index 13f18406eaf2..53337662aa75 100644 --- a/e2e/__tests__/jest.config.ts.test.ts +++ b/e2e/__tests__/jest.config.ts.test.ts @@ -78,8 +78,6 @@ test('invalid JS in jest.config.ts', () => { }); const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']); - expect(stderr).toMatch( - 'Error: Jest: Failed to parse the TypeScript config file ', - ); + expect(stderr).toMatch('TSError: ⨯ Unable to compile TypeScript:'); expect(exitCode).toBe(1); }); diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 65a341093b87..543ae17834bd 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -32,7 +32,7 @@ export default async function readConfigFileAndSetRootDir( try { if (isTS) { - configObject = loadTSConfigFile(configPath); + configObject = await loadTSConfigFile(configPath); } else { configObject = require(configPath); } From 4ec1eb13c363e607fed8b3a31105f9b1ecef7010 Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 11:38:09 +0200 Subject: [PATCH 26/38] chore: 'jest-environment-jsdom-fifteen' was replaced with 'jest-environment-node' --- e2e/__tests__/jest.config.ts.test.ts | 4 +- package.json | 2 +- yarn.lock | 318 +++------------------------ 3 files changed, 28 insertions(+), 296 deletions(-) diff --git a/e2e/__tests__/jest.config.ts.test.ts b/e2e/__tests__/jest.config.ts.test.ts index 53337662aa75..9b3edcef87ff 100644 --- a/e2e/__tests__/jest.config.ts.test.ts +++ b/e2e/__tests__/jest.config.ts.test.ts @@ -18,7 +18,7 @@ afterAll(() => cleanup(DIR)); test('works with jest.config.ts', () => { writeFiles(DIR, { '__tests__/a-giraffe.js': `test('giraffe', () => expect(1).toBe(1));`, - 'jest.config.ts': `export default {testEnvironment: 'jest-environment-jsdom-fifteen', testRegex: '.*-giraffe.js'};`, + 'jest.config.ts': `export default {testEnvironment: 'jest-environment-node', testRegex: '.*-giraffe.js'};`, 'package.json': '{}', }); @@ -36,7 +36,7 @@ test('traverses directory tree up until it finds jest.config', () => { test('giraffe', () => expect(1).toBe(1)); test('abc', () => console.log(slash(process.cwd()))); `, - 'jest.config.ts': `export default {testEnvironment: 'jest-environment-jsdom-fifteen', testRegex: '.*-giraffe.js'};`, + 'jest.config.ts': `export default {testEnvironment: 'jest-environment-node', testRegex: '.*-giraffe.js'};`, 'package.json': '{}', 'some/nested/directory/file.js': '// nothing special', }); diff --git a/package.json b/package.json index 04a813ccb4d6..e2f13e536238 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "istanbul-lib-report": "^3.0.0", "istanbul-reports": "^3.0.0", "jest": "workspace:packages/jest", - "jest-environment-jsdom-fifteen": "^1.0.2", + "jest-environment-node": "^26.5.2", "jest-junit": "^12.0.0", "jest-runner-tsd": "^1.1.0", "jest-silent-reporter": "^0.2.1", diff --git a/yarn.lock b/yarn.lock index 42362f97b647..2fd39aa6c257 100644 --- a/yarn.lock +++ b/yarn.lock @@ -135,7 +135,7 @@ __metadata: languageName: node linkType: hard -"@babel/generator@npm:^7.11.5, @babel/generator@npm:^7.11.6, @babel/generator@npm:^7.4.0, @babel/generator@npm:^7.5.0": +"@babel/generator@npm:^7.11.5, @babel/generator@npm:^7.11.6, @babel/generator@npm:^7.5.0": version: 7.11.6 resolution: "@babel/generator@npm:7.11.6" dependencies: @@ -430,7 +430,7 @@ __metadata: languageName: node linkType: hard -"@babel/parser@npm:^7.0.0, @babel/parser@npm:^7.1.0, @babel/parser@npm:^7.10.4, @babel/parser@npm:^7.11.5, @babel/parser@npm:^7.4.3, @babel/parser@npm:^7.7.0": +"@babel/parser@npm:^7.0.0, @babel/parser@npm:^7.1.0, @babel/parser@npm:^7.10.4, @babel/parser@npm:^7.11.5, @babel/parser@npm:^7.7.0": version: 7.11.5 resolution: "@babel/parser@npm:7.11.5" bin: @@ -1523,7 +1523,7 @@ __metadata: languageName: node linkType: hard -"@babel/template@npm:^7.0.0, @babel/template@npm:^7.10.4, @babel/template@npm:^7.3.3, @babel/template@npm:^7.4.0": +"@babel/template@npm:^7.0.0, @babel/template@npm:^7.10.4, @babel/template@npm:^7.3.3": version: 7.10.4 resolution: "@babel/template@npm:7.10.4" dependencies: @@ -1534,7 +1534,7 @@ __metadata: languageName: node linkType: hard -"@babel/traverse@npm:^7.0.0, @babel/traverse@npm:^7.1.0, @babel/traverse@npm:^7.10.4, @babel/traverse@npm:^7.11.5, @babel/traverse@npm:^7.3.4, @babel/traverse@npm:^7.4.3, @babel/traverse@npm:^7.7.0, @babel/traverse@npm:^7.9.0": +"@babel/traverse@npm:^7.0.0, @babel/traverse@npm:^7.1.0, @babel/traverse@npm:^7.10.4, @babel/traverse@npm:^7.11.5, @babel/traverse@npm:^7.3.4, @babel/traverse@npm:^7.7.0, @babel/traverse@npm:^7.9.0": version: 7.11.5 resolution: "@babel/traverse@npm:7.11.5" dependencies: @@ -1551,7 +1551,7 @@ __metadata: languageName: node linkType: hard -"@babel/types@npm:^7.0.0, @babel/types@npm:^7.10.4, @babel/types@npm:^7.10.5, @babel/types@npm:^7.11.0, @babel/types@npm:^7.11.5, @babel/types@npm:^7.3.0, @babel/types@npm:^7.3.3, @babel/types@npm:^7.4.0, @babel/types@npm:^7.4.4, @babel/types@npm:^7.7.0, @babel/types@npm:^7.8.3, @babel/types@npm:^7.9.0": +"@babel/types@npm:^7.0.0, @babel/types@npm:^7.10.4, @babel/types@npm:^7.10.5, @babel/types@npm:^7.11.0, @babel/types@npm:^7.11.5, @babel/types@npm:^7.3.0, @babel/types@npm:^7.3.3, @babel/types@npm:^7.4.4, @babel/types@npm:^7.7.0, @babel/types@npm:^7.8.3, @babel/types@npm:^7.9.0": version: 7.11.5 resolution: "@babel/types@npm:7.11.5" dependencies: @@ -1827,18 +1827,6 @@ __metadata: languageName: unknown linkType: soft -"@jest/environment@npm:^24.3.0": - version: 24.9.0 - resolution: "@jest/environment@npm:24.9.0" - dependencies: - "@jest/fake-timers": ^24.9.0 - "@jest/transform": ^24.9.0 - "@jest/types": ^24.9.0 - jest-mock: ^24.9.0 - checksum: 77f7313e1b913253b63edc5742aa9fa5e07f38d39b703d5f6246e4dd9778718b99313514c6245fe37791e64fd98fc7cc2fd12c98c75b05d916ec67a877d3943c - languageName: node - linkType: hard - "@jest/fake-timers@^26.5.2, @jest/fake-timers@workspace:packages/jest-fake-timers": version: 0.0.0-use.local resolution: "@jest/fake-timers@workspace:packages/jest-fake-timers" @@ -1853,7 +1841,7 @@ __metadata: languageName: unknown linkType: soft -"@jest/fake-timers@npm:^24.3.0, @jest/fake-timers@npm:^24.9.0": +"@jest/fake-timers@npm:^24.9.0": version: 24.9.0 resolution: "@jest/fake-timers@npm:24.9.0" dependencies: @@ -2020,30 +2008,6 @@ __metadata: languageName: unknown linkType: soft -"@jest/transform@npm:^24.9.0": - version: 24.9.0 - resolution: "@jest/transform@npm:24.9.0" - dependencies: - "@babel/core": ^7.1.0 - "@jest/types": ^24.9.0 - babel-plugin-istanbul: ^5.1.0 - chalk: ^2.0.1 - convert-source-map: ^1.4.0 - fast-json-stable-stringify: ^2.0.0 - graceful-fs: ^4.1.15 - jest-haste-map: ^24.9.0 - jest-regex-util: ^24.9.0 - jest-util: ^24.9.0 - micromatch: ^3.1.10 - pirates: ^4.0.1 - realpath-native: ^1.1.0 - slash: ^2.0.0 - source-map: ^0.6.1 - write-file-atomic: 2.4.1 - checksum: 73c5ad0ae6bae5c60261b6b256b995f099f84a964580537154293edc63ab0e9fb6e3dda737c04aafd9daa815f19b6fb437e611f4f811f8041bd37e8192709650 - languageName: node - linkType: hard - "@jest/types@^26.5.2, @jest/types@workspace:packages/jest-types": version: 0.0.0-use.local resolution: "@jest/types@workspace:packages/jest-types" @@ -2056,7 +2020,7 @@ __metadata: languageName: unknown linkType: soft -"@jest/types@npm:^24.3.0, @jest/types@npm:^24.9.0": +"@jest/types@npm:^24.9.0": version: 24.9.0 resolution: "@jest/types@npm:24.9.0" dependencies: @@ -4006,7 +3970,7 @@ __metadata: languageName: node linkType: hard -"abab@npm:^2.0.0, abab@npm:^2.0.3": +"abab@npm:^2.0.3": version: 2.0.5 resolution: "abab@npm:2.0.5" checksum: a42b91bd9dd2451a3fc6996bc8953139904ff7b1a793719205041148da892337afc97ed0589ef2c44765c4da3d688eed145781db1623b611621d805294c367a3 @@ -4046,16 +4010,6 @@ __metadata: languageName: node linkType: hard -"acorn-globals@npm:^4.3.2": - version: 4.3.4 - resolution: "acorn-globals@npm:4.3.4" - dependencies: - acorn: ^6.0.1 - acorn-walk: ^6.0.1 - checksum: 6c3511f40d25daefda449b803f9d651c1b2427009d5dc74ae485efe5b704be0ce17983ac9571df3f5344a6ab1df77a29cb4e19c5f34796cec3c1c049f3ad5951 - languageName: node - linkType: hard - "acorn-globals@npm:^6.0.0": version: 6.0.0 resolution: "acorn-globals@npm:6.0.0" @@ -4075,13 +4029,6 @@ __metadata: languageName: node linkType: hard -"acorn-walk@npm:^6.0.1": - version: 6.2.0 - resolution: "acorn-walk@npm:6.2.0" - checksum: 3bd8415090ecfcf0a40e9bdde722993104d209d8e7721b48d9c77c46fb1dd261cc29ae0ee47e6532db9fbfe96d911b19ec0d72a383b20ed331364ab18d35b75b - languageName: node - linkType: hard - "acorn-walk@npm:^7.1.1": version: 7.2.0 resolution: "acorn-walk@npm:7.2.0" @@ -4089,16 +4036,7 @@ __metadata: languageName: node linkType: hard -"acorn@npm:^6.0.1": - version: 6.4.2 - resolution: "acorn@npm:6.4.2" - bin: - acorn: bin/acorn - checksum: ec4707ffa0f41dcd9ef67e5f0938a9e8c83f2f1ffcbd3588b07126833d2ca3a6573e094c511162ad40f658a267c6533c6dd5eedead6844d50f7d8d0be080cc55 - languageName: node - linkType: hard - -"acorn@npm:^7.1.0, acorn@npm:^7.1.1, acorn@npm:^7.4.0": +"acorn@npm:^7.1.1, acorn@npm:^7.4.0": version: 7.4.1 resolution: "acorn@npm:7.4.1" bin: @@ -4507,13 +4445,6 @@ __metadata: languageName: node linkType: hard -"array-equal@npm:^1.0.0": - version: 1.0.0 - resolution: "array-equal@npm:1.0.0" - checksum: ad82ed549385a7cacb7ed3a2be9cef73ccc0ebf371e4a30635bfc5737464f7fd5c70433e25c1bbdeec3d230d41be13e46b778e5a373300655531b4b7eff1f447 - languageName: node - linkType: hard - "array-filter@npm:^1.0.0": version: 1.0.0 resolution: "array-filter@npm:1.0.0" @@ -4850,18 +4781,6 @@ __metadata: languageName: node linkType: hard -"babel-plugin-istanbul@npm:^5.1.0": - version: 5.2.0 - resolution: "babel-plugin-istanbul@npm:5.2.0" - dependencies: - "@babel/helper-plugin-utils": ^7.0.0 - find-up: ^3.0.0 - istanbul-lib-instrument: ^3.3.0 - test-exclude: ^5.2.3 - checksum: e94429f5c2fbc6b098f8ded77addabe5d229a8c4c8d449b746396c9f05e419ef41e7582aa19f8c1674c6774f9029f686653796e15de494f63ceef40d1f60e083 - languageName: node - linkType: hard - "babel-plugin-istanbul@npm:^6.0.0": version: 6.0.0 resolution: "babel-plugin-istanbul@npm:6.0.0" @@ -6778,7 +6697,7 @@ __metadata: languageName: node linkType: hard -"cssom@npm:^0.4.1, cssom@npm:^0.4.4": +"cssom@npm:^0.4.4": version: 0.4.4 resolution: "cssom@npm:0.4.4" checksum: db81cac44219b20d76b06f51d2614cead098478d1323b2df5e4b5d25bdc3f16d8474c3d45ae28f594a0933691c774fc2102837df66ccf375e280b0728ad53c5f @@ -6792,7 +6711,7 @@ __metadata: languageName: node linkType: hard -"cssstyle@npm:^2.0.0, cssstyle@npm:^2.2.0": +"cssstyle@npm:^2.2.0": version: 2.3.0 resolution: "cssstyle@npm:2.3.0" dependencies: @@ -6849,17 +6768,6 @@ __metadata: languageName: node linkType: hard -"data-urls@npm:^1.1.0": - version: 1.1.0 - resolution: "data-urls@npm:1.1.0" - dependencies: - abab: ^2.0.0 - whatwg-mimetype: ^2.2.0 - whatwg-url: ^7.0.0 - checksum: 04d211e1e9f83bab75450487da34b124b32beacd1ad0df96e3a747b705c24c65579833a04a6ea30a528ea5b99d5247660408c513b38905bf855f2de585b9e91c - languageName: node - linkType: hard - "data-urls@npm:^2.0.0": version: 2.0.0 resolution: "data-urls@npm:2.0.0" @@ -7397,15 +7305,6 @@ __metadata: languageName: node linkType: hard -"domexception@npm:^1.0.1": - version: 1.0.1 - resolution: "domexception@npm:1.0.1" - dependencies: - webidl-conversions: ^4.0.2 - checksum: 0a678e600248b8a6f0149cb6a6ddae77d698d16a6fcf39d4228b933d5ac2b9ee657a36b2cd08ea82ec6196da756535bd30b8362f697cc9e564d969e52437fcd8 - languageName: node - linkType: hard - "domexception@npm:^2.0.1": version: 2.0.1 resolution: "domexception@npm:2.0.1" @@ -7874,7 +7773,7 @@ __metadata: languageName: node linkType: hard -"escodegen@npm:^1.11.1, escodegen@npm:^1.14.1": +"escodegen@npm:^1.14.1": version: 1.14.3 resolution: "escodegen@npm:1.14.3" dependencies: @@ -10188,15 +10087,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"html-encoding-sniffer@npm:^1.0.2": - version: 1.0.2 - resolution: "html-encoding-sniffer@npm:1.0.2" - dependencies: - whatwg-encoding: ^1.0.1 - checksum: fff1462d9845f08315b41a19b3deaeebf465b4abc44c12218ee2be42a4655dec18b8ca4ae2ea72270d564164a3092b9a72701c1c529777e378036a49c4f6bc80 - languageName: node - linkType: hard - "html-encoding-sniffer@npm:^2.0.1": version: 2.0.1 resolution: "html-encoding-sniffer@npm:2.0.1" @@ -11407,13 +11297,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"istanbul-lib-coverage@npm:^2.0.5": - version: 2.0.5 - resolution: "istanbul-lib-coverage@npm:2.0.5" - checksum: 72737ebc48c31a45ab80fb1161b4c79a7d035d3088007ec55ec7a53b8bf6ae107a8222335e018978720270d71f2036abe73e150da4733f573be32398ad6aedd1 - languageName: node - linkType: hard - "istanbul-lib-coverage@npm:^3.0.0": version: 3.0.0 resolution: "istanbul-lib-coverage@npm:3.0.0" @@ -11421,21 +11304,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"istanbul-lib-instrument@npm:^3.3.0": - version: 3.3.0 - resolution: "istanbul-lib-instrument@npm:3.3.0" - dependencies: - "@babel/generator": ^7.4.0 - "@babel/parser": ^7.4.3 - "@babel/template": ^7.4.0 - "@babel/traverse": ^7.4.3 - "@babel/types": ^7.4.0 - istanbul-lib-coverage: ^2.0.5 - semver: ^6.0.0 - checksum: d7a7dae5db459ac4365cea3ecdaf0586c79bfb850059e2fc2364c060ca6bcbbf686675d8944d6490a52f0d018781403ec5902523430e7a404d4f2b2ad82e1aef - languageName: node - linkType: hard - "istanbul-lib-instrument@npm:^4.0.0, istanbul-lib-instrument@npm:^4.0.3": version: 4.0.3 resolution: "istanbul-lib-instrument@npm:4.0.3" @@ -11633,20 +11501,6 @@ fsevents@^1.2.7: languageName: unknown linkType: soft -"jest-environment-jsdom-fifteen@npm:^1.0.2": - version: 1.0.2 - resolution: "jest-environment-jsdom-fifteen@npm:1.0.2" - dependencies: - "@jest/environment": ^24.3.0 - "@jest/fake-timers": ^24.3.0 - "@jest/types": ^24.3.0 - jest-mock: ^24.0.0 - jest-util: ^24.0.0 - jsdom: ^15.2.1 - checksum: 9951594e15af8c9ab1ef8bb2490103d115728cdd1ef423b10a2b3ee3ab8569d1c6ba5c7cf21b3424d123ef14159f2b7bcd4d32a703c8470245ea94b6137c2cc9 - languageName: node - linkType: hard - "jest-environment-jsdom@^26.5.2, jest-environment-jsdom@workspace:packages/jest-environment-jsdom": version: 0.0.0-use.local resolution: "jest-environment-jsdom@workspace:packages/jest-environment-jsdom" @@ -11718,7 +11572,7 @@ fsevents@^1.2.7: languageName: unknown linkType: soft -"jest-haste-map@npm:^24.7.1, jest-haste-map@npm:^24.9.0": +"jest-haste-map@npm:^24.7.1": version: 24.9.0 resolution: "jest-haste-map@npm:24.9.0" dependencies: @@ -11847,7 +11701,7 @@ fsevents@^1.2.7: languageName: unknown linkType: soft -"jest-mock@npm:^24.0.0, jest-mock@npm:^24.9.0": +"jest-mock@npm:^24.9.0": version: 24.9.0 resolution: "jest-mock@npm:24.9.0" dependencies: @@ -11884,13 +11738,6 @@ fsevents@^1.2.7: languageName: unknown linkType: soft -"jest-regex-util@npm:^24.9.0": - version: 24.9.0 - resolution: "jest-regex-util@npm:24.9.0" - checksum: 3a30d04bcfd779397d38c6b663c0e45492bba83908c57d3498bd682aa4dd299565edb7620e38de2225c19bc06ad8febfb268101494caee39b08c1d1493050a8d - languageName: node - linkType: hard - "jest-repl@workspace:packages/jest-repl": version: 0.0.0-use.local resolution: "jest-repl@workspace:packages/jest-repl" @@ -12321,45 +12168,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"jsdom@npm:^15.2.1": - version: 15.2.1 - resolution: "jsdom@npm:15.2.1" - dependencies: - abab: ^2.0.0 - acorn: ^7.1.0 - acorn-globals: ^4.3.2 - array-equal: ^1.0.0 - cssom: ^0.4.1 - cssstyle: ^2.0.0 - data-urls: ^1.1.0 - domexception: ^1.0.1 - escodegen: ^1.11.1 - html-encoding-sniffer: ^1.0.2 - nwsapi: ^2.2.0 - parse5: 5.1.0 - pn: ^1.1.0 - request: ^2.88.0 - request-promise-native: ^1.0.7 - saxes: ^3.1.9 - symbol-tree: ^3.2.2 - tough-cookie: ^3.0.1 - w3c-hr-time: ^1.0.1 - w3c-xmlserializer: ^1.1.2 - webidl-conversions: ^4.0.2 - whatwg-encoding: ^1.0.5 - whatwg-mimetype: ^2.3.0 - whatwg-url: ^7.0.0 - ws: ^7.0.0 - xml-name-validator: ^3.0.0 - peerDependencies: - canvas: ^2.5.0 - peerDependenciesMeta: - canvas: - optional: true - checksum: 706d227c378e88fb13528252cc83fa18ec6d9810ee081478da3c392f9d2a543a504f23aa5d811943c9ef545cdf17850fc48db8e8aa62e5b8162f2916bc2bb008 - languageName: node - linkType: hard - "jsdom@npm:^16.4.0": version: 16.4.0 resolution: "jsdom@npm:16.4.0" @@ -15300,13 +15108,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"parse5@npm:5.1.0": - version: 5.1.0 - resolution: "parse5@npm:5.1.0" - checksum: f82ab2581011704c1dd3f56fa9509904a169d06bee8d4154d40a774335ad158bc59693c6620d29093252ad120521302ff25b257bcc9aebbe12453f74659a5d65 - languageName: node - linkType: hard - "parse5@npm:5.1.1": version: 5.1.1 resolution: "parse5@npm:5.1.1" @@ -15581,13 +15382,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"pn@npm:^1.1.0": - version: 1.1.0 - resolution: "pn@npm:1.1.0" - checksum: 7df19be13c86dfab22e8484590480e49d496b270430a731be0bb40cea8a16c29e45188a7303d7c57b7140754f807877b0c10aa95400ad30a7ad4fb3f7d132381 - languageName: node - linkType: hard - "portfinder@npm:^1.0.25": version: 1.0.28 resolution: "portfinder@npm:1.0.28" @@ -16607,16 +16401,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"read-pkg-up@npm:^4.0.0": - version: 4.0.0 - resolution: "read-pkg-up@npm:4.0.0" - dependencies: - find-up: ^3.0.0 - read-pkg: ^3.0.0 - checksum: e611538e096723fa15f36960a293b26704145d646a3ddae6a206fa50ddba18f655b2901581ef06943758cebe8660bbf6b3b07bad645f2256cf2f775e64867ea5 - languageName: node - linkType: hard - "read-pkg-up@npm:^7.0.0, read-pkg-up@npm:^7.0.1": version: 7.0.1 resolution: "read-pkg-up@npm:7.0.1" @@ -16740,15 +16524,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"realpath-native@npm:^1.1.0": - version: 1.1.0 - resolution: "realpath-native@npm:1.1.0" - dependencies: - util.promisify: ^1.0.0 - checksum: 67ce6bdaf8f8dd2a85e771b7b79b74b8a47299315a0a3553947df1ab4117de80d1910a2ba856a480d9e4284172cf8d7df209117f5522475e30bb7ecdee63b75b - languageName: node - linkType: hard - "rechoir@npm:^0.6.2": version: 0.6.2 resolution: "rechoir@npm:0.6.2" @@ -17022,7 +16797,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"request-promise-native@npm:^1.0.7, request-promise-native@npm:^1.0.8": +"request-promise-native@npm:^1.0.8": version: 1.0.9 resolution: "request-promise-native@npm:1.0.9" dependencies: @@ -17319,7 +17094,7 @@ fsevents@^1.2.7: istanbul-lib-report: ^3.0.0 istanbul-reports: ^3.0.0 jest: "workspace:packages/jest" - jest-environment-jsdom-fifteen: ^1.0.2 + jest-environment-node: ^26.5.2 jest-junit: ^12.0.0 jest-runner-tsd: ^1.1.0 jest-silent-reporter: ^0.2.1 @@ -17342,7 +17117,7 @@ fsevents@^1.2.7: strip-ansi: ^6.0.0 tempy: ^0.6.0 throat: ^5.0.0 - ts-node: ^9.0.0 + ts-node: ">=9.0.0" type-fest: ^0.16.0 typescript: ^4.0.2 which: ^2.0.1 @@ -17495,15 +17270,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"saxes@npm:^3.1.9": - version: 3.1.11 - resolution: "saxes@npm:3.1.11" - dependencies: - xmlchars: ^2.1.1 - checksum: dbdbd14f903e2a18c3efb422401ad0630dd25e4ed6a52fd01e42b205508ee70e5170da4d39ab2957eca54dc2934b9c8fa6f2f90292b136bfa935db7877177a08 - languageName: node - linkType: hard - "saxes@npm:^5.0.0": version: 5.0.1 resolution: "saxes@npm:5.0.1" @@ -18634,7 +18400,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"symbol-tree@npm:^3.2.2, symbol-tree@npm:^3.2.4": +"symbol-tree@npm:^3.2.4": version: 3.2.4 resolution: "symbol-tree@npm:3.2.4" checksum: 0b9af4e5f005f9f0b9c916d91a1b654422ffa49ef09c5c4b6efa7a778f63976be9f410e57db1e9ea7576eea0631a34b69a5622674aa92a60a896ccf2afca87a7 @@ -18817,18 +18583,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"test-exclude@npm:^5.2.3": - version: 5.2.3 - resolution: "test-exclude@npm:5.2.3" - dependencies: - glob: ^7.1.3 - minimatch: ^3.0.4 - read-pkg-up: ^4.0.0 - require-main-filename: ^2.0.0 - checksum: d441f2531cf102d267de7f4ceecb4eacc8de2a6703abbab20591d0e8b30877a0e4cdcb88f88bd292f36950feda87b25e159e2fd407c275b13cce15a2a56eefaf - languageName: node - linkType: hard - "test-exclude@npm:^6.0.0": version: 6.0.0 resolution: "test-exclude@npm:6.0.0" @@ -19174,7 +18928,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"ts-node@npm:^9.0.0": +"ts-node@npm:>=9.0.0": version: 9.0.0 resolution: "ts-node@npm:9.0.0" dependencies: @@ -19742,7 +19496,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"util.promisify@npm:^1.0.0, util.promisify@npm:~1.0.0": +"util.promisify@npm:~1.0.0": version: 1.0.1 resolution: "util.promisify@npm:1.0.1" dependencies: @@ -19876,7 +19630,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"w3c-hr-time@npm:^1.0.1, w3c-hr-time@npm:^1.0.2": +"w3c-hr-time@npm:^1.0.2": version: 1.0.2 resolution: "w3c-hr-time@npm:1.0.2" dependencies: @@ -19885,17 +19639,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"w3c-xmlserializer@npm:^1.1.2": - version: 1.1.2 - resolution: "w3c-xmlserializer@npm:1.1.2" - dependencies: - domexception: ^1.0.1 - webidl-conversions: ^4.0.2 - xml-name-validator: ^3.0.0 - checksum: 9a7b5c7e32d4fa3d272a38e62595ff43169a9aa1b000d27a6b2613df759071034a8e870f7e6ebae8d0024d3056eeff1cad0fdab118ad4430c3d1cac3384dcd29 - languageName: node - linkType: hard - "w3c-xmlserializer@npm:^2.0.0": version: 2.0.0 resolution: "w3c-xmlserializer@npm:2.0.0" @@ -19974,7 +19717,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"whatwg-encoding@npm:^1.0.1, whatwg-encoding@npm:^1.0.5": +"whatwg-encoding@npm:^1.0.5": version: 1.0.5 resolution: "whatwg-encoding@npm:1.0.5" dependencies: @@ -19990,7 +19733,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"whatwg-mimetype@npm:^2.2.0, whatwg-mimetype@npm:^2.3.0": +"whatwg-mimetype@npm:^2.3.0": version: 2.3.0 resolution: "whatwg-mimetype@npm:2.3.0" checksum: 926e6ef8c7e53d158e501ce5e3c0e491d343c3c97e71b3d30451ffe4b1d6f81844c336b46a446a0b4f3fe4f327d76e3451d53ee8055344a0f5f2f35b84518011 @@ -20143,17 +19886,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"write-file-atomic@npm:2.4.1": - version: 2.4.1 - resolution: "write-file-atomic@npm:2.4.1" - dependencies: - graceful-fs: ^4.1.11 - imurmurhash: ^0.1.4 - signal-exit: ^3.0.2 - checksum: d5a00706d00cb4a13bca748d85d4d149b9a997201cdbedc9162810c9ac04188e191b1b06ca868df670db972ae9dbd4022a4eff2aec0c7dce73376dccb6d4efab - languageName: node - linkType: hard - "write-file-atomic@npm:^1.2.0": version: 1.3.4 resolution: "write-file-atomic@npm:1.3.4" @@ -20245,7 +19977,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"ws@npm:^7, ws@npm:^7.0.0, ws@npm:^7.2.3": +"ws@npm:^7, ws@npm:^7.2.3": version: 7.3.1 resolution: "ws@npm:7.3.1" peerDependencies: @@ -20323,7 +20055,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"xmlchars@npm:^2.1.1, xmlchars@npm:^2.2.0": +"xmlchars@npm:^2.2.0": version: 2.2.0 resolution: "xmlchars@npm:2.2.0" checksum: 69bbb61e8d939873c8aa7d006d082944de2eb6f12f55e53fdfc670d544e677736b59e498ece303f264bd1dc39b77557eef1c1c9bfb09eb5e1e30ac552420d81e From 7d6f815ed64782822300b4707f935ff16021d63e Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 13:43:26 +0200 Subject: [PATCH 27/38] chore: ask the user if he want's to use the Typescript configuration and create it --- .../jest-cli/src/init/generate_config_file.ts | 19 +++++++++--- packages/jest-cli/src/init/index.ts | 29 ++++++++++++------- packages/jest-cli/src/init/questions.ts | 6 ++++ 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/packages/jest-cli/src/init/generate_config_file.ts b/packages/jest-cli/src/init/generate_config_file.ts index 4d7f8a428c3f..f4265a62a2e7 100644 --- a/packages/jest-cli/src/init/generate_config_file.ts +++ b/packages/jest-cli/src/init/generate_config_file.ts @@ -35,7 +35,13 @@ const generateConfigFile = ( results: Record, generateEsm = false, ): string => { - const {coverage, coverageProvider, clearMocks, environment} = results; + const { + useTypescript, + coverage, + coverageProvider, + clearMocks, + environment, + } = results; const overrides: Record = {}; @@ -81,10 +87,15 @@ const generateConfigFile = ( } } - return ( + const configDocMessage = '// For a detailed explanation regarding each configuration property, visit:\n' + - '// https://jestjs.io/docs/en/configuration.html\n\n' + - (generateEsm ? 'export default {\n' : 'module.exports = {\n') + + '// https://jestjs.io/docs/en/configuration.html\n\n'; + + return ( + configDocMessage + + (useTypescript || generateEsm + ? 'export default {\n' + : 'module.exports = {\n') + properties.join('\n') + '};\n' ); diff --git a/packages/jest-cli/src/init/index.ts b/packages/jest-cli/src/init/index.ts index 82b0fceffa4b..5d0ddecf3460 100644 --- a/packages/jest-cli/src/init/index.ts +++ b/packages/jest-cli/src/init/index.ts @@ -11,6 +11,7 @@ import chalk = require('chalk'); import prompts = require('prompts'); import {constants} from 'jest-config'; import {tryRealpath} from 'jest-util'; +import {JEST_CONFIG_EXT_TS} from 'jest-config/build/constants'; import defaultQuestions, {testScriptQuestion} from './questions'; import {MalformedPackageJsonError, NotFoundPackageJsonError} from './errors'; import generateConfigFile from './generate_config_file'; @@ -26,6 +27,7 @@ const { } = constants; type PromptsResults = { + useTypescript: boolean; clearMocks: boolean; coverage: boolean; coverageProvider: boolean; @@ -64,16 +66,6 @@ export default async ( const existingJestConfigExt = JEST_CONFIG_EXT_ORDER.find(ext => fs.existsSync(path.join(rootDir, getConfigFilename(ext))), ); - const jestConfigPath = existingJestConfigExt - ? getConfigFilename(existingJestConfigExt) - : path.join( - rootDir, - getConfigFilename( - projectPackageJson.type === 'module' - ? JEST_CONFIG_EXT_MJS - : JEST_CONFIG_EXT_JS, - ), - ); if (hasJestProperty || existingJestConfigExt) { const result: {continue: boolean} = await prompts({ @@ -122,6 +114,23 @@ export default async ( return; } + // Determine if Jest should use JS or TS for the config file + const jestConfigFileExt = results.useTypescript + ? JEST_CONFIG_EXT_TS + : JEST_CONFIG_EXT_JS; + + // Determine Jest config path + const jestConfigPath = existingJestConfigExt + ? getConfigFilename(existingJestConfigExt) + : path.join( + rootDir, + getConfigFilename( + projectPackageJson.type === 'module' + ? JEST_CONFIG_EXT_MJS + : jestConfigFileExt, + ), + ); + const shouldModifyScripts = results.scripts; if (shouldModifyScripts || hasJestProperty) { diff --git a/packages/jest-cli/src/init/questions.ts b/packages/jest-cli/src/init/questions.ts index ae8671848b64..235411dc441a 100644 --- a/packages/jest-cli/src/init/questions.ts +++ b/packages/jest-cli/src/init/questions.ts @@ -8,6 +8,12 @@ import type {PromptObject} from 'prompts'; const defaultQuestions: Array = [ + { + initial: false, + message: 'Would you like to use Typescript for the configuration file?', + name: 'useTypescript', + type: 'confirm', + }, { choices: [ {title: 'node', value: 'node'}, From 9bf4a0cda5e54c2e0d362478a30255b2f4578b26 Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 14:16:02 +0200 Subject: [PATCH 28/38] chore: use the local 'jest-environment-node' --- package.json | 2 +- yarn.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index e2f13e536238..2460d8ec4b4d 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "istanbul-lib-report": "^3.0.0", "istanbul-reports": "^3.0.0", "jest": "workspace:packages/jest", - "jest-environment-node": "^26.5.2", + "jest-environment-node": "workspace:packages/jest-environment-node", "jest-junit": "^12.0.0", "jest-runner-tsd": "^1.1.0", "jest-silent-reporter": "^0.2.1", diff --git a/yarn.lock b/yarn.lock index 2fd39aa6c257..bf0e144fff19 100644 --- a/yarn.lock +++ b/yarn.lock @@ -17094,7 +17094,7 @@ fsevents@^1.2.7: istanbul-lib-report: ^3.0.0 istanbul-reports: ^3.0.0 jest: "workspace:packages/jest" - jest-environment-node: ^26.5.2 + jest-environment-node: "workspace:packages/jest-environment-node" jest-junit: ^12.0.0 jest-runner-tsd: ^1.1.0 jest-silent-reporter: ^0.2.1 From a69c677f60d3c395037ff87ae8ab7fd1d80d5108 Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 14:17:27 +0200 Subject: [PATCH 29/38] fix: use `jest-util` instead of `jest-util/build` --- packages/jest-cli/src/init/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-cli/src/init/index.ts b/packages/jest-cli/src/init/index.ts index 5d0ddecf3460..90d7e7d254ca 100644 --- a/packages/jest-cli/src/init/index.ts +++ b/packages/jest-cli/src/init/index.ts @@ -11,7 +11,6 @@ import chalk = require('chalk'); import prompts = require('prompts'); import {constants} from 'jest-config'; import {tryRealpath} from 'jest-util'; -import {JEST_CONFIG_EXT_TS} from 'jest-config/build/constants'; import defaultQuestions, {testScriptQuestion} from './questions'; import {MalformedPackageJsonError, NotFoundPackageJsonError} from './errors'; import generateConfigFile from './generate_config_file'; @@ -22,6 +21,7 @@ const { JEST_CONFIG_BASE_NAME, JEST_CONFIG_EXT_MJS, JEST_CONFIG_EXT_JS, + JEST_CONFIG_EXT_TS, JEST_CONFIG_EXT_ORDER, PACKAGE_JSON, } = constants; From a3b7210d6fa2eb6ec658d8f57f2e34e4f106bd1c Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 14:21:52 +0200 Subject: [PATCH 30/38] chore: add type annotation for the 'ts-node' register variable --- packages/jest-config/src/readConfigFileAndSetRootDir.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 543ae17834bd..47f1d6823efc 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -10,6 +10,7 @@ import {pathToFileURL} from 'url'; import * as fs from 'graceful-fs'; import type {Config} from '@jest/types'; import {interopRequireDefault} from 'jest-util'; +import type {Register} from 'ts-node'; // @ts-expect-error: vendored import jsonlint from './vendor/jsonlint'; import { @@ -102,7 +103,7 @@ export default async function readConfigFileAndSetRootDir( const loadTSConfigFile = async ( configPath: Config.Path, ): Promise => { - let registerer; + let registerer: Register; // Register TypeScript compiler instance try { From 8b678d9ee3993f76137dab197d4a8dd8c8643301 Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 15:30:43 +0200 Subject: [PATCH 31/38] chore: make Typescript override module --- packages/jest-cli/src/init/index.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/jest-cli/src/init/index.ts b/packages/jest-cli/src/init/index.ts index 90d7e7d254ca..cdc5a5298ade 100644 --- a/packages/jest-cli/src/init/index.ts +++ b/packages/jest-cli/src/init/index.ts @@ -117,19 +117,14 @@ export default async ( // Determine if Jest should use JS or TS for the config file const jestConfigFileExt = results.useTypescript ? JEST_CONFIG_EXT_TS + : projectPackageJson.type === 'module' + ? JEST_CONFIG_EXT_MJS : JEST_CONFIG_EXT_JS; // Determine Jest config path const jestConfigPath = existingJestConfigExt ? getConfigFilename(existingJestConfigExt) - : path.join( - rootDir, - getConfigFilename( - projectPackageJson.type === 'module' - ? JEST_CONFIG_EXT_MJS - : jestConfigFileExt, - ), - ); + : path.join(rootDir, getConfigFilename(jestConfigFileExt)); const shouldModifyScripts = results.scripts; From 729f6dc80d8838e91a4843131d065a3757c83530 Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 16:59:01 +0200 Subject: [PATCH 32/38] chore: 'jest-environment-node' was removed from the root `package.json` --- package.json | 1 - yarn.lock | 1 - 2 files changed, 2 deletions(-) diff --git a/package.json b/package.json index 2460d8ec4b4d..dae436a03715 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,6 @@ "istanbul-lib-report": "^3.0.0", "istanbul-reports": "^3.0.0", "jest": "workspace:packages/jest", - "jest-environment-node": "workspace:packages/jest-environment-node", "jest-junit": "^12.0.0", "jest-runner-tsd": "^1.1.0", "jest-silent-reporter": "^0.2.1", diff --git a/yarn.lock b/yarn.lock index bf0e144fff19..faa458d233a3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -17094,7 +17094,6 @@ fsevents@^1.2.7: istanbul-lib-report: ^3.0.0 istanbul-reports: ^3.0.0 jest: "workspace:packages/jest" - jest-environment-node: "workspace:packages/jest-environment-node" jest-junit: ^12.0.0 jest-runner-tsd: ^1.1.0 jest-silent-reporter: ^0.2.1 From 7c02d1c651e83f1c264d091cfbf53f583f530cff Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 17:44:20 +0200 Subject: [PATCH 33/38] chore: added `ts-node` as dev dependency in the `jest-config` --- package.json | 2 +- packages/jest-config/package.json | 6 ++++-- yarn.lock | 12 +++++++----- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index dae436a03715..899223832694 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "strip-ansi": "^6.0.0", "tempy": "^0.6.0", "throat": "^5.0.0", - "ts-node": ">=9.0.0", + "ts-node": "^9.0.0", "type-fest": "^0.16.0", "typescript": "^4.0.2", "which": "^2.0.1" diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index fc8acfb6955e..7a89d8e73b7b 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -10,7 +10,7 @@ "main": "build/index.js", "types": "build/index.d.ts", "peerDependencies": { - "ts-node": "^9.0.0" + "ts-node": ">=9.0.0" }, "peerDependenciesMeta": { "ts-node": { @@ -35,7 +35,9 @@ "jest-util": "^26.5.2", "jest-validate": "^26.5.3", "micromatch": "^4.0.2", - "pretty-format": "^26.5.2" + "pretty-format": "^26.5.2", + "ts-node": "^9.0.0", + "typescript": "^4.0.3" }, "devDependencies": { "@types/babel__core": "^7.0.4", diff --git a/yarn.lock b/yarn.lock index faa458d233a3..e5177dc89236 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11459,8 +11459,10 @@ fsevents@^1.2.7: jest-validate: ^26.5.3 micromatch: ^4.0.2 pretty-format: ^26.5.2 - peerDependencies: ts-node: ^9.0.0 + typescript: ^4.0.3 + peerDependencies: + ts-node: ">=9.0.0" peerDependenciesMeta: ts-node: optional: true @@ -17116,7 +17118,7 @@ fsevents@^1.2.7: strip-ansi: ^6.0.0 tempy: ^0.6.0 throat: ^5.0.0 - ts-node: ">=9.0.0" + ts-node: ^9.0.0 type-fest: ^0.16.0 typescript: ^4.0.2 which: ^2.0.1 @@ -18927,7 +18929,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"ts-node@npm:>=9.0.0": +"ts-node@npm:^9.0.0": version: 9.0.0 resolution: "ts-node@npm:9.0.0" dependencies: @@ -19100,7 +19102,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"typescript@*, typescript@^4.0.2": +"typescript@*, typescript@^4.0.2, typescript@^4.0.3": version: 4.0.3 resolution: "typescript@npm:4.0.3" bin: @@ -19110,7 +19112,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"typescript@patch:typescript@*#builtin, typescript@patch:typescript@^4.0.2#builtin": +"typescript@patch:typescript@*#builtin, typescript@patch:typescript@^4.0.2#builtin, typescript@patch:typescript@^4.0.3#builtin": version: 4.0.3 resolution: "typescript@patch:typescript@npm%3A4.0.3#builtin::version=4.0.3&hash=5bf698" bin: From 9aa8d53d495d2d8f68af0920dbe07eb6c9437992 Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 18:48:45 +0200 Subject: [PATCH 34/38] chores: - added comment in the generated '.ts' which point to the doc for more detail on type checking - fix tests to accommodate the new config comment - tests added to check the '.ts' file --- .../__tests__/__snapshots__/init.test.js.snap | 67 ++++++++++++++++++- .../package.json | 1 + .../jest-cli/src/init/__tests__/init.test.js | 30 ++++++++- .../jest-cli/src/init/generate_config_file.ts | 8 ++- 4 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 packages/jest-cli/src/init/__tests__/fixtures/test_generated_jest_config_ts/package.json diff --git a/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap b/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap index 0e8040cfade6..e90ed19aae3e 100644 --- a/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap +++ b/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap @@ -54,9 +54,72 @@ Object { } `; +exports[`init project using jest.config.ts ask the user whether he wants to use Typescript or not user answered with "Yes" 1`] = ` +Array [ + Object { + "initial": true, + "message": "Would you like to use Jest when running \\"test\\" script in \\"package.json\\"?", + "name": "scripts", + "type": "confirm", + }, + Object { + "initial": false, + "message": "Would you like to use Typescript for the configuration file?", + "name": "useTypescript", + "type": "confirm", + }, + Object { + "choices": Array [ + Object { + "title": "node", + "value": "node", + }, + Object { + "title": "jsdom (browser-like)", + "value": "jsdom", + }, + ], + "initial": 0, + "message": "Choose the test environment that will be used for testing", + "name": "environment", + "type": "select", + }, + Object { + "initial": false, + "message": "Do you want Jest to add coverage reports?", + "name": "coverage", + "type": "confirm", + }, + Object { + "choices": Array [ + Object { + "title": "v8", + "value": "v8", + }, + Object { + "title": "babel", + "value": "babel", + }, + ], + "initial": 0, + "message": "Which provider should be used to instrument code for coverage?", + "name": "coverageProvider", + "type": "select", + }, + Object { + "initial": false, + "message": "Automatically clear mock calls and instances between every test?", + "name": "clearMocks", + "type": "confirm", + }, +] +`; + exports[`init project with package.json and no jest config all questions answered with answer: "No" should return the default configuration (an empty config) 1`] = ` -"// For a detailed explanation regarding each configuration property, visit: -// https://jestjs.io/docs/en/configuration.html +"/* + * For a detailed explanation regarding each configuration property, visit: + * https://jestjs.io/docs/en/configuration.html + */ module.exports = { // All imported modules in your tests should be mocked automatically diff --git a/packages/jest-cli/src/init/__tests__/fixtures/test_generated_jest_config_ts/package.json b/packages/jest-cli/src/init/__tests__/fixtures/test_generated_jest_config_ts/package.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/packages/jest-cli/src/init/__tests__/fixtures/test_generated_jest_config_ts/package.json @@ -0,0 +1 @@ +{} diff --git a/packages/jest-cli/src/init/__tests__/init.test.js b/packages/jest-cli/src/init/__tests__/init.test.js index e8b1b2fe1265..e3da47e6b8a9 100644 --- a/packages/jest-cli/src/init/__tests__/init.test.js +++ b/packages/jest-cli/src/init/__tests__/init.test.js @@ -66,7 +66,7 @@ describe('init', () => { expect(writtenJestConfigFilename.endsWith('.mjs')).toBe(true); expect(typeof writtenJestConfig).toBe('string'); - expect(writtenJestConfig.split('\n')[3]).toBe('export default {'); + expect(writtenJestConfig.split('\n')[5]).toBe('export default {'); }); }); @@ -193,6 +193,34 @@ describe('init', () => { }, ); + describe('project using jest.config.ts', () => { + describe('ask the user whether he wants to use Typescript or not', () => { + it('user answered with "Yes"', async () => { + prompts.mockReturnValueOnce({useTypescript: true}); + + await init(resolveFromFixture('test_generated_jest_config_ts')); + + expect(prompts.mock.calls[0][0]).toMatchSnapshot(); + + const jestConfigFileName = fs.writeFileSync.mock.calls[0][0]; + const writtenJestConfig = fs.writeFileSync.mock.calls[0][1]; + + expect(jestConfigFileName).toContain('/jest.config.ts'); + expect(writtenJestConfig.split('\n')[5]).toBe('export default {'); + }); + + it('user answered with "No"', async () => { + prompts.mockReturnValueOnce({useTypescript: true}); + + await init(resolveFromFixture('test_generated_jest_config_ts')); + + const jestConfigFileName = fs.writeFileSync.mock.calls[0][0]; + + expect(jestConfigFileName).not.toBe('jest.config.ts'); + }); + }); + }); + describe('has jest config in package.json', () => { it('should ask the user whether to override config or not', async () => { prompts.mockReturnValueOnce({continue: true}).mockReturnValueOnce({}); diff --git a/packages/jest-cli/src/init/generate_config_file.ts b/packages/jest-cli/src/init/generate_config_file.ts index f4265a62a2e7..5ef32e80afb3 100644 --- a/packages/jest-cli/src/init/generate_config_file.ts +++ b/packages/jest-cli/src/init/generate_config_file.ts @@ -88,8 +88,12 @@ const generateConfigFile = ( } const configDocMessage = - '// For a detailed explanation regarding each configuration property, visit:\n' + - '// https://jestjs.io/docs/en/configuration.html\n\n'; + '/*\n' + + ' * For a detailed explanation regarding each configuration property' + + (useTypescript ? ' and type check' : '') + + ', visit:\n' + + ' * https://jestjs.io/docs/en/configuration.html\n' + + ' */\n\n'; return ( configDocMessage + From 55169aa0855fd91df06ac87f8d331ac101458779 Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 21:14:34 +0200 Subject: [PATCH 35/38] chore: move `typescript` and `ts-node` from 'dependencies' to 'devDependencies' --- packages/jest-config/package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index 7a89d8e73b7b..549658f47ce6 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -35,15 +35,15 @@ "jest-util": "^26.5.2", "jest-validate": "^26.5.3", "micromatch": "^4.0.2", - "pretty-format": "^26.5.2", - "ts-node": "^9.0.0", - "typescript": "^4.0.3" + "pretty-format": "^26.5.2" }, "devDependencies": { "@types/babel__core": "^7.0.4", "@types/glob": "^7.1.1", "@types/graceful-fs": "^4.1.3", - "@types/micromatch": "^4.0.0" + "@types/micromatch": "^4.0.0", + "ts-node": "^9.0.0", + "typescript": "^4.0.3" }, "engines": { "node": ">= 10.14.2" From b6149953ea8bdaf36e5ebe7417cfd191d0e8b1ca Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 21:15:25 +0200 Subject: [PATCH 36/38] fix: jest.config.ts test > user answered with "No" --- packages/jest-cli/src/init/__tests__/init.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-cli/src/init/__tests__/init.test.js b/packages/jest-cli/src/init/__tests__/init.test.js index e3da47e6b8a9..d8d820393c51 100644 --- a/packages/jest-cli/src/init/__tests__/init.test.js +++ b/packages/jest-cli/src/init/__tests__/init.test.js @@ -210,13 +210,13 @@ describe('init', () => { }); it('user answered with "No"', async () => { - prompts.mockReturnValueOnce({useTypescript: true}); + prompts.mockReturnValueOnce({useTypescript: false}); await init(resolveFromFixture('test_generated_jest_config_ts')); const jestConfigFileName = fs.writeFileSync.mock.calls[0][0]; - expect(jestConfigFileName).not.toBe('jest.config.ts'); + expect(jestConfigFileName).not.toContain('jest.config.ts'); }); }); }); From 3fb9994b643a8ca89229ebe39f6e9ae56681676b Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 21:20:58 +0200 Subject: [PATCH 37/38] chore: use multiline string instead of manual newlines --- .../jest-cli/src/init/generate_config_file.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/jest-cli/src/init/generate_config_file.ts b/packages/jest-cli/src/init/generate_config_file.ts index 5ef32e80afb3..78c6134c89cc 100644 --- a/packages/jest-cli/src/init/generate_config_file.ts +++ b/packages/jest-cli/src/init/generate_config_file.ts @@ -87,13 +87,14 @@ const generateConfigFile = ( } } - const configDocMessage = - '/*\n' + - ' * For a detailed explanation regarding each configuration property' + - (useTypescript ? ' and type check' : '') + - ', visit:\n' + - ' * https://jestjs.io/docs/en/configuration.html\n' + - ' */\n\n'; + const configDocMessage = `/* + * For a detailed explanation regarding each configuration property${ + useTypescript ? ' and type check' : '' + }, visit: + * https://jestjs.io/docs/en/configuration.html + */ + +`; return ( configDocMessage + From 6336d3601fc37c8cc828a7d593f17aa937c385f6 Mon Sep 17 00:00:00 2001 From: Gamote Date: Mon, 12 Oct 2020 22:07:48 +0200 Subject: [PATCH 38/38] chore: better naming for the 'configDocMessage': 'configHeaderMessage' --- packages/jest-cli/src/init/generate_config_file.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-cli/src/init/generate_config_file.ts b/packages/jest-cli/src/init/generate_config_file.ts index 78c6134c89cc..a7fdff6e277a 100644 --- a/packages/jest-cli/src/init/generate_config_file.ts +++ b/packages/jest-cli/src/init/generate_config_file.ts @@ -87,7 +87,7 @@ const generateConfigFile = ( } } - const configDocMessage = `/* + const configHeaderMessage = `/* * For a detailed explanation regarding each configuration property${ useTypescript ? ' and type check' : '' }, visit: @@ -97,7 +97,7 @@ const generateConfigFile = ( `; return ( - configDocMessage + + configHeaderMessage + (useTypescript || generateEsm ? 'export default {\n' : 'module.exports = {\n') +