-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Changes from 5 commits
2b1a7ff
8dbe6fd
93c0a87
da0c199
2d8deb5
8e00bc0
0bcaacd
6ad8d3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,7 @@ const loadTSConfigFile = async ( | |
configPath: Config.Path, | ||
): Promise<Config.InitialOptions> => { | ||
let registerer: Service; | ||
let tsNode; | ||
|
||
// Register TypeScript compiler instance | ||
try { | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The requiring of I don't think we need to be checking that |
||
} | ||
|
||
throw e; | ||
} | ||
|
||
registerer.enabled(true); | ||
if (tsNode) { | ||
registerer.enabled(true); | ||
} | ||
|
||
let configObject = interopRequireDefault(require(configPath)).default; | ||
|
||
|
@@ -111,7 +125,9 @@ const loadTSConfigFile = async ( | |
configObject = await configObject(); | ||
} | ||
|
||
registerer.enabled(false); | ||
if (tsNode) { | ||
registerer.enabled(false); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I open an issue on |
||
|
||
return configObject; | ||
}; |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
specifiesesbuild
as a peerDependency so we'll still need the user to installesbuild