From 839f6794353a959d2ae520e840c157e642f53f26 Mon Sep 17 00:00:00 2001 From: Tryggvi Gylfason Date: Wed, 13 Nov 2024 17:08:22 +0100 Subject: [PATCH 1/2] jest: support custom testEnvironment --- packages/knip/fixtures/plugins/jest2/jest.config.js | 2 ++ packages/knip/fixtures/plugins/jest2/jest.environment.js | 1 + packages/knip/src/plugins/jest/index.ts | 9 ++++++++- packages/knip/test/plugins/jest2.test.ts | 7 +++++-- 4 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 packages/knip/fixtures/plugins/jest2/jest.environment.js diff --git a/packages/knip/fixtures/plugins/jest2/jest.config.js b/packages/knip/fixtures/plugins/jest2/jest.config.js index cd556eaef..3669705e0 100644 --- a/packages/knip/fixtures/plugins/jest2/jest.config.js +++ b/packages/knip/fixtures/plugins/jest2/jest.config.js @@ -1,3 +1,5 @@ module.exports = { displayName: 'dev', + testEnvironment: + '/jest.environment.js', }; diff --git a/packages/knip/fixtures/plugins/jest2/jest.environment.js b/packages/knip/fixtures/plugins/jest2/jest.environment.js new file mode 100644 index 000000000..f053ebf79 --- /dev/null +++ b/packages/knip/fixtures/plugins/jest2/jest.environment.js @@ -0,0 +1 @@ +module.exports = {}; diff --git a/packages/knip/src/plugins/jest/index.ts b/packages/knip/src/plugins/jest/index.ts index 583594ef0..f23a100d9 100644 --- a/packages/knip/src/plugins/jest/index.ts +++ b/packages/knip/src/plugins/jest/index.ts @@ -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 = []; + 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 diff --git a/packages/knip/test/plugins/jest2.test.ts b/packages/knip/test/plugins/jest2.test.ts index 664042977..e85de0323 100644 --- a/packages/knip/test/plugins/jest2.test.ts +++ b/packages/knip/test/plugins/jest2.test.ts @@ -19,13 +19,16 @@ test('Find dependencies with the Jest plugin', async () => { // to reference the root directory. assert(!issues.files.has(join(cwd, 'project1/setupFiles/setup.js'))); + // Correctly identifies a local `testEnvironment` file. + 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, }); }); From b80e392ae1433677a9331e92c2a09837323623e1 Mon Sep 17 00:00:00 2001 From: Tryggvi Gylfason Date: Wed, 13 Nov 2024 20:43:41 +0100 Subject: [PATCH 2/2] resolve environments in a single expression --- packages/knip/src/plugins/jest/index.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/knip/src/plugins/jest/index.ts b/packages/knip/src/plugins/jest/index.ts index f23a100d9..2bfe09501 100644 --- a/packages/knip/src/plugins/jest/index.ts +++ b/packages/knip/src/plugins/jest/index.ts @@ -58,14 +58,12 @@ const resolveDependencies = async (config: JestInitialOptions, options: PluginOp const runner = config.runner ? [config.runner] : []; const runtime = config.runtime && config.runtime !== 'jest-circus' ? [config.runtime] : []; - - let environments: Array = []; - if (config.testEnvironment === 'jsdom') { - environments = ['jest-environment-jsdom']; - } else if (config.testEnvironment) { - environments = [config.testEnvironment]; - } - + const environments = + config.testEnvironment === 'jsdom' + ? ['jest-environment-jsdom'] + : config.testEnvironment + ? [config.testEnvironment] + : []; const resolvers = config.resolver ? [config.resolver] : []; const reporters = config.reporters ? config.reporters