Skip to content

Commit 7920922

Browse files
committed
fix(testing): bust require cache in jest plugin so configs reload (#22893)
(cherry picked from commit 595ea21)
1 parent 6ab9ae3 commit 7920922

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

packages/jest/src/plugins/plugin.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,36 @@ export const createNodes: CreateNodes<JestPluginOptions> = [
109109
},
110110
];
111111

112+
const jestValidatePath = dirname(
113+
require.resolve('jest-validate/package.json', {
114+
paths: [dirname(require.resolve('jest-config'))],
115+
})
116+
);
117+
112118
async function buildJestTargets(
113119
configFilePath: string,
114120
projectRoot: string,
115121
options: JestPluginOptions,
116122
context: CreateNodesContext
117123
): Promise<Pick<ProjectConfiguration, 'targets' | 'metadata'>> {
124+
const absConfigFilePath = resolve(context.workspaceRoot, configFilePath);
125+
126+
if (require.cache[absConfigFilePath]) {
127+
for (const k of Object.keys(require.cache)) {
128+
// Only delete the cache outside of jest-validate
129+
// jest-validate has a Symbol which is important for jest config validation which breaks if the require cache is broken
130+
if (relative(jestValidatePath, k).startsWith('../')) {
131+
delete require.cache[k];
132+
}
133+
}
134+
}
135+
118136
const config = await readConfig(
119137
{
120138
_: [],
121139
$0: undefined,
122140
},
123-
resolve(context.workspaceRoot, configFilePath)
141+
absConfigFilePath
124142
);
125143

126144
const namedInputs = getNamedInputs(projectRoot, context);

0 commit comments

Comments
 (0)