Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add esbuild resolver for jest.config.ts #12041

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- `[jest-config]` Add esbuild resolver for `jest.config.ts` as a `ts-node` alternative ([#12041](https://github.com/facebook/jest/pull/12041))
- `[jest-core]` Add support for `testResultsProcessor` written in ESM ([#12006](https://github.com/facebook/jest/pull/12006))
- `[jest-diff, pretty-format]` Add `compareKeys` option for custom sorting of object keys ([#11992](https://github.com/facebook/jest/pull/11992))

Expand Down
5 changes: 5 additions & 0 deletions packages/jest-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
"./package.json": "./package.json"
},
"peerDependencies": {
"esbuild": ">=0.13.12",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be replaced with esbuild-register, since that is what Jest actually needs to do the work.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, however esbuild-register specifies esbuild as a peerDependency so we'll still need the user to install esbuild

"ts-node": ">=9.0.0"
},
"peerDependenciesMeta": {
"esbuild": {
"optional": true
},
"ts-node": {
"optional": true
}
Expand All @@ -29,6 +33,7 @@
"chalk": "^4.0.0",
"ci-info": "^3.2.0",
"deepmerge": "^4.2.2",
"esbuild-register": "^3.0.0",
"glob": "^7.1.1",
"graceful-fs": "^4.2.4",
"jest-circus": "^27.3.1",
Expand Down
26 changes: 21 additions & 5 deletions packages/jest-config/src/readConfigFileAndSetRootDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const loadTSConfigFile = async (
configPath: Config.Path,
): Promise<Config.InitialOptions> => {
let registerer: Service;
let tsNode;

// Register TypeScript compiler instance
try {
Expand All @@ -92,17 +93,30 @@ const loadTSConfigFile = async (
module: 'CommonJS',
},
});
tsNode = true;
} catch (e: any) {
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}`,
);
try {
require('esbuild');
} catch (error: any) {
if (error.code === 'MODULE_NOT_FOUND') {
throw new Error(
`Jest: 'ts-node' or 'esbuild' is required for the TypeScript configuration files. Make sure it is installed\nError: ${e.message}`,
);
}

throw error;
}

registerer = require('esbuild-register').register();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requiring of esbuild-register should be done in this try/catch, and the error message should be updated to say esbuild-register instead of esbuild.

I don't think we need to be checking that esbuild itself is installed as it's a peer dependency of esbuild-register so that package should be handling checking if it's available (+ package managers will say that its needed due to being listed as a peer dependency)

}

throw e;
}

registerer.enabled(true);
if (tsNode) {
registerer.enabled(true);
}

let configObject = interopRequireDefault(require(configPath)).default;

Expand All @@ -111,7 +125,9 @@ const loadTSConfigFile = async (
configObject = await configObject();
}

registerer.enabled(false);
if (tsNode) {
registerer.enabled(false);
}
Copy link
Contributor

@G-Rath G-Rath Nov 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The registerer needs to be disabled before this function returns because otherwise it will continue to run on unrelated files which will mess with Jest's transformation abilities & expectations, and cause a bunch of weird bugs (e.g. Jest can end up getting js versions of ts files, which'll not get mapped back to their sources when showing failure messages & stack traces).

Sadly it looks like esbuild-register doesn't support being enabled and disabled, so that will a block - however it shouldn't be hard to implement and if it's done with the same API as ts-node we should be able to leave these enabled calls as is.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I open an issue on esbuild-register?


return configObject;
};
22 changes: 22 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9113,6 +9113,17 @@ __metadata:
languageName: node
linkType: hard

"esbuild-register@npm:^3.0.0":
version: 3.0.0
resolution: "esbuild-register@npm:3.0.0"
dependencies:
jsonc-parser: ^3.0.0
peerDependencies:
esbuild: ">=0.12 <1"
checksum: 9d60f690f9b1ed6b08fdca1d9d40ac464d9580bfec439be9cc5df8a5cab565e0ded8671c07726f35fea0299f94d69ea3e1cd6b46f762c6783b93c1a58fae7503
languageName: node
linkType: hard

"escalade@npm:^3.0.2, escalade@npm:^3.1.1":
version: 3.1.1
resolution: "escalade@npm:3.1.1"
Expand Down Expand Up @@ -12642,6 +12653,7 @@ fsevents@^1.2.7:
chalk: ^4.0.0
ci-info: ^3.2.0
deepmerge: ^4.2.2
esbuild-register: ^3.0.0
glob: ^7.1.1
graceful-fs: ^4.2.4
jest-circus: ^27.3.1
Expand All @@ -12662,8 +12674,11 @@ fsevents@^1.2.7:
ts-node: ^9.0.0
typescript: ^4.0.3
peerDependencies:
esbuild: ">=0.13.12"
ts-node: ">=9.0.0"
peerDependenciesMeta:
esbuild:
optional: true
ts-node:
optional: true
languageName: unknown
Expand Down Expand Up @@ -13602,6 +13617,13 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"jsonc-parser@npm:^3.0.0":
version: 3.0.0
resolution: "jsonc-parser@npm:3.0.0"
checksum: 36b9080a7e25f5052c7d551109c4ead564f3102d7ca0c5ee4a2f7debc18b2cd31896e0e369cfd1442d9d2d71fc1defaf094ba885bc9b43a6a3f2b3a9083fd2cb
languageName: node
linkType: hard

"jsonfile@npm:^2.1.0":
version: 2.4.0
resolution: "jsonfile@npm:2.4.0"
Expand Down