10000 Merge pull request #2761 from microsoft/octogonz/heft-jest-regressions · clouedoc/rushstack@e0ccf8e · GitHub
[go: up one dir, main page]

Skip to content

Commit e0ccf8e

Browse files
authored
Merge pull request microsoft#2761 from microsoft/octogonz/heft-jest-regressions
[heft] Fix some regressions involving jest.config.json resolution
2 parents 3fa91f9 + 74838dd commit e0ccf8e

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/heft-jest-plugin",
5+
"comment": "Fix a regression where \"testEnvironment\" did not resolve correctly (GitHub #2745)",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@rushstack/heft-jest-plugin",
10+
"email": "4673363+octogonz@users.noreply.github.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/heft-jest-plugin",
5+
"comment": "Enable \"@rushstack/heft-jest-plugin/lib/exports/jest-global-setup.js\" to resolve for rigged projects that don't have a direct dependency on that package",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@rushstack/heft-jest-plugin",
10+
"email": "4673363+octogonz@users.noreply.github.com"
11+
}

heft-plugins/heft-jest-plugin/src/JestPlugin.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export class JestPlugin implements IHeftPlugin<IJestPluginOptions> {
4747
public readonly pluginName: string = PLUGIN_NAME;
4848
public readonly optionsSchema: JsonSchema = JsonSchema.fromFile(PLUGIN_SCHEMA_PATH);
4949

50+
private static _ownPackageFolder: string = path.resolve(__dirname, '..');
51+
5052
/**
5153
* Returns the loader for the `config/api-extractor-task.json` config file.
5254
*/
@@ -80,8 +82,33 @@ export class JestPlugin implements IHeftPlugin<IJestPluginOptions> {
8082
// that we provide to Jest. Resolve if we modified since paths containing <rootDir> should be absolute.
8183
const nodeResolveMetadata: IJsonPathMetadata = {
8284
preresolve: (jsonPath: string) => {
83-
const newJsonPath: string = jsonPath.replace(/<rootDir>/g, buildFolder);
84-
return jsonPath === newJsonPath ? jsonPath : path.resolve(newJsonPath);
85+
// Compare with replaceRootDirInPath() from here:
86+
// https://github.com/facebook/jest/blob/5f4dd187d89070d07617444186684c20d9213031/packages/jest-config/src/utils.ts#L58
87+
const ROOTDIR_TOKEN: string = '<rootDir>';
88+
89+
// Example: <rootDir>/path/to/file.js
90+
if (jsonPath.startsWith(ROOTDIR_TOKEN)) {
91+
const restOfPath: string = path.normalize('./' + jsonPath.substr(ROOTDIR_TOKEN.length));
92+
return path.resolve(buildFolder, restOfPath);
93+
}
94+
95+
// The normal PathResolutionMethod.NodeResolve will generally not be able to find @rushstack/heft-jest-plugin
96+
// from a project that is using a rig. Since it is important, and it is our own package, we resolve it
97+
// manually as a special case.
98+
const PLUGIN_PACKAGE_NAME: string = '@rushstack/heft-jest-plugin';
99+
100+
// Example: @rushstack/heft-jest-plugin
101+
if (jsonPath === PLUGIN_PACKAGE_NAME) {
102+
return JestPlugin._ownPackageFolder;
103+
}
104+
105+
// Example: @rushstack/heft-jest-plugin/path/to/file.js
106+
if (jsonPath.startsWith(PLUGIN_PACKAGE_NAME)) {
107+
const restOfPath: string = path.normalize('./' + jsonPath.substr(PLUGIN_PACKAGE_NAME.length));
108+
return path.join(JestPlugin._ownPackageFolder, restOfPath);
109+
}
110+
111+
return jsonPath;
85112
},
86113
pathResolutionMethod: PathResolutionMethod.NodeResolve
87114
};
@@ -114,7 +141,8 @@ export class JestPlugin implements IHeftPlugin<IJestPluginOptions> {
114141
'$.resolver': nodeResolveMetadata,
115142
'$.runner': nodeResolveMetadata,
116143
'$.snapshotResolver': nodeResolveMetadata,
117-
'$.testEnvironment': nodeResolveMetadata,
144+
// This is a name like "jsdom" that gets mapped into a package name like "jest-environment-jsdom"
145+
// '$.testEnvironment': string
118146
'$.testResultsProcessor': nodeResolveMetadata,
119147
'$.testRunner': nodeResolveMetadata,
120148
'$.testSequencer': nodeResolveMetadata,

0 commit comments

Comments
 (0)
0