From 7f17fe207955111c15758abb282d856af576dfd7 Mon Sep 17 00:00:00 2001 From: Rajat Date: Mon, 15 Jul 2024 00:57:24 +0530 Subject: [PATCH 01/15] load ts configs via loader --- e2e/__tests__/readInitialOptions.test.ts | 62 +---- .../ts-esbuild-register-config/jest.config.ts | 16 ++ .../jest.config.ts | 10 +- packages/jest-config/package.json | 7 + .../src/readConfigFileAndSetRootDir.ts | 76 ++++-- packages/jest-config/tsconfig.json | 1 + yarn.lock | 250 +++++++++++++++++- 7 files changed, 351 insertions(+), 71 deletions(-) create mode 100644 e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts rename e2e/read-initial-options/{ts-config => ts-node-config}/jest.config.ts (52%) diff --git a/e2e/__tests__/readInitialOptions.test.ts b/e2e/__tests__/readInitialOptions.test.ts index d5776913f5e0..d20c6b853560 100644 --- a/e2e/__tests__/readInitialOptions.test.ts +++ b/e2e/__tests__/readInitialOptions.test.ts @@ -46,61 +46,25 @@ describe('readInitialOptions', () => { expect(config).toEqual({jestConfig: 'jest.config.js', rootDir}); expect(configPath).toEqual(configFile); }); - test('should read a jest.config.js file', async () => { - const configFile = resolveFixture('js-config', 'jest.config.js'); - const rootDir = resolveFixture('js-config'); - const {config, configPath} = await proxyReadInitialOptions(undefined, { - cwd: rootDir, - }); - expect(config).toEqual({jestConfig: 'jest.config.js', rootDir}); - expect(configPath).toEqual(configFile); - }); - test('should read a package.json file', async () => { - const configFile = resolveFixture('pkg-config', 'package.json'); - const rootDir = resolveFixture('pkg-config'); + test.each([ + ['js-config', 'jest.config.js'], + ['pkg-config', 'package.json'], + ['ts-node-config', 'jest.config.ts'], + ['ts-esbuild-register-config', 'jest.config.ts'], + ['mjs-config', 'jest.config.mjs'], + ['json-config', 'jest.config.json'], + ['async-config', 'jest.config.js'], + ])('should read %s/%s file', async (directory: string, filename: string) => { + const configFile = resolveFixture(directory, filename); + const rootDir = resolveFixture(directory); const {config, configPath} = await proxyReadInitialOptions(undefined, { cwd: rootDir, }); - expect(config).toEqual({jestConfig: 'package.json', rootDir}); - expect(configPath).toEqual(configFile); - }); - test('should read a jest.config.ts file', async () => { - const configFile = resolveFixture('ts-config', 'jest.config.ts'); - const rootDir = resolveFixture('ts-config'); - const {config, configPath} = await proxyReadInitialOptions(undefined, { - cwd: rootDir, - }); - expect(config).toEqual({jestConfig: 'jest.config.ts', rootDir}); - expect(configPath).toEqual(configFile); - }); - test('should read a jest.config.mjs file', async () => { - const configFile = resolveFixture('mjs-config', 'jest.config.mjs'); - const rootDir = resolveFixture('mjs-config'); - const {config, configPath} = await proxyReadInitialOptions(undefined, { - cwd: rootDir, - }); - expect(config).toEqual({jestConfig: 'jest.config.mjs', rootDir}); - expect(configPath).toEqual(configFile); - }); - test('should read a jest.config.json file', async () => { - const configFile = resolveFixture('json-config', 'jest.config.json'); - const rootDir = resolveFixture('json-config'); - const {config, configPath} = await proxyReadInitialOptions(undefined, { - cwd: rootDir, - }); - expect(config).toEqual({jestConfig: 'jest.config.json', rootDir}); - expect(configPath).toEqual(configFile); - }); - test('should read a jest config exporting an async function', async () => { - const configFile = resolveFixture('async-config', 'jest.config.js'); - const rootDir = resolveFixture('async-config'); - const {config, configPath} = await proxyReadInitialOptions(undefined, { - cwd: rootDir, - }); - expect(config).toEqual({jestConfig: 'async-config', rootDir}); + expect(config).toEqual({jestConfig: filename, rootDir}); expect(configPath).toEqual(configFile); }); + test('should be able to skip config reading, instead read from cwd', async () => { const expectedConfigFile = resolveFixture( 'json-config', diff --git a/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts b/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts new file mode 100644 index 000000000000..64a432bf78fd --- /dev/null +++ b/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts @@ -0,0 +1,16 @@ +/** + * 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. + * + * @jest-config-loader esbuild-register + */ +interface Config { + jestConfig: string; + } + + export default { + jestConfig: 'jest.config.ts', + } as Config; + \ No newline at end of file diff --git a/e2e/read-initial-options/ts-config/jest.config.ts b/e2e/read-initial-options/ts-node-config/jest.config.ts similarity index 52% rename from e2e/read-initial-options/ts-config/jest.config.ts rename to e2e/read-initial-options/ts-node-config/jest.config.ts index ab855d363864..93976bf8a4bb 100644 --- a/e2e/read-initial-options/ts-config/jest.config.ts +++ b/e2e/read-initial-options/ts-node-config/jest.config.ts @@ -1,9 +1,15 @@ /** - * Copyright (c) Meta Platforms, Inc. and affiliates. + * 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. + * + * @jest-config-loader ts-node */ +interface Config { + jestConfig: string; +} + export default { jestConfig: 'jest.config.ts', -}; +} as Config; diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index 447995cc2340..caafc091fb03 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -20,12 +20,16 @@ }, "peerDependencies": { "@types/node": "*", + "esbuild-register": ">=3.1.0", "ts-node": ">=9.0.0" }, "peerDependenciesMeta": { "@types/node": { "optional": true }, + "esbuild-register": { + "optional": true + }, "ts-node": { "optional": true } @@ -42,6 +46,7 @@ "glob": "^10.3.10", "graceful-fs": "^4.2.9", "jest-circus": "workspace:*", + "jest-docblock": "workspace:^", "jest-environment-node": "workspace:*", "jest-get-type": "workspace:*", "jest-regex-util": "workspace:*", @@ -59,6 +64,8 @@ "@types/graceful-fs": "^4.1.3", "@types/micromatch": "^4.0.7", "@types/parse-json": "^4.0.0", + "esbuild": "^0.15.0", + "esbuild-register": "^3.1.0", "semver": "^7.5.3", "ts-node": "^10.5.0", "typescript": "^5.0.4" diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index bd9d7bae0d5b..5335b691d266 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -10,8 +10,8 @@ import {isNativeError} from 'util/types'; import * as fs from 'graceful-fs'; import parseJson = require('parse-json'); import stripJsonComments = require('strip-json-comments'); -import type {Service} from 'ts-node'; -import type {Config} from '@jest/types'; +import {extract, parse} from 'jest-docblock'; +import {Config} from '@jest/types'; import {interopRequireDefault, requireOrImportModule} from 'jest-util'; import { JEST_CONFIG_EXT_CTS, @@ -20,6 +20,10 @@ import { PACKAGE_JSON, } from './constants'; +interface TsLoader { + enabled: (bool: boolean) => void; +} +type TsLoaderModule = 'ts-node' | 'esbuild-register'; // 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 use `ts-node` to transpile & require it @@ -89,8 +93,24 @@ const loadTSConfigFile = async ( configPath: string, ): Promise => { // Get registered TypeScript compiler instance - const registeredCompiler = await getRegisteredCompiler(); + const docblockPragmas = parse(extract(fs.readFileSync(configPath, 'utf8'))); + const tsLoader = docblockPragmas['jest-config-loader']; + if(tsLoader===undefined){ + throw new Error( + 'Jest: loader is required for the TypeScript configuration files.' + ); + } + if (Array.isArray(tsLoader)) { + throw new Error( + `Jest: You can only define a single loader through docblocks, got "${tsLoader.join( + ', ', + )}"`, + ); + } + const registeredCompiler = await getRegisteredCompiler( + tsLoader as TsLoaderModule, + ); registeredCompiler.enabled(true); let configObject = interopRequireDefault(require(configPath)).default; @@ -105,36 +125,54 @@ const loadTSConfigFile = async ( return configObject; }; -let registeredCompilerPromise: Promise; +let registeredCompilerPromise: Promise; -function getRegisteredCompiler() { +function getRegisteredCompiler(loader: TsLoaderModule) { // Cache the promise to avoid multiple registrations - registeredCompilerPromise = registeredCompilerPromise ?? registerTsNode(); + registeredCompilerPromise = registeredCompilerPromise ?? registerTsLoader(loader); return registeredCompilerPromise; } -async function registerTsNode(): Promise { +async function registerTsLoader(loader: TsLoaderModule): Promise { try { // Register TypeScript compiler instance - const tsNode = await import(/* webpackIgnore: true */ 'ts-node'); - return tsNode.register({ - compilerOptions: { - module: 'CommonJS', - moduleResolution: 'Node10', - }, - moduleTypes: { - '**': 'cjs', - }, - transpileOnly: + if (loader === 'ts-node') { + const tsLoader = await import(/* webpackIgnore: true */'ts-node'); + return tsLoader.register({ + compilerOptions: { + module: 'CommonJS', + }, + moduleTypes: { + '**': 'cjs', + }, + transpileOnly: process.env.JEST_CONFIG_TRANSPILE_ONLY?.toLowerCase() === 'true', - }); + }); + } else if (loader === 'esbuild-register') { + const tsLoader = await import(/* webpackIgnore: true */'esbuild-register/dist/node'); + let instance: {unregister: () => void} | undefined; + return { + enabled: (bool: boolean) => { + if (bool) { + instance = tsLoader.register({ + target: `node${process.version.slice(1)}`, + }); + } else { + instance?.unregister(); + } + }, + }; + } + throw new Error( + `Jest: '${loader}' is not a valid TypeScript configuration loader.`, + ); } catch (error) { if ( isNativeError(error) && (error as NodeJS.ErrnoException).code === 'ERR_MODULE_NOT_FOUND' ) { throw new Error( - `Jest: 'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${error.message}`, + `Jest: '${loader}' is required for the TypeScript configuration files. Make sure it is installed\nError: ${error.message}`, ); } diff --git a/packages/jest-config/tsconfig.json b/packages/jest-config/tsconfig.json index 2e008d38e257..0989f49aadba 100644 --- a/packages/jest-config/tsconfig.json +++ b/packages/jest-config/tsconfig.json @@ -10,6 +10,7 @@ // jest-test-sequencer, but that is just `require.resolve`d, so no real use // for their types "references": [ + {"path": "../jest-docblock"}, {"path": "../jest-environment-node"}, {"path": "../jest-get-type"}, {"path": "../jest-pattern"}, diff --git a/yarn.lock b/yarn.lock index c75b24c3adf2..a1955b0ffb3d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2686,6 +2686,20 @@ __metadata: languageName: node linkType: hard +"@esbuild/android-arm@npm:0.15.18": + version: 0.15.18 + resolution: "@esbuild/android-arm@npm:0.15.18" + conditions: os=android & cpu=arm + languageName: node + linkType: hard + +"@esbuild/linux-loong64@npm:0.15.18": + version: 0.15.18 + resolution: "@esbuild/linux-loong64@npm:0.15.18" + conditions: os=linux & cpu=loong64 + languageName: node + linkType: hard + "@eslint-community/eslint-utils@npm:^4.2.0, @eslint-community/eslint-utils@npm:^4.4.0": version: 4.4.0 resolution: "@eslint-community/eslint-utils@npm:4.4.0" @@ -9479,6 +9493,234 @@ __metadata: languageName: node linkType: hard +"esbuild-android-64@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-android-64@npm:0.15.18" + conditions: os=android & cpu=x64 + languageName: node + linkType: hard + +"esbuild-android-arm64@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-android-arm64@npm:0.15.18" + conditions: os=android & cpu=arm64 + languageName: node + linkType: hard + +"esbuild-darwin-64@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-darwin-64@npm:0.15.18" + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + +"esbuild-darwin-arm64@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-darwin-arm64@npm:0.15.18" + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + +"esbuild-freebsd-64@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-freebsd-64@npm:0.15.18" + conditions: os=freebsd & cpu=x64 + languageName: node + linkType: hard + +"esbuild-freebsd-arm64@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-freebsd-arm64@npm:0.15.18" + conditions: os=freebsd & cpu=arm64 + languageName: node + linkType: hard + +"esbuild-linux-32@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-linux-32@npm:0.15.18" + conditions: os=linux & cpu=ia32 + languageName: node + linkType: hard + +"esbuild-linux-64@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-linux-64@npm:0.15.18" + conditions: os=linux & cpu=x64 + languageName: node + linkType: hard + +"esbuild-linux-arm64@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-linux-arm64@npm:0.15.18" + conditions: os=linux & cpu=arm64 + languageName: node + linkType: hard + +"esbuild-linux-arm@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-linux-arm@npm:0.15.18" + conditions: os=linux & cpu=arm + languageName: node + linkType: hard + +"esbuild-linux-mips64le@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-linux-mips64le@npm:0.15.18" + conditions: os=linux & cpu=mips64el + languageName: node + linkType: hard + +"esbuild-linux-ppc64le@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-linux-ppc64le@npm:0.15.18" + conditions: os=linux & cpu=ppc64 + languageName: node + linkType: hard + +"esbuild-linux-riscv64@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-linux-riscv64@npm:0.15.18" + conditions: os=linux & cpu=riscv64 + languageName: node + linkType: hard + +"esbuild-linux-s390x@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-linux-s390x@npm:0.15.18" + conditions: os=linux & cpu=s390x + languageName: node + linkType: hard + +"esbuild-netbsd-64@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-netbsd-64@npm:0.15.18" + conditions: os=netbsd & cpu=x64 + languageName: node + linkType: hard + +"esbuild-openbsd-64@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-openbsd-64@npm:0.15.18" + conditions: os=openbsd & cpu=x64 + languageName: node + linkType: hard + +"esbuild-register@npm:^3.1.0": + version: 3.5.0 + resolution: "esbuild-register@npm:3.5.0" + dependencies: + debug: ^4.3.4 + peerDependencies: + esbuild: ">=0.12 <1" + checksum: f4307753c9672a2c901d04a1165031594a854f0a4c6f4c1db08aa393b68a193d38f2df483dc8ca0513e89f7b8998415e7e26fb9830989fb8cdccc5fb5f181c6b + languageName: node + linkType: hard + +"esbuild-sunos-64@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-sunos-64@npm:0.15.18" + conditions: os=sunos & cpu=x64 + languageName: node + linkType: hard + +"esbuild-windows-32@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-windows-32@npm:0.15.18" + conditions: os=win32 & cpu=ia32 + languageName: node + linkType: hard + +"esbuild-windows-64@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-windows-64@npm:0.15.18" + conditions: os=win32 & cpu=x64 + languageName: node + linkType: hard + +"esbuild-windows-arm64@npm:0.15.18": + version: 0.15.18 + resolution: "esbuild-windows-arm64@npm:0.15.18" + conditions: os=win32 & cpu=arm64 + languageName: node + linkType: hard + +"esbuild@npm:^0.15.0": + version: 0.15.18 + resolution: "esbuild@npm:0.15.18" + dependencies: + "@esbuild/android-arm": 0.15.18 + "@esbuild/linux-loong64": 0.15.18 + esbuild-android-64: 0.15.18 + esbuild-android-arm64: 0.15.18 + esbuild-darwin-64: 0.15.18 + esbuild-darwin-arm64: 0.15.18 + esbuild-freebsd-64: 0.15.18 + esbuild-freebsd-arm64: 0.15.18 + esbuild-linux-32: 0.15.18 + esbuild-linux-64: 0.15.18 + esbuild-linux-arm: 0.15.18 + esbuild-linux-arm64: 0.15.18 + esbuild-linux-mips64le: 0.15.18 + esbuild-linux-ppc64le: 0.15.18 + esbuild-linux-riscv64: 0.15.18 + esbuild-linux-s390x: 0.15.18 + esbuild-netbsd-64: 0.15.18 + esbuild-openbsd-64: 0.15.18 + esbuild-sunos-64: 0.15.18 + esbuild-windows-32: 0.15.18 + esbuild-windows-64: 0.15.18 + esbuild-windows-arm64: 0.15.18 + dependenciesMeta: + "@esbuild/android-arm": + optional: true + "@esbuild/linux-loong64": + optional: true + esbuild-android-64: + optional: true + esbuild-android-arm64: + optional: true + esbuild-darwin-64: + optional: true + esbuild-darwin-arm64: + optional: true + esbuild-freebsd-64: + optional: true + esbuild-freebsd-arm64: + optional: true + esbuild-linux-32: + optional: true + esbuild-linux-64: + optional: true + esbuild-linux-arm: + optional: true + esbuild-linux-arm64: + optional: true + esbuild-linux-mips64le: + optional: true + esbuild-linux-ppc64le: + optional: true + esbuild-linux-riscv64: + optional: true + esbuild-linux-s390x: + optional: true + esbuild-netbsd-64: + optional: true + esbuild-openbsd-64: + optional: true + esbuild-sunos-64: + optional: true + esbuild-windows-32: + optional: true + esbuild-windows-64: + optional: true + esbuild-windows-arm64: + optional: true + bin: + esbuild: bin/esbuild + checksum: ec12682b2cb2d4f0669d0e555028b87a9284ca7f6a1b26e35e69a8697165b35cc682ad598abc70f0bbcfdc12ca84ef888caf5ceee389237862e8f8c17da85f89 + languageName: node + linkType: hard + "escalade@npm:^3.1.1, escalade@npm:^3.1.2": version: 3.1.2 resolution: "escalade@npm:3.1.2" @@ -12908,9 +13150,12 @@ __metadata: chalk: ^4.0.0 ci-info: ^4.0.0 deepmerge: ^4.2.2 + esbuild: ^0.15.0 + esbuild-register: ^3.1.0 glob: ^10.3.10 graceful-fs: ^4.2.9 jest-circus: "workspace:*" + jest-docblock: "workspace:^" jest-environment-node: "workspace:*" jest-get-type: "workspace:*" jest-regex-util: "workspace:*" @@ -12928,10 +13173,13 @@ __metadata: typescript: ^5.0.4 peerDependencies: "@types/node": "*" + esbuild-register: ">=3.1.0" ts-node: ">=9.0.0" peerDependenciesMeta: "@types/node": optional: true + esbuild-register: + optional: true ts-node: optional: true languageName: unknown @@ -12950,7 +13198,7 @@ __metadata: languageName: unknown linkType: soft -"jest-docblock@workspace:*, jest-docblock@workspace:packages/jest-docblock": +"jest-docblock@workspace:*, jest-docblock@workspace:^, jest-docblock@workspace:packages/jest-docblock": version: 0.0.0-use.local resolution: "jest-docblock@workspace:packages/jest-docblock" dependencies: From 7dab201c75146861d4d3204863cf8f236e349873 Mon Sep 17 00:00:00 2001 From: Rajat v Date: Mon, 15 Jul 2024 05:15:45 +0000 Subject: [PATCH 02/15] more e2e tests --- e2e/__tests__/readInitialOptions.test.ts | 8 +++++++- .../async-config/jest.config.js | 2 +- e2e/read-initial-options/ts-config/jest.config.ts | 15 +++++++++++++++ packages/jest-config/package.json | 4 ++-- .../src/readConfigFileAndSetRootDir.ts | 2 +- yarn.lock | 6 +++--- 6 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 e2e/read-initial-options/ts-config/jest.config.ts diff --git a/e2e/__tests__/readInitialOptions.test.ts b/e2e/__tests__/readInitialOptions.test.ts index d20c6b853560..08e9a924da8f 100644 --- a/e2e/__tests__/readInitialOptions.test.ts +++ b/e2e/__tests__/readInitialOptions.test.ts @@ -84,7 +84,13 @@ describe('readInitialOptions', () => { }); expect(configPath).toEqual(expectedConfigFile); }); - + test('should give an error when there is no loader defined in jest.config.ts', async () => { + const rootDir = resolveFixture('ts-config'); + const error: Error = await proxyReadInitialOptions(undefined, {cwd:rootDir}).catch( + error => error, + ); + expect(error.message).toContain('Jest: loader is required for the TypeScript configuration files (https://jestjs.io/docs/configuration)'); + }); test('should give an error when there are multiple config files', async () => { const cwd = resolveFixture('multiple-config-files'); const error: Error = await proxyReadInitialOptions(undefined, {cwd}).catch( diff --git a/e2e/read-initial-options/async-config/jest.config.js b/e2e/read-initial-options/async-config/jest.config.js index 3e002067b1c8..812d02f942c0 100644 --- a/e2e/read-initial-options/async-config/jest.config.js +++ b/e2e/read-initial-options/async-config/jest.config.js @@ -6,6 +6,6 @@ */ module.exports = async function () { return { - jestConfig: 'async-config', + jestConfig: 'jest.config.js', }; }; diff --git a/e2e/read-initial-options/ts-config/jest.config.ts b/e2e/read-initial-options/ts-config/jest.config.ts new file mode 100644 index 000000000000..dbdc3af89456 --- /dev/null +++ b/e2e/read-initial-options/ts-config/jest.config.ts @@ -0,0 +1,15 @@ +/** + * 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. + * + */ +interface Config { + jestConfig: string; + } + + export default { + jestConfig: 'jest.config.ts', + } as Config; + \ No newline at end of file diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index caafc091fb03..c94a6d1dae51 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -20,7 +20,7 @@ }, "peerDependencies": { "@types/node": "*", - "esbuild-register": ">=3.1.0", + "esbuild-register": ">=3.4.0", "ts-node": ">=9.0.0" }, "peerDependenciesMeta": { @@ -65,7 +65,7 @@ "@types/micromatch": "^4.0.7", "@types/parse-json": "^4.0.0", "esbuild": "^0.15.0", - "esbuild-register": "^3.1.0", + "esbuild-register": "^3.4.0", "semver": "^7.5.3", "ts-node": "^10.5.0", "typescript": "^5.0.4" diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index 5335b691d266..f7e2d20b39ea 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -97,7 +97,7 @@ const loadTSConfigFile = async ( const tsLoader = docblockPragmas['jest-config-loader']; if(tsLoader===undefined){ throw new Error( - 'Jest: loader is required for the TypeScript configuration files.' + 'Jest: loader is required for the TypeScript configuration files (https://jestjs.io/docs/configuration)' ); } if (Array.isArray(tsLoader)) { diff --git a/yarn.lock b/yarn.lock index a1955b0ffb3d..febb91730524 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9605,7 +9605,7 @@ __metadata: languageName: node linkType: hard -"esbuild-register@npm:^3.1.0": +"esbuild-register@npm:^3.4.0": version: 3.5.0 resolution: "esbuild-register@npm:3.5.0" dependencies: @@ -13151,7 +13151,7 @@ __metadata: ci-info: ^4.0.0 deepmerge: ^4.2.2 esbuild: ^0.15.0 - esbuild-register: ^3.1.0 + esbuild-register: ^3.4.0 glob: ^10.3.10 graceful-fs: ^4.2.9 jest-circus: "workspace:*" @@ -13173,7 +13173,7 @@ __metadata: typescript: ^5.0.4 peerDependencies: "@types/node": "*" - esbuild-register: ">=3.1.0" + esbuild-register: ">=3.4.0" ts-node: ">=9.0.0" peerDependenciesMeta: "@types/node": From 7a9e2b183e1134f4c91d61d009c8120f4d8f1710 Mon Sep 17 00:00:00 2001 From: Rajat v Date: Mon, 15 Jul 2024 06:34:12 +0000 Subject: [PATCH 03/15] add loader in 2e tests --- e2e/typescript-config/modern-module-resolution/jest.config.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/e2e/typescript-config/modern-module-resolution/jest.config.ts b/e2e/typescript-config/modern-module-resolution/jest.config.ts index 995821a204f5..2a9a4cfdf46d 100644 --- a/e2e/typescript-config/modern-module-resolution/jest.config.ts +++ b/e2e/typescript-config/modern-module-resolution/jest.config.ts @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @jest-config-loader esbuild-register */ const config = { From dc23020badb376f9499fe77373ca0d2e8589c36f Mon Sep 17 00:00:00 2001 From: Rajat v Date: Mon, 15 Jul 2024 09:02:48 +0000 Subject: [PATCH 04/15] fix:set ts-node to default --- e2e/__tests__/readInitialOptions.test.ts | 9 +------- .../ts-config/jest.config.ts | 15 ------------ .../ts-esbuild-register-config/jest.config.ts | 13 +++++------ .../ts-node-config/jest.config.ts | 2 +- .../modern-module-resolution/jest.config.ts | 2 +- .../src/readConfigFileAndSetRootDir.ts | 23 +++++++++---------- 6 files changed, 20 insertions(+), 44 deletions(-) delete mode 100644 e2e/read-initial-options/ts-config/jest.config.ts diff --git a/e2e/__tests__/readInitialOptions.test.ts b/e2e/__tests__/readInitialOptions.test.ts index 08e9a924da8f..44ac75b513f8 100644 --- a/e2e/__tests__/readInitialOptions.test.ts +++ b/e2e/__tests__/readInitialOptions.test.ts @@ -64,7 +64,6 @@ describe('readInitialOptions', () => { expect(configPath).toEqual(configFile); }); - test('should be able to skip config reading, instead read from cwd', async () => { const expectedConfigFile = resolveFixture( 'json-config', @@ -84,13 +83,7 @@ describe('readInitialOptions', () => { }); expect(configPath).toEqual(expectedConfigFile); }); - test('should give an error when there is no loader defined in jest.config.ts', async () => { - const rootDir = resolveFixture('ts-config'); - const error: Error = await proxyReadInitialOptions(undefined, {cwd:rootDir}).catch( - error => error, - ); - expect(error.message).toContain('Jest: loader is required for the TypeScript configuration files (https://jestjs.io/docs/configuration)'); - }); + test('should give an error when there are multiple config files', async () => { const cwd = resolveFixture('multiple-config-files'); const error: Error = await proxyReadInitialOptions(undefined, {cwd}).catch( diff --git a/e2e/read-initial-options/ts-config/jest.config.ts b/e2e/read-initial-options/ts-config/jest.config.ts deleted file mode 100644 index dbdc3af89456..000000000000 --- a/e2e/read-initial-options/ts-config/jest.config.ts +++ /dev/null @@ -1,15 +0,0 @@ -/** - * 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. - * - */ -interface Config { - jestConfig: string; - } - - export default { - jestConfig: 'jest.config.ts', - } as Config; - \ No newline at end of file diff --git a/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts b/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts index 64a432bf78fd..9b003d380264 100644 --- a/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts +++ b/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts @@ -7,10 +7,9 @@ * @jest-config-loader esbuild-register */ interface Config { - jestConfig: string; - } - - export default { - jestConfig: 'jest.config.ts', - } as Config; - \ No newline at end of file + jestConfig: string; +} + +export default { + jestConfig: 'jest.config.ts', +} as Config; diff --git a/e2e/read-initial-options/ts-node-config/jest.config.ts b/e2e/read-initial-options/ts-node-config/jest.config.ts index 93976bf8a4bb..0b4884f67922 100644 --- a/e2e/read-initial-options/ts-node-config/jest.config.ts +++ b/e2e/read-initial-options/ts-node-config/jest.config.ts @@ -3,7 +3,7 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * + * * @jest-config-loader ts-node */ interface Config { diff --git a/e2e/typescript-config/modern-module-resolution/jest.config.ts b/e2e/typescript-config/modern-module-resolution/jest.config.ts index 2a9a4cfdf46d..e321266aa49d 100644 --- a/e2e/typescript-config/modern-module-resolution/jest.config.ts +++ b/e2e/typescript-config/modern-module-resolution/jest.config.ts @@ -3,7 +3,7 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * + * * @jest-config-loader esbuild-register */ diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index f7e2d20b39ea..ddc4acb2de80 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -10,8 +10,8 @@ import {isNativeError} from 'util/types'; import * as fs from 'graceful-fs'; import parseJson = require('parse-json'); import stripJsonComments = require('strip-json-comments'); +import type {Config} from '@jest/types'; import {extract, parse} from 'jest-docblock'; -import {Config} from '@jest/types'; import {interopRequireDefault, requireOrImportModule} from 'jest-util'; import { JEST_CONFIG_EXT_CTS, @@ -94,14 +94,10 @@ const loadTSConfigFile = async ( ): Promise => { // Get registered TypeScript compiler instance const docblockPragmas = parse(extract(fs.readFileSync(configPath, 'utf8'))); - const tsLoader = docblockPragmas['jest-config-loader']; - if(tsLoader===undefined){ - throw new Error( - 'Jest: loader is required for the TypeScript configuration files (https://jestjs.io/docs/configuration)' - ); - } + const tsLoader = docblockPragmas['jest-config-loader'] || 'ts-node'; + if (Array.isArray(tsLoader)) { - throw new Error( + throw new TypeError( `Jest: You can only define a single loader through docblocks, got "${tsLoader.join( ', ', )}"`, @@ -129,7 +125,8 @@ let registeredCompilerPromise: Promise; function getRegisteredCompiler(loader: TsLoaderModule) { // Cache the promise to avoid multiple registrations - registeredCompilerPromise = registeredCompilerPromise ?? registerTsLoader(loader); + registeredCompilerPromise = + registeredCompilerPromise ?? registerTsLoader(loader); return registeredCompilerPromise; } @@ -137,7 +134,7 @@ async function registerTsLoader(loader: TsLoaderModule): Promise { try { // Register TypeScript compiler instance if (loader === 'ts-node') { - const tsLoader = await import(/* webpackIgnore: true */'ts-node'); + const tsLoader = await import(/* webpackIgnore: true */ 'ts-node'); return tsLoader.register({ compilerOptions: { module: 'CommonJS', @@ -146,10 +143,12 @@ async function registerTsLoader(loader: TsLoaderModule): Promise { '**': 'cjs', }, transpileOnly: - process.env.JEST_CONFIG_TRANSPILE_ONLY?.toLowerCase() === 'true', + process.env.JEST_CONFIG_TRANSPILE_ONLY?.toLowerCase() === 'true', }); } else if (loader === 'esbuild-register') { - const tsLoader = await import(/* webpackIgnore: true */'esbuild-register/dist/node'); + const tsLoader = await import( + /* webpackIgnore: true */ 'esbuild-register/dist/node' + ); let instance: {unregister: () => void} | undefined; return { enabled: (bool: boolean) => { From a729f80f06e7da998837ebf3258d93c39f1a496f Mon Sep 17 00:00:00 2001 From: Rajat v Date: Mon, 15 Jul 2024 09:27:12 +0000 Subject: [PATCH 05/15] fix: failing checks --- .../ts-esbuild-register-config/jest.config.ts | 5 ++--- e2e/read-initial-options/ts-node-config/jest.config.ts | 5 ++--- packages/jest-config/package.json | 2 +- yarn.lock | 4 ++-- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts b/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts index 9b003d380264..30ae69caa645 100644 --- a/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts +++ b/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts @@ -1,11 +1,10 @@ /** - * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * @jest-config-loader esbuild-register */ +/**@jest-config-loader esbuild-register */ interface Config { jestConfig: string; } diff --git a/e2e/read-initial-options/ts-node-config/jest.config.ts b/e2e/read-initial-options/ts-node-config/jest.config.ts index 0b4884f67922..a0c619389fe8 100644 --- a/e2e/read-initial-options/ts-node-config/jest.config.ts +++ b/e2e/read-initial-options/ts-node-config/jest.config.ts @@ -1,11 +1,10 @@ /** - * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * @jest-config-loader ts-node */ +/**@jest-config-loader ts-node */ interface Config { jestConfig: string; } diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index c94a6d1dae51..1b151bb68f75 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -46,7 +46,7 @@ "glob": "^10.3.10", "graceful-fs": "^4.2.9", "jest-circus": "workspace:*", - "jest-docblock": "workspace:^", + "jest-docblock": "workspace:*", "jest-environment-node": "workspace:*", "jest-get-type": "workspace:*", "jest-regex-util": "workspace:*", diff --git a/yarn.lock b/yarn.lock index febb91730524..e7d9fe00d5d7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13155,7 +13155,7 @@ __metadata: glob: ^10.3.10 graceful-fs: ^4.2.9 jest-circus: "workspace:*" - jest-docblock: "workspace:^" + jest-docblock: "workspace:*" jest-environment-node: "workspace:*" jest-get-type: "workspace:*" jest-regex-util: "workspace:*" @@ -13198,7 +13198,7 @@ __metadata: languageName: unknown linkType: soft -"jest-docblock@workspace:*, jest-docblock@workspace:^, jest-docblock@workspace:packages/jest-docblock": +"jest-docblock@workspace:*, jest-docblock@workspace:packages/jest-docblock": version: 0.0.0-use.local resolution: "jest-docblock@workspace:packages/jest-docblock" dependencies: From 849f6b0536f75905f68b1d939063221218417445 Mon Sep 17 00:00:00 2001 From: Rajat v Date: Mon, 15 Jul 2024 09:39:03 +0000 Subject: [PATCH 06/15] Update Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dfb02abcd021..e605551aa784 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +- `[jest-config]` Support using esbuild-register for loading TS configs ([#15190](https://github.com/jestjs/jest/pull/15190)) - `[babel-jest]` Add option `excludeJestPreset` to allow opting out of `babel-preset-jest` ([#15164](https://github.com/jestjs/jest/pull/15164)) - `[jest-circus, jest-cli, jest-config]` Add `waitNextEventLoopTurnForUnhandledRejectionEvents` flag to minimise performance impact of correct detection of unhandled promise rejections introduced in [#14315](https://github.com/jestjs/jest/pull/14315) ([#14681](https://github.com/jestjs/jest/pull/14681)) - `[jest-circus]` Add a `waitBeforeRetry` option to `jest.retryTimes` ([#14738](https://github.com/jestjs/jest/pull/14738)) From 1bd881a00d8a3c34541ef6b98ae99e1eefd45fcc Mon Sep 17 00:00:00 2001 From: Rajat v Date: Mon, 15 Jul 2024 10:46:03 +0000 Subject: [PATCH 07/15] Update docs --- docs/Configuration.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index d4f2bbfec7e2..5e2928314736 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -57,9 +57,21 @@ export default async (): Promise => { :::tip -To read TypeScript configuration files Jest requires [`ts-node`](https://npmjs.com/package/ts-node). Make sure it is installed in your project. +To read TypeScript configuration files Jest by default requires [`ts-node`](https://npmjs.com/package/ts-node). You can override this behavior by adding a `@jest-config-loader` docblock at the top of the file. Currently, [`ts-node`](https://npmjs.com/package/ts-node) and [`esbuild-register`](https://npmjs.com/package/esbuild-register) is supported. Make sure `ts-node` or the loader you specify is installed. -To read configuration files without typechecking, You can set `JEST_CONFIG_TRANSPILE_ONLY` environment variable to `true` (case insensitive). +```ts title="jest.config.ts" +/**@jest-config-loader ts-node(or esbuild-register)*/ + +import type {Config} from 'jest'; + +const config: Config = { + verbose: true, +}; + +export default config; +``` + +If you are using `ts-node`, you can set `JEST_CONFIG_TRANSPILE_ONLY` environment variable to `true` (case insensitive) to read configuration files without typechecking. ::: From b9c6ee5d1987d546e16a1774cbb75bb96c72875a Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 16 Jul 2024 11:31:13 +0200 Subject: [PATCH 08/15] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e605551aa784..52697154b468 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,6 @@ ### Features -- `[jest-config]` Support using esbuild-register for loading TS configs ([#15190](https://github.com/jestjs/jest/pull/15190)) - `[babel-jest]` Add option `excludeJestPreset` to allow opting out of `babel-preset-jest` ([#15164](https://github.com/jestjs/jest/pull/15164)) - `[jest-circus, jest-cli, jest-config]` Add `waitNextEventLoopTurnForUnhandledRejectionEvents` flag to minimise performance impact of correct detection of unhandled promise rejections introduced in [#14315](https://github.com/jestjs/jest/pull/14315) ([#14681](https://github.com/jestjs/jest/pull/14681)) - `[jest-circus]` Add a `waitBeforeRetry` option to `jest.retryTimes` ([#14738](https://github.com/jestjs/jest/pull/14738)) @@ -13,6 +12,7 @@ - `[jest-config]` Loads config file from provided path in `package.json` ([#14044](https://github.com/facebook/jest/pull/14044)) - `[jest-config]` Allow loading `jest.config.cts` files ([#14070](https://github.com/facebook/jest/pull/14070)) - `[jest-config]` Added an option to disable `ts-node` typechecking ([#15161](https://github.com/jestjs/jest/pull/15161)) +- `[jest-config]` Support loading TS config files via docblock loader ([#15190](https://github.com/jestjs/jest/pull/15190)) - `[@jest/core]` Group together open handles with the same stack trace ([#13417](https://github.com/jestjs/jest/pull/13417), & [#14789](https://github.com/jestjs/jest/pull/14789)) - `[@jest/core]` Add `perfStats` to surface test setup overhead ([#14622](https://github.com/jestjs/jest/pull/14622)) - `[@jest/core]` [**BREAKING**] Changed `--filter` to accept an object with shape `{ filtered: Array }` to match [documentation](https://jestjs.io/docs/cli#--filterfile) ([#13319](https://github.com/jestjs/jest/pull/13319)) From 9cd57e8ec5bfcecc454cf924096398470ef97547 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 16 Jul 2024 11:32:08 +0200 Subject: [PATCH 09/15] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52697154b468..0ae58edb359c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ - `[jest-config]` Loads config file from provided path in `package.json` ([#14044](https://github.com/facebook/jest/pull/14044)) - `[jest-config]` Allow loading `jest.config.cts` files ([#14070](https://github.com/facebook/jest/pull/14070)) - `[jest-config]` Added an option to disable `ts-node` typechecking ([#15161](https://github.com/jestjs/jest/pull/15161)) -- `[jest-config]` Support loading TS config files via docblock loader ([#15190](https://github.com/jestjs/jest/pull/15190)) +- `[jest-config]` Support loading TS config files using `esbuild-register` via docblock loader ([#15190](https://github.com/jestjs/jest/pull/15190)) - `[@jest/core]` Group together open handles with the same stack trace ([#13417](https://github.com/jestjs/jest/pull/13417), & [#14789](https://github.com/jestjs/jest/pull/14789)) - `[@jest/core]` Add `perfStats` to surface test setup overhead ([#14622](https://github.com/jestjs/jest/pull/14622)) - `[@jest/core]` [**BREAKING**] Changed `--filter` to accept an object with shape `{ filtered: Array }` to match [documentation](https://jestjs.io/docs/cli#--filterfile) ([#13319](https://github.com/jestjs/jest/pull/13319)) From 9747160711db95a75beea496d0781a8c86725e2f Mon Sep 17 00:00:00 2001 From: Rajat v Date: Tue, 16 Jul 2024 12:27:40 +0000 Subject: [PATCH 10/15] fix: requested changes --- docs/Configuration.md | 4 +- e2e/__tests__/readInitialOptions.test.ts | 8 +- .../async-config/jest.config.js | 2 +- .../js-config/jest.config.js | 2 +- .../json-config/jest.config.json | 2 +- .../mjs-config/jest.config.mjs | 2 +- .../multiple-config-files/jest.config.js | 2 +- .../multiple-config-files/jest.config.json | 2 +- .../pkg-config/package.json | 2 +- .../ts-esbuild-register-config/jest.config.ts | 2 +- .../ts-node-config/jest.config.ts | 2 +- packages/jest-config/package.json | 2 +- yarn.lock | 410 +++++++++--------- 13 files changed, 232 insertions(+), 210 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 5e2928314736..c73a1fcaa6f5 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -60,7 +60,9 @@ export default async (): Promise => { To read TypeScript configuration files Jest by default requires [`ts-node`](https://npmjs.com/package/ts-node). You can override this behavior by adding a `@jest-config-loader` docblock at the top of the file. Currently, [`ts-node`](https://npmjs.com/package/ts-node) and [`esbuild-register`](https://npmjs.com/package/esbuild-register) is supported. Make sure `ts-node` or the loader you specify is installed. ```ts title="jest.config.ts" -/**@jest-config-loader ts-node(or esbuild-register)*/ +/** @jest-config-loader ts-node */ +// or +/** @jest-config-loader esbuild-register */ import type {Config} from 'jest'; diff --git a/e2e/__tests__/readInitialOptions.test.ts b/e2e/__tests__/readInitialOptions.test.ts index 44ac75b513f8..c42a034a8c47 100644 --- a/e2e/__tests__/readInitialOptions.test.ts +++ b/e2e/__tests__/readInitialOptions.test.ts @@ -43,7 +43,7 @@ describe('readInitialOptions', () => { const {config, configPath} = await proxyReadInitialOptions(undefined, { cwd: rootDir, }); - expect(config).toEqual({jestConfig: 'jest.config.js', rootDir}); + expect(config).toEqual({jestConfig: 'jest-config', rootDir}); expect(configPath).toEqual(configFile); }); test.each([ @@ -60,7 +60,7 @@ describe('readInitialOptions', () => { const {config, configPath} = await proxyReadInitialOptions(undefined, { cwd: rootDir, }); - expect(config).toEqual({jestConfig: filename, rootDir}); + expect(config).toEqual({jestConfig: 'jest-config', rootDir}); expect(configPath).toEqual(configFile); }); @@ -78,7 +78,7 @@ describe('readInitialOptions', () => { ); expect(config).toEqual({ - jestConfig: 'jest.config.json', + jestConfig: 'jest-config', rootDir: path.dirname(expectedConfigFile), }); expect(configPath).toEqual(expectedConfigFile); @@ -101,7 +101,7 @@ describe('readInitialOptions', () => { skipMultipleConfigError: true, }); expect(config).toEqual({ - jestConfig: 'jest.config.js', + jestConfig: 'jest-config', rootDir: resolveFixture('multiple-config-files'), }); expect(configPath).toEqual( diff --git a/e2e/read-initial-options/async-config/jest.config.js b/e2e/read-initial-options/async-config/jest.config.js index 812d02f942c0..ac2e3a577c80 100644 --- a/e2e/read-initial-options/async-config/jest.config.js +++ b/e2e/read-initial-options/async-config/jest.config.js @@ -6,6 +6,6 @@ */ module.exports = async function () { return { - jestConfig: 'jest.config.js', + jestConfig: 'jest-config', }; }; diff --git a/e2e/read-initial-options/js-config/jest.config.js b/e2e/read-initial-options/js-config/jest.config.js index ab426a7f04dd..78850f9adc0f 100644 --- a/e2e/read-initial-options/js-config/jest.config.js +++ b/e2e/read-initial-options/js-config/jest.config.js @@ -5,5 +5,5 @@ * LICENSE file in the root directory of this source tree. */ module.exports = { - jestConfig: 'jest.config.js', + jestConfig: 'jest-config', }; diff --git a/e2e/read-initial-options/json-config/jest.config.json b/e2e/read-initial-options/json-config/jest.config.json index bf022d79f271..467a3cb0de32 100644 --- a/e2e/read-initial-options/json-config/jest.config.json +++ b/e2e/read-initial-options/json-config/jest.config.json @@ -1,3 +1,3 @@ { - "jestConfig": "jest.config.json" + "jestConfig": "jest-config" } diff --git a/e2e/read-initial-options/mjs-config/jest.config.mjs b/e2e/read-initial-options/mjs-config/jest.config.mjs index df42e372ba3a..0a440968ed4d 100644 --- a/e2e/read-initial-options/mjs-config/jest.config.mjs +++ b/e2e/read-initial-options/mjs-config/jest.config.mjs @@ -5,5 +5,5 @@ * LICENSE file in the root directory of this source tree. */ export default { - jestConfig: 'jest.config.mjs', + jestConfig: 'jest-config', }; diff --git a/e2e/read-initial-options/multiple-config-files/jest.config.js b/e2e/read-initial-options/multiple-config-files/jest.config.js index ab426a7f04dd..78850f9adc0f 100644 --- a/e2e/read-initial-options/multiple-config-files/jest.config.js +++ b/e2e/read-initial-options/multiple-config-files/jest.config.js @@ -5,5 +5,5 @@ * LICENSE file in the root directory of this source tree. */ module.exports = { - jestConfig: 'jest.config.js', + jestConfig: 'jest-config', }; diff --git a/e2e/read-initial-options/multiple-config-files/jest.config.json b/e2e/read-initial-options/multiple-config-files/jest.config.json index bf022d79f271..467a3cb0de32 100644 --- a/e2e/read-initial-options/multiple-config-files/jest.config.json +++ b/e2e/read-initial-options/multiple-config-files/jest.config.json @@ -1,3 +1,3 @@ { - "jestConfig": "jest.config.json" + "jestConfig": "jest-config" } diff --git a/e2e/read-initial-options/pkg-config/package.json b/e2e/read-initial-options/pkg-config/package.json index 766501789210..eafb99795b30 100644 --- a/e2e/read-initial-options/pkg-config/package.json +++ b/e2e/read-initial-options/pkg-config/package.json @@ -1,5 +1,5 @@ { "jest": { - "jestConfig": "package.json" + "jestConfig": "jest-config" } } diff --git a/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts b/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts index 30ae69caa645..1554d6cdc83a 100644 --- a/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts +++ b/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts @@ -10,5 +10,5 @@ interface Config { } export default { - jestConfig: 'jest.config.ts', + jestConfig: 'jest-config', } as Config; diff --git a/e2e/read-initial-options/ts-node-config/jest.config.ts b/e2e/read-initial-options/ts-node-config/jest.config.ts index a0c619389fe8..3be44741a38e 100644 --- a/e2e/read-initial-options/ts-node-config/jest.config.ts +++ b/e2e/read-initial-options/ts-node-config/jest.config.ts @@ -10,5 +10,5 @@ interface Config { } export default { - jestConfig: 'jest.config.ts', + jestConfig: 'jest-config', } as Config; diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index 1b151bb68f75..a7455ef34fd1 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -64,7 +64,7 @@ "@types/graceful-fs": "^4.1.3", "@types/micromatch": "^4.0.7", "@types/parse-json": "^4.0.0", - "esbuild": "^0.15.0", + "esbuild": "^0.23.0", "esbuild-register": "^3.4.0", "semver": "^7.5.3", "ts-node": "^10.5.0", diff --git a/yarn.lock b/yarn.lock index e7d9fe00d5d7..9750f8958cfa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2686,20 +2686,174 @@ __metadata: languageName: node linkType: hard -"@esbuild/android-arm@npm:0.15.18": - version: 0.15.18 - resolution: "@esbuild/android-arm@npm:0.15.18" +"@esbuild/aix-ppc64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/aix-ppc64@npm:0.23.0" + conditions: os=aix & cpu=ppc64 + languageName: node + linkType: hard + +"@esbuild/android-arm64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/android-arm64@npm:0.23.0" + conditions: os=android & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/android-arm@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/android-arm@npm:0.23.0" conditions: os=android & cpu=arm languageName: node linkType: hard -"@esbuild/linux-loong64@npm:0.15.18": - version: 0.15.18 - resolution: "@esbuild/linux-loong64@npm:0.15.18" +"@esbuild/android-x64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/android-x64@npm:0.23.0" + conditions: os=android & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/darwin-arm64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/darwin-arm64@npm:0.23.0" + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/darwin-x64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/darwin-x64@npm:0.23.0" + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/freebsd-arm64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/freebsd-arm64@npm:0.23.0" + conditions: os=freebsd & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/freebsd-x64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/freebsd-x64@npm:0.23.0" + conditions: os=freebsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/linux-arm64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/linux-arm64@npm:0.23.0" + conditions: os=linux & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/linux-arm@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/linux-arm@npm:0.23.0" + conditions: os=linux & cpu=arm + languageName: node + linkType: hard + +"@esbuild/linux-ia32@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/linux-ia32@npm:0.23.0" + conditions: os=linux & cpu=ia32 + languageName: node + linkType: hard + +"@esbuild/linux-loong64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/linux-loong64@npm:0.23.0" conditions: os=linux & cpu=loong64 languageName: node linkType: hard +"@esbuild/linux-mips64el@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/linux-mips64el@npm:0.23.0" + conditions: os=linux & cpu=mips64el + languageName: node + linkType: hard + +"@esbuild/linux-ppc64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/linux-ppc64@npm:0.23.0" + conditions: os=linux & cpu=ppc64 + languageName: node + linkType: hard + +"@esbuild/linux-riscv64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/linux-riscv64@npm:0.23.0" + conditions: os=linux & cpu=riscv64 + languageName: node + linkType: hard + +"@esbuild/linux-s390x@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/linux-s390x@npm:0.23.0" + conditions: os=linux & cpu=s390x + languageName: node + linkType: hard + +"@esbuild/linux-x64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/linux-x64@npm:0.23.0" + conditions: os=linux & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/netbsd-x64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/netbsd-x64@npm:0.23.0" + conditions: os=netbsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/openbsd-arm64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/openbsd-arm64@npm:0.23.0" + conditions: os=openbsd & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/openbsd-x64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/openbsd-x64@npm:0.23.0" + conditions: os=openbsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/sunos-x64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/sunos-x64@npm:0.23.0" + conditions: os=sunos & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/win32-arm64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/win32-arm64@npm:0.23.0" + conditions: os=win32 & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/win32-ia32@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/win32-ia32@npm:0.23.0" + conditions: os=win32 & cpu=ia32 + languageName: node + linkType: hard + +"@esbuild/win32-x64@npm:0.23.0": + version: 0.23.0 + resolution: "@esbuild/win32-x64@npm:0.23.0" + conditions: os=win32 & cpu=x64 + languageName: node + linkType: hard + "@eslint-community/eslint-utils@npm:^4.2.0, @eslint-community/eslint-utils@npm:^4.4.0": version: 4.4.0 resolution: "@eslint-community/eslint-utils@npm:4.4.0" @@ -9493,118 +9647,6 @@ __metadata: languageName: node linkType: hard -"esbuild-android-64@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-android-64@npm:0.15.18" - conditions: os=android & cpu=x64 - languageName: node - linkType: hard - -"esbuild-android-arm64@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-android-arm64@npm:0.15.18" - conditions: os=android & cpu=arm64 - languageName: node - linkType: hard - -"esbuild-darwin-64@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-darwin-64@npm:0.15.18" - conditions: os=darwin & cpu=x64 - languageName: node - linkType: hard - -"esbuild-darwin-arm64@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-darwin-arm64@npm:0.15.18" - conditions: os=darwin & cpu=arm64 - languageName: node - linkType: hard - -"esbuild-freebsd-64@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-freebsd-64@npm:0.15.18" - conditions: os=freebsd & cpu=x64 - languageName: node - linkType: hard - -"esbuild-freebsd-arm64@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-freebsd-arm64@npm:0.15.18" - conditions: os=freebsd & cpu=arm64 - languageName: node - linkType: hard - -"esbuild-linux-32@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-linux-32@npm:0.15.18" - conditions: os=linux & cpu=ia32 - languageName: node - linkType: hard - -"esbuild-linux-64@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-linux-64@npm:0.15.18" - conditions: os=linux & cpu=x64 - languageName: node - linkType: hard - -"esbuild-linux-arm64@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-linux-arm64@npm:0.15.18" - conditions: os=linux & cpu=arm64 - languageName: node - linkType: hard - -"esbuild-linux-arm@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-linux-arm@npm:0.15.18" - conditions: os=linux & cpu=arm - languageName: node - linkType: hard - -"esbuild-linux-mips64le@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-linux-mips64le@npm:0.15.18" - conditions: os=linux & cpu=mips64el - languageName: node - linkType: hard - -"esbuild-linux-ppc64le@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-linux-ppc64le@npm:0.15.18" - conditions: os=linux & cpu=ppc64 - languageName: node - linkType: hard - -"esbuild-linux-riscv64@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-linux-riscv64@npm:0.15.18" - conditions: os=linux & cpu=riscv64 - languageName: node - linkType: hard - -"esbuild-linux-s390x@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-linux-s390x@npm:0.15.18" - conditions: os=linux & cpu=s390x - languageName: node - linkType: hard - -"esbuild-netbsd-64@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-netbsd-64@npm:0.15.18" - conditions: os=netbsd & cpu=x64 - languageName: node - linkType: hard - -"esbuild-openbsd-64@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-openbsd-64@npm:0.15.18" - conditions: os=openbsd & cpu=x64 - languageName: node - linkType: hard - "esbuild-register@npm:^3.4.0": version: 3.5.0 resolution: "esbuild-register@npm:3.5.0" @@ -9616,108 +9658,86 @@ __metadata: languageName: node linkType: hard -"esbuild-sunos-64@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-sunos-64@npm:0.15.18" - conditions: os=sunos & cpu=x64 - languageName: node - linkType: hard - -"esbuild-windows-32@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-windows-32@npm:0.15.18" - conditions: os=win32 & cpu=ia32 - languageName: node - linkType: hard - -"esbuild-windows-64@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-windows-64@npm:0.15.18" - conditions: os=win32 & cpu=x64 - languageName: node - linkType: hard - -"esbuild-windows-arm64@npm:0.15.18": - version: 0.15.18 - resolution: "esbuild-windows-arm64@npm:0.15.18" - conditions: os=win32 & cpu=arm64 - languageName: node - linkType: hard - -"esbuild@npm:^0.15.0": - version: 0.15.18 - resolution: "esbuild@npm:0.15.18" - dependencies: - "@esbuild/android-arm": 0.15.18 - "@esbuild/linux-loong64": 0.15.18 - esbuild-android-64: 0.15.18 - esbuild-android-arm64: 0.15.18 - esbuild-darwin-64: 0.15.18 - esbuild-darwin-arm64: 0.15.18 - esbuild-freebsd-64: 0.15.18 - esbuild-freebsd-arm64: 0.15.18 - esbuild-linux-32: 0.15.18 - esbuild-linux-64: 0.15.18 - esbuild-linux-arm: 0.15.18 - esbuild-linux-arm64: 0.15.18 - esbuild-linux-mips64le: 0.15.18 - esbuild-linux-ppc64le: 0.15.18 - esbuild-linux-riscv64: 0.15.18 - esbuild-linux-s390x: 0.15.18 - esbuild-netbsd-64: 0.15.18 - esbuild-openbsd-64: 0.15.18 - esbuild-sunos-64: 0.15.18 - esbuild-windows-32: 0.15.18 - esbuild-windows-64: 0.15.18 - esbuild-windows-arm64: 0.15.18 +"esbuild@npm:^0.23.0": + version: 0.23.0 + resolution: "esbuild@npm:0.23.0" + dependencies: + "@esbuild/aix-ppc64": 0.23.0 + "@esbuild/android-arm": 0.23.0 + "@esbuild/android-arm64": 0.23.0 + "@esbuild/android-x64": 0.23.0 + "@esbuild/darwin-arm64": 0.23.0 + "@esbuild/darwin-x64": 0.23.0 + "@esbuild/freebsd-arm64": 0.23.0 + "@esbuild/freebsd-x64": 0.23.0 + "@esbuild/linux-arm": 0.23.0 + "@esbuild/linux-arm64": 0.23.0 + "@esbuild/linux-ia32": 0.23.0 + "@esbuild/linux-loong64": 0.23.0 + "@esbuild/linux-mips64el": 0.23.0 + "@esbuild/linux-ppc64": 0.23.0 + "@esbuild/linux-riscv64": 0.23.0 + "@esbuild/linux-s390x": 0.23.0 + "@esbuild/linux-x64": 0.23.0 + "@esbuild/netbsd-x64": 0.23.0 + "@esbuild/openbsd-arm64": 0.23.0 + "@esbuild/openbsd-x64": 0.23.0 + "@esbuild/sunos-x64": 0.23.0 + "@esbuild/win32-arm64": 0.23.0 + "@esbuild/win32-ia32": 0.23.0 + "@esbuild/win32-x64": 0.23.0 dependenciesMeta: + "@esbuild/aix-ppc64": + optional: true "@esbuild/android-arm": optional: true - "@esbuild/linux-loong64": + "@esbuild/android-arm64": optional: true - esbuild-android-64: + "@esbuild/android-x64": optional: true - esbuild-android-arm64: + "@esbuild/darwin-arm64": optional: true - esbuild-darwin-64: + "@esbuild/darwin-x64": optional: true - esbuild-darwin-arm64: + "@esbuild/freebsd-arm64": optional: true - esbuild-freebsd-64: + "@esbuild/freebsd-x64": optional: true - esbuild-freebsd-arm64: + "@esbuild/linux-arm": optional: true - esbuild-linux-32: + "@esbuild/linux-arm64": optional: true - esbuild-linux-64: + "@esbuild/linux-ia32": + optional: true + "@esbuild/linux-loong64": optional: true - esbuild-linux-arm: + "@esbuild/linux-mips64el": optional: true - esbuild-linux-arm64: + "@esbuild/linux-ppc64": optional: true - esbuild-linux-mips64le: + "@esbuild/linux-riscv64": optional: true - esbuild-linux-ppc64le: + "@esbuild/linux-s390x": optional: true - esbuild-linux-riscv64: + "@esbuild/linux-x64": optional: true - esbuild-linux-s390x: + "@esbuild/netbsd-x64": optional: true - esbuild-netbsd-64: + "@esbuild/openbsd-arm64": optional: true - esbuild-openbsd-64: + "@esbuild/openbsd-x64": optional: true - esbuild-sunos-64: + "@esbuild/sunos-x64": optional: true - esbuild-windows-32: + "@esbuild/win32-arm64": optional: true - esbuild-windows-64: + "@esbuild/win32-ia32": optional: true - esbuild-windows-arm64: + "@esbuild/win32-x64": optional: true bin: esbuild: bin/esbuild - checksum: ec12682b2cb2d4f0669d0e555028b87a9284ca7f6a1b26e35e69a8697165b35cc682ad598abc70f0bbcfdc12ca84ef888caf5ceee389237862e8f8c17da85f89 + checksum: 22138538225d5ce79f84fc0d3d3e31b57a91ef50ef00f2d6a9c8a4be4ed28d4b1d0ed14239e54341d1b9a7079f25e69761d0266f3c255da94e647b079b790421 languageName: node linkType: hard @@ -13150,7 +13170,7 @@ __metadata: chalk: ^4.0.0 ci-info: ^4.0.0 deepmerge: ^4.2.2 - esbuild: ^0.15.0 + esbuild: ^0.23.0 esbuild-register: ^3.4.0 glob: ^10.3.10 graceful-fs: ^4.2.9 From deea0d6930bef3ca0492f0db03249f2a2f745b94 Mon Sep 17 00:00:00 2001 From: Rajat v Date: Tue, 16 Jul 2024 16:41:20 +0000 Subject: [PATCH 11/15] fix: tests --- e2e/__tests__/readInitialOptions.test.ts | 19 +++++++++++++------ .../async-config/jest.config.js | 2 +- .../js-config/jest.config.js | 2 +- .../json-config/jest.config.json | 2 +- .../mjs-config/jest.config.mjs | 2 +- .../multiple-config-files/jest.config.js | 2 +- .../multiple-config-files/jest.config.json | 2 +- .../pkg-config/package.json | 2 +- .../ts-esbuild-register-config/jest.config.ts | 2 +- .../ts-node-config/jest.config.ts | 2 +- 10 files changed, 22 insertions(+), 15 deletions(-) diff --git a/e2e/__tests__/readInitialOptions.test.ts b/e2e/__tests__/readInitialOptions.test.ts index c42a034a8c47..72a567c7ae52 100644 --- a/e2e/__tests__/readInitialOptions.test.ts +++ b/e2e/__tests__/readInitialOptions.test.ts @@ -43,7 +43,7 @@ describe('readInitialOptions', () => { const {config, configPath} = await proxyReadInitialOptions(undefined, { cwd: rootDir, }); - expect(config).toEqual({jestConfig: 'jest-config', rootDir}); + expect(config).toEqual({jestConfig: 'jest.config.js', rootDir}); expect(configPath).toEqual(configFile); }); test.each([ @@ -53,17 +53,24 @@ describe('readInitialOptions', () => { ['ts-esbuild-register-config', 'jest.config.ts'], ['mjs-config', 'jest.config.mjs'], ['json-config', 'jest.config.json'], - ['async-config', 'jest.config.js'], ])('should read %s/%s file', async (directory: string, filename: string) => { const configFile = resolveFixture(directory, filename); const rootDir = resolveFixture(directory); const {config, configPath} = await proxyReadInitialOptions(undefined, { cwd: rootDir, }); - expect(config).toEqual({jestConfig: 'jest-config', rootDir}); + expect(config).toEqual({jestConfig: filename, rootDir}); + expect(configPath).toEqual(configFile); + }); + test('should read a jest config exporting an async function', async () => { + const configFile = resolveFixture('async-config', 'jest.config.js'); + const rootDir = resolveFixture('async-config'); + const {config, configPath} = await proxyReadInitialOptions(undefined, { + cwd: rootDir, + }); + expect(config).toEqual({jestConfig: 'async-config', rootDir}); expect(configPath).toEqual(configFile); }); - test('should be able to skip config reading, instead read from cwd', async () => { const expectedConfigFile = resolveFixture( 'json-config', @@ -78,7 +85,7 @@ describe('readInitialOptions', () => { ); expect(config).toEqual({ - jestConfig: 'jest-config', + jestConfig: 'jest.config.json', rootDir: path.dirname(expectedConfigFile), }); expect(configPath).toEqual(expectedConfigFile); @@ -101,7 +108,7 @@ describe('readInitialOptions', () => { skipMultipleConfigError: true, }); expect(config).toEqual({ - jestConfig: 'jest-config', + jestConfig: 'jest.config.js', rootDir: resolveFixture('multiple-config-files'), }); expect(configPath).toEqual( diff --git a/e2e/read-initial-options/async-config/jest.config.js b/e2e/read-initial-options/async-config/jest.config.js index ac2e3a577c80..3e002067b1c8 100644 --- a/e2e/read-initial-options/async-config/jest.config.js +++ b/e2e/read-initial-options/async-config/jest.config.js @@ -6,6 +6,6 @@ */ module.exports = async function () { return { - jestConfig: 'jest-config', + jestConfig: 'async-config', }; }; diff --git a/e2e/read-initial-options/js-config/jest.config.js b/e2e/read-initial-options/js-config/jest.config.js index 78850f9adc0f..ab426a7f04dd 100644 --- a/e2e/read-initial-options/js-config/jest.config.js +++ b/e2e/read-initial-options/js-config/jest.config.js @@ -5,5 +5,5 @@ * LICENSE file in the root directory of this source tree. */ module.exports = { - jestConfig: 'jest-config', + jestConfig: 'jest.config.js', }; diff --git a/e2e/read-initial-options/json-config/jest.config.json b/e2e/read-initial-options/json-config/jest.config.json index 467a3cb0de32..bf022d79f271 100644 --- a/e2e/read-initial-options/json-config/jest.config.json +++ b/e2e/read-initial-options/json-config/jest.config.json @@ -1,3 +1,3 @@ { - "jestConfig": "jest-config" + "jestConfig": "jest.config.json" } diff --git a/e2e/read-initial-options/mjs-config/jest.config.mjs b/e2e/read-initial-options/mjs-config/jest.config.mjs index 0a440968ed4d..df42e372ba3a 100644 --- a/e2e/read-initial-options/mjs-config/jest.config.mjs +++ b/e2e/read-initial-options/mjs-config/jest.config.mjs @@ -5,5 +5,5 @@ * LICENSE file in the root directory of this source tree. */ export default { - jestConfig: 'jest-config', + jestConfig: 'jest.config.mjs', }; diff --git a/e2e/read-initial-options/multiple-config-files/jest.config.js b/e2e/read-initial-options/multiple-config-files/jest.config.js index 78850f9adc0f..ab426a7f04dd 100644 --- a/e2e/read-initial-options/multiple-config-files/jest.config.js +++ b/e2e/read-initial-options/multiple-config-files/jest.config.js @@ -5,5 +5,5 @@ * LICENSE file in the root directory of this source tree. */ module.exports = { - jestConfig: 'jest-config', + jestConfig: 'jest.config.js', }; diff --git a/e2e/read-initial-options/multiple-config-files/jest.config.json b/e2e/read-initial-options/multiple-config-files/jest.config.json index 467a3cb0de32..bf022d79f271 100644 --- a/e2e/read-initial-options/multiple-config-files/jest.config.json +++ b/e2e/read-initial-options/multiple-config-files/jest.config.json @@ -1,3 +1,3 @@ { - "jestConfig": "jest-config" + "jestConfig": "jest.config.json" } diff --git a/e2e/read-initial-options/pkg-config/package.json b/e2e/read-initial-options/pkg-config/package.json index eafb99795b30..766501789210 100644 --- a/e2e/read-initial-options/pkg-config/package.json +++ b/e2e/read-initial-options/pkg-config/package.json @@ -1,5 +1,5 @@ { "jest": { - "jestConfig": "jest-config" + "jestConfig": "package.json" } } diff --git a/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts b/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts index 1554d6cdc83a..30ae69caa645 100644 --- a/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts +++ b/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts @@ -10,5 +10,5 @@ interface Config { } export default { - jestConfig: 'jest-config', + jestConfig: 'jest.config.ts', } as Config; diff --git a/e2e/read-initial-options/ts-node-config/jest.config.ts b/e2e/read-initial-options/ts-node-config/jest.config.ts index 3be44741a38e..a0c619389fe8 100644 --- a/e2e/read-initial-options/ts-node-config/jest.config.ts +++ b/e2e/read-initial-options/ts-node-config/jest.config.ts @@ -10,5 +10,5 @@ interface Config { } export default { - jestConfig: 'jest-config', + jestConfig: 'jest.config.ts', } as Config; From afb45092d0e9647516a451fcbcf65614352f9cf7 Mon Sep 17 00:00:00 2001 From: Rajat v Date: Wed, 17 Jul 2024 04:15:04 +0000 Subject: [PATCH 12/15] fix: passing 3rd arg in test.each --- e2e/__tests__/readInitialOptions.test.ts | 44 +++++++++++------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/e2e/__tests__/readInitialOptions.test.ts b/e2e/__tests__/readInitialOptions.test.ts index 72a567c7ae52..ed595764fe66 100644 --- a/e2e/__tests__/readInitialOptions.test.ts +++ b/e2e/__tests__/readInitialOptions.test.ts @@ -47,30 +47,26 @@ describe('readInitialOptions', () => { expect(configPath).toEqual(configFile); }); test.each([ - ['js-config', 'jest.config.js'], - ['pkg-config', 'package.json'], - ['ts-node-config', 'jest.config.ts'], - ['ts-esbuild-register-config', 'jest.config.ts'], - ['mjs-config', 'jest.config.mjs'], - ['json-config', 'jest.config.json'], - ])('should read %s/%s file', async (directory: string, filename: string) => { - const configFile = resolveFixture(directory, filename); - const rootDir = resolveFixture(directory); - const {config, configPath} = await proxyReadInitialOptions(undefined, { - cwd: rootDir, - }); - expect(config).toEqual({jestConfig: filename, rootDir}); - expect(configPath).toEqual(configFile); - }); - test('should read a jest config exporting an async function', async () => { - const configFile = resolveFixture('async-config', 'jest.config.js'); - const rootDir = resolveFixture('async-config'); - const {config, configPath} = await proxyReadInitialOptions(undefined, { - cwd: rootDir, - }); - expect(config).toEqual({jestConfig: 'async-config', rootDir}); - expect(configPath).toEqual(configFile); - }); + ['js-config', 'jest.config.js', 'jest.config.js'], + ['pkg-config', 'package.json', 'package.json'], + ['ts-node-config', 'jest.config.ts', 'jest.config.ts'], + ['ts-esbuild-register-config', 'jest.config.ts', 'jest.config.ts'], + ['mjs-config', 'jest.config.mjs', 'jest.config.mjs'], + ['json-config', 'jest.config.json', 'jest.config.json'], + ['async-config', 'jest.config.js', 'async-config'], + ])( + 'should read %s/%s file', + async (directory: string, filename: string, configString: string) => { + const configFile = resolveFixture(directory, filename); + const rootDir = resolveFixture(directory); + const {config, configPath} = await proxyReadInitialOptions(undefined, { + cwd: rootDir, + }); + expect(config).toEqual({jestConfig: configString, rootDir}); + expect(configPath).toEqual(configFile); + }, + ); + test('should be able to skip config reading, instead read from cwd', async () => { const expectedConfigFile = resolveFixture( 'json-config', From d2c3a8dd490537b64bfb06a1cbacff03e8c047fa Mon Sep 17 00:00:00 2001 From: Rajat Date: Thu, 18 Jul 2024 11:04:12 +0530 Subject: [PATCH 13/15] e2e tests for different loaders and for unsupported loaders --- e2e/__tests__/multipleConfigs.ts | 12 ++++++++++++ e2e/__tests__/readInitialOptions.test.ts | 10 +++++++++- .../prj-1/__tests__/test.js | 10 ++++++++++ .../prj-1/jest.config.ts | 16 ++++++++++++++++ .../prj-2/__tests__/test.js | 10 ++++++++++ .../prj-2/jest.config.ts | 16 ++++++++++++++++ .../ts-esbuild-register-config/jest.config.ts | 4 +++- .../ts-loader-config/jest.config.ts | 16 ++++++++++++++++ .../ts-node-config/jest.config.ts | 4 +++- 9 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 e2e/multi-project-multiple-configs/prj-1/__tests__/test.js create mode 100644 e2e/multi-project-multiple-configs/prj-1/jest.config.ts create mode 100644 e2e/multi-project-multiple-configs/prj-2/__tests__/test.js create mode 100644 e2e/multi-project-multiple-configs/prj-2/jest.config.ts create mode 100644 e2e/read-initial-options/ts-loader-config/jest.config.ts diff --git a/e2e/__tests__/multipleConfigs.ts b/e2e/__tests__/multipleConfigs.ts index c0f2dd681ed6..dee87c683665 100644 --- a/e2e/__tests__/multipleConfigs.ts +++ b/e2e/__tests__/multipleConfigs.ts @@ -34,3 +34,15 @@ test('multiple configs error can be suppressed by using --config', () => { ); expect(exitCode).toBe(0); }); +test('should works correctly when using different loaders in different projects', () => { + const {exitCode, stdout, stderr} = runJest( + 'multi-project-multiple-configs', + ['--projects', 'prj-1', 'prj-2'], + { + skipPkgJsonCheck: true, + }, + ); + expect(exitCode).toBe(0); + console.log(stdout); + console.log(stderr); +}); diff --git a/e2e/__tests__/readInitialOptions.test.ts b/e2e/__tests__/readInitialOptions.test.ts index ed595764fe66..a7a7af56f7a5 100644 --- a/e2e/__tests__/readInitialOptions.test.ts +++ b/e2e/__tests__/readInitialOptions.test.ts @@ -86,7 +86,15 @@ describe('readInitialOptions', () => { }); expect(configPath).toEqual(expectedConfigFile); }); - + test('should give an error when using unsupported loader', async () => { + const cwd = resolveFixture('ts-loader-config'); + const error: Error = await proxyReadInitialOptions(undefined, {cwd}).catch( + error => error, + ); + expect(error.message).toContain( + "Jest: 'ts-loader' is not a valid TypeScript configuration loader.", + ); + }); test('should give an error when there are multiple config files', async () => { const cwd = resolveFixture('multiple-config-files'); const error: Error = await proxyReadInitialOptions(undefined, {cwd}).catch( diff --git a/e2e/multi-project-multiple-configs/prj-1/__tests__/test.js b/e2e/multi-project-multiple-configs/prj-1/__tests__/test.js new file mode 100644 index 000000000000..fc395e3a0f4a --- /dev/null +++ b/e2e/multi-project-multiple-configs/prj-1/__tests__/test.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +test('dummy test', () => { + expect(1).toBe(1); +}); diff --git a/e2e/multi-project-multiple-configs/prj-1/jest.config.ts b/e2e/multi-project-multiple-configs/prj-1/jest.config.ts new file mode 100644 index 000000000000..791946f5d1b1 --- /dev/null +++ b/e2e/multi-project-multiple-configs/prj-1/jest.config.ts @@ -0,0 +1,16 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @jest-config-loader esbuild-register + */ + +import type {Config} from 'jest'; + +const config: Config = { + displayName: 'PROJECT 1', +}; + +export default config; diff --git a/e2e/multi-project-multiple-configs/prj-2/__tests__/test.js b/e2e/multi-project-multiple-configs/prj-2/__tests__/test.js new file mode 100644 index 000000000000..7496fad23c4d --- /dev/null +++ b/e2e/multi-project-multiple-configs/prj-2/__tests__/test.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +test('dummy test', () => { + expect(2).toBe(2); +}); diff --git a/e2e/multi-project-multiple-configs/prj-2/jest.config.ts b/e2e/multi-project-multiple-configs/prj-2/jest.config.ts new file mode 100644 index 000000000000..9b3050e06426 --- /dev/null +++ b/e2e/multi-project-multiple-configs/prj-2/jest.config.ts @@ -0,0 +1,16 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @jest-config-loader ts-node + */ + +import type {Config} from 'jest'; + +const config: Config = { + displayName: 'PROJECT 2', +}; + +export default config; diff --git a/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts b/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts index 30ae69caa645..e7d1a194cd0e 100644 --- a/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts +++ b/e2e/read-initial-options/ts-esbuild-register-config/jest.config.ts @@ -3,8 +3,10 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @jest-config-loader esbuild-register */ -/**@jest-config-loader esbuild-register */ + interface Config { jestConfig: string; } diff --git a/e2e/read-initial-options/ts-loader-config/jest.config.ts b/e2e/read-initial-options/ts-loader-config/jest.config.ts new file mode 100644 index 000000000000..69c3fea16f87 --- /dev/null +++ b/e2e/read-initial-options/ts-loader-config/jest.config.ts @@ -0,0 +1,16 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @jest-config-loader ts-loader + */ + +interface Config { + jestConfig: string; +} + +export default { + jestConfig: 'jest.config.ts', +} as Config; diff --git a/e2e/read-initial-options/ts-node-config/jest.config.ts b/e2e/read-initial-options/ts-node-config/jest.config.ts index a0c619389fe8..5a06f0ed51a0 100644 --- a/e2e/read-initial-options/ts-node-config/jest.config.ts +++ b/e2e/read-initial-options/ts-node-config/jest.config.ts @@ -3,8 +3,10 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @jest-config-loader ts-node */ -/**@jest-config-loader ts-node */ + interface Config { jestConfig: string; } From 8b70b9505fa86224abcc54787e11a699584e9567 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 23 Jul 2024 09:51:26 +0200 Subject: [PATCH 14/15] newlines --- e2e/__tests__/readInitialOptions.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/e2e/__tests__/readInitialOptions.test.ts b/e2e/__tests__/readInitialOptions.test.ts index a7a7af56f7a5..110c1e4b749c 100644 --- a/e2e/__tests__/readInitialOptions.test.ts +++ b/e2e/__tests__/readInitialOptions.test.ts @@ -46,6 +46,7 @@ describe('readInitialOptions', () => { expect(config).toEqual({jestConfig: 'jest.config.js', rootDir}); expect(configPath).toEqual(configFile); }); + test.each([ ['js-config', 'jest.config.js', 'jest.config.js'], ['pkg-config', 'package.json', 'package.json'], @@ -86,6 +87,7 @@ describe('readInitialOptions', () => { }); expect(configPath).toEqual(expectedConfigFile); }); + test('should give an error when using unsupported loader', async () => { const cwd = resolveFixture('ts-loader-config'); const error: Error = await proxyReadInitialOptions(undefined, {cwd}).catch( @@ -95,6 +97,7 @@ describe('readInitialOptions', () => { "Jest: 'ts-loader' is not a valid TypeScript configuration loader.", ); }); + test('should give an error when there are multiple config files', async () => { const cwd = resolveFixture('multiple-config-files'); const error: Error = await proxyReadInitialOptions(undefined, {cwd}).catch( From b05a5f5c6d2f95a54cb5c6dea7f80f8b39e8294a Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 23 Jul 2024 09:53:36 +0200 Subject: [PATCH 15/15] newlines --- e2e/__tests__/multipleConfigs.ts | 1 + e2e/typescript-config/modern-module-resolution/jest.config.ts | 2 -- packages/jest-config/src/readConfigFileAndSetRootDir.ts | 4 ++++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/e2e/__tests__/multipleConfigs.ts b/e2e/__tests__/multipleConfigs.ts index dee87c683665..57834de89501 100644 --- a/e2e/__tests__/multipleConfigs.ts +++ b/e2e/__tests__/multipleConfigs.ts @@ -34,6 +34,7 @@ test('multiple configs error can be suppressed by using --config', () => { ); expect(exitCode).toBe(0); }); + test('should works correctly when using different loaders in different projects', () => { const {exitCode, stdout, stderr} = runJest( 'multi-project-multiple-configs', diff --git a/e2e/typescript-config/modern-module-resolution/jest.config.ts b/e2e/typescript-config/modern-module-resolution/jest.config.ts index e321266aa49d..995821a204f5 100644 --- a/e2e/typescript-config/modern-module-resolution/jest.config.ts +++ b/e2e/typescript-config/modern-module-resolution/jest.config.ts @@ -3,8 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * @jest-config-loader esbuild-register */ const config = { diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index ddc4acb2de80..d232bcd37db1 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -135,6 +135,7 @@ async function registerTsLoader(loader: TsLoaderModule): Promise { // Register TypeScript compiler instance if (loader === 'ts-node') { const tsLoader = await import(/* webpackIgnore: true */ 'ts-node'); + return tsLoader.register({ compilerOptions: { module: 'CommonJS', @@ -149,7 +150,9 @@ async function registerTsLoader(loader: TsLoaderModule): Promise { const tsLoader = await import( /* webpackIgnore: true */ 'esbuild-register/dist/node' ); + let instance: {unregister: () => void} | undefined; + return { enabled: (bool: boolean) => { if (bool) { @@ -162,6 +165,7 @@ async function registerTsLoader(loader: TsLoaderModule): Promise { }, }; } + throw new Error( `Jest: '${loader}' is not a valid TypeScript configuration loader.`, );