@@ -47,6 +47,8 @@ export class JestPlugin implements IHeftPlugin<IJestPluginOptions> {
47
47
public readonly pluginName : string = PLUGIN_NAME ;
48
48
public readonly optionsSchema : JsonSchema = JsonSchema . fromFile ( PLUGIN_SCHEMA_PATH ) ;
49
49
50
+ private static _ownPackageFolder : string = path . resolve ( __dirname , '..' ) ;
51
+
50
52
/**
51
53
* Returns the loader for the `config/api-extractor-task.json` config file.
52
54
*/
@@ -80,8 +82,33 @@ export class JestPlugin implements IHeftPlugin<IJestPluginOptions> {
80
82
// that we provide to Jest. Resolve if we modified since paths containing <rootDir> should be absolute.
81
83
const nodeResolveMetadata : IJsonPathMetadata = {
82
84
preresolve : ( jsonPath : string ) => {
83
- const newJsonPath : string = jsonPath . replace ( / < r o o t D i r > / 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 ;
85
112
} ,
86
113
pathResolutionMethod : PathResolutionMethod . NodeResolve
87
114
} ;
@@ -114,7 +141,8 @@ export class JestPlugin implements IHeftPlugin<IJestPluginOptions> {
114
141
'$.resolver' : nodeResolveMetadata ,
115
142
'$.runner' : nodeResolveMetadata ,
116
143
'$.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
118
146
'$.testResultsProcessor' : nodeResolveMetadata ,
119
147
'$.testRunner' : nodeResolveMetadata ,
120
148
'$.testSequencer' : nodeResolveMetadata ,
0 commit comments