Skip to content

Commit 9730d02

Browse files
authoredNov 13, 2024··
fix: Fix incorrect rootPath resolution in jest plugin (#833)
1 parent 403d257 commit 9730d02

File tree

7 files changed

+65
-3
lines changed

7 files changed

+65
-3
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
displayName: 'dev',
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default {
2+
"jest": {
3+
"config": [
4+
"jest.config.js",
5+
"project1/jest.config.js",
6+
"package.json"
7+
],
8+
"entry": [
9+
"**/__tests__/**/*.[jt]s?(x)",
10+
"**/?(*.)+(spec|test).[jt]s?(x)"
11+
]
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@fixtures/jest2",
3+
"scripts": {},
4+
"devDependencies": {
5+
"jest": "*",
6+
"@testing-library/jest-dom": "*"
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
rootDir: path.join(__dirname, '../'),
5+
displayName: 'project1',
6+
setupFilesAfterEnv: ['<rootDir>/project1/setupFiles/setup.js'],
7+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/jest-dom/extend-expect';

‎packages/knip/src/plugins/jest/index.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,15 @@ const resolveDependencies = async (config: JestInitialOptions, options: PluginOp
112112
const resolveEntryPaths: ResolveEntryPaths<JestConfig> = async (localConfig, options) => {
113113
const { configFileDir } = options;
114114
if (typeof localConfig === 'function') localConfig = await localConfig();
115-
const rootDir = localConfig.rootDir ? join(configFileDir, localConfig.rootDir) : configFileDir;
115+
const rootDir = localConfig.rootDir ?? configFileDir;
116116
const replaceRootDir = (name: string) => name.replace(/<rootDir>/, rootDir);
117117
return (localConfig.testMatch ?? []).map(replaceRootDir).map(toEntry);
118118
};
119119

120120
const resolveConfig: ResolveConfig<JestConfig> = async (localConfig, options) => {
121121
const { configFileDir } = options;
122122
if (typeof localConfig === 'function') localConfig = await localConfig();
123-
const rootDir = localConfig.rootDir ? join(configFileDir, localConfig.rootDir) : configFileDir;
124-
123+
const rootDir = localConfig.rootDir ?? configFileDir;
125124
const replaceRootDir = (name: string) => name.replace(/<rootDir>/, rootDir);
126125

127126
const inputs = await resolveDependencies(localConfig, options);
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { test } from 'bun:test';
2+
import assert from 'node:assert/strict';
3+
import { main } from '../../src/index.js';
4+
import { join, resolve } from '../../src/util/path.js';
5+
import baseArguments from '../helpers/baseArguments.js';
6+
import baseCounters from '../helpers/baseCounters.js';
7+
8+
const cwd = resolve('fixtures/plugins/jest2');
9+
10+
test('Find dependencies with the Jest plugin', async () => {
11+
const { issues, counters } = await main({
12+
...baseArguments,
13+
cwd,
14+
});
15+
16+
assert(issues.devDependencies['package.json']['jest']);
17+
18+
// Correctly identifies setup file in a non-root jest.config.js which uses
19+
// <rootDir> to reference the root directory.
20+
assert(!issues.files.has(join(cwd, 'project1/setupFiles/setup.js')));
21+
22+
assert.deepEqual(counters, {
23+
...baseCounters,
24+
files: 0,
25+
devDependencies: 1,
26+
unlisted: 0,
27+
unresolved: 0,
28+
processed: 4,
29+
total: 4,
30+
});
31+
});

0 commit comments

Comments
 (0)
Please sign in to comment.