Skip to content

Commit

Permalink
jest: support custom testEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
tryggvigy committed Nov 13, 2024
1 parent 9730d02 commit 021d2c5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/knip/fixtures/plugins/jest2/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module.exports = {
displayName: 'dev',
testEnvironment:
'<rootDir>/jest.environment.js',
};
1 change: 1 addition & 0 deletions packages/knip/fixtures/plugins/jest2/jest.environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
9 changes: 8 additions & 1 deletion packages/knip/src/plugins/jest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ const resolveDependencies = async (config: JestInitialOptions, options: PluginOp

const runner = config.runner ? [config.runner] : [];
const runtime = config.runtime && config.runtime !== 'jest-circus' ? [config.runtime] : [];
const environments = config.testEnvironment === 'jsdom' ? ['jest-environment-jsdom'] : [];

let environments: Array<string> = [];
if (config.testEnvironment === 'jsdom') {
environments = ['jest-environment-jsdom'];
} else if (config.testEnvironment) {
environments = [config.testEnvironment];
}

const resolvers = config.resolver ? [config.resolver] : [];
const reporters = config.reporters
? config.reporters
Expand Down
8 changes: 6 additions & 2 deletions packages/knip/test/plugins/jest2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ test('Find dependencies with the Jest plugin', async () => {
// <rootDir> to reference the root directory.
assert(!issues.files.has(join(cwd, 'project1/setupFiles/setup.js')));

// Correctly identifies a local environment file which uses
// <rootDir> to reference the root directory.
assert(!issues.files.has(join(cwd, 'jest.environment.js')));

assert.deepEqual(counters, {
...baseCounters,
files: 0,
devDependencies: 1,
unlisted: 0,
unresolved: 0,
processed: 4,
total: 4,
processed: 5,
total: 5,
});
});

0 comments on commit 021d2c5

Please sign in to comment.