diff --git a/packages/knip/fixtures/plugins/jest/junitProperties.js b/packages/knip/fixtures/plugins/jest/junitProperties.js deleted file mode 100644 index 7c6d6c73d..000000000 --- a/packages/knip/fixtures/plugins/jest/junitProperties.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = {} \ No newline at end of file diff --git a/packages/knip/fixtures/plugins/jest2/junitTestCaseProperties.js b/packages/knip/fixtures/plugins/jest2/junitTestCaseProperties.js deleted file mode 100644 index 7c6d6c73d..000000000 --- a/packages/knip/fixtures/plugins/jest2/junitTestCaseProperties.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = {} \ No newline at end of file diff --git a/packages/knip/fixtures/plugins/jest3/index.spec.ts b/packages/knip/fixtures/plugins/jest3/index.spec.ts new file mode 100644 index 000000000..d73a9586a --- /dev/null +++ b/packages/knip/fixtures/plugins/jest3/index.spec.ts @@ -0,0 +1 @@ +import 'jest'; diff --git a/packages/knip/fixtures/plugins/jest3/jest.config.js b/packages/knip/fixtures/plugins/jest3/jest.config.js new file mode 100644 index 000000000..252c44240 --- /dev/null +++ b/packages/knip/fixtures/plugins/jest3/jest.config.js @@ -0,0 +1,13 @@ +module.exports = { + testEnvironment: 'node', + reporters: [ + 'default', + [ + 'jest-junit', + { + outputDirectory: 'reports/junit', + outputName: 'junit.xml', + }, + ], + ], +}; diff --git a/packages/knip/fixtures/plugins/jest3/package.json b/packages/knip/fixtures/plugins/jest3/package.json new file mode 100644 index 000000000..b72145e05 --- /dev/null +++ b/packages/knip/fixtures/plugins/jest3/package.json @@ -0,0 +1,8 @@ +{ + "name": "@fixtures/jest3", + "scripts": {}, + "devDependencies": { + "jest": "*", + "jest-junit": "*" + } +} diff --git a/packages/knip/src/plugins/jest/helpers.ts b/packages/knip/src/plugins/jest/helpers.ts index 81984e14e..f1e50d569 100644 --- a/packages/knip/src/plugins/jest/helpers.ts +++ b/packages/knip/src/plugins/jest/helpers.ts @@ -32,16 +32,17 @@ export const getReportersDependencies = (config: JestInitialOptions, options: Pl testSuitePropertiesDirectory, } = reporter[1]; - const testCaseFileName = getStringPropOrFallback(testCasePropertiesFile, 'junitProperties.js'); - const testCaseDirectory = getStringPropOrFallback(testCasePropertiesDirectory, options.rootCwd); - const testCaseFilePath = join(testCaseDirectory, testCaseFileName); - - const testSuiteFileName = getStringPropOrFallback(testSuitePropertiesFile, 'junitTestCaseProperties.js'); - const testSuiteDirectory = getStringPropOrFallback(testSuitePropertiesDirectory, options.rootCwd); - const testSuiteFilePath = join(testSuiteDirectory, testSuiteFileName); - - jUnitReporterDeps.push(testCaseFilePath); - jUnitReporterDeps.push(testSuiteFilePath); + if (testCasePropertiesFile) { + const fileName = getStringPropOrFallback(testCasePropertiesFile, 'junitProperties.js'); + const dir = getStringPropOrFallback(testCasePropertiesDirectory, options.rootCwd); + jUnitReporterDeps.push(join(dir, fileName)); + } + + if (testSuitePropertiesFile) { + const fileName = getStringPropOrFallback(testSuitePropertiesFile, 'junitTestCaseProperties.js'); + const dir = getStringPropOrFallback(testSuitePropertiesDirectory, options.rootCwd); + jUnitReporterDeps.push(join(dir, fileName)); + } } } diff --git a/packages/knip/test/plugins/jest.test.ts b/packages/knip/test/plugins/jest.test.ts index 3a036eba4..5274b3657 100644 --- a/packages/knip/test/plugins/jest.test.ts +++ b/packages/knip/test/plugins/jest.test.ts @@ -31,7 +31,7 @@ test('Find dependencies with the Jest plugin', async () => { devDependencies: 1, unlisted: 3, unresolved: 9, - processed: 8, - total: 8, + processed: 7, + total: 7, }); }); diff --git a/packages/knip/test/plugins/jest2.test.ts b/packages/knip/test/plugins/jest2.test.ts index 8819d3bf8..c0a3cdc21 100644 --- a/packages/knip/test/plugins/jest2.test.ts +++ b/packages/knip/test/plugins/jest2.test.ts @@ -1,13 +1,13 @@ import { test } from 'bun:test'; import assert from 'node:assert/strict'; import { main } from '../../src/index.js'; -import { join, resolve } from '../../src/util/path.js'; +import { resolve } from '../../src/util/path.js'; import baseArguments from '../helpers/baseArguments.js'; import baseCounters from '../helpers/baseCounters.js'; const cwd = resolve('fixtures/plugins/jest2'); -test('Find dependencies with the Jest plugin', async () => { +test('Find dependencies with the Jest plugin (2)', async () => { const { issues, counters } = await main({ ...baseArguments, cwd, @@ -15,20 +15,12 @@ test('Find dependencies with the Jest plugin', async () => { assert(issues.devDependencies['package.json']['jest']); - // Correctly identifies setup file in a non-root jest.config.js which uses - // 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: 7, - total: 7, + processed: 6, + total: 6, }); }); diff --git a/packages/knip/test/plugins/jest3.test.ts b/packages/knip/test/plugins/jest3.test.ts new file mode 100644 index 000000000..ec540dfb1 --- /dev/null +++ b/packages/knip/test/plugins/jest3.test.ts @@ -0,0 +1,21 @@ +import { test } from 'bun:test'; +import assert from 'node:assert/strict'; +import { main } from '../../src/index.js'; +import { resolve } from '../../src/util/path.js'; +import baseArguments from '../helpers/baseArguments.js'; +import baseCounters from '../helpers/baseCounters.js'; + +const cwd = resolve('fixtures/plugins/jest3'); + +test('Find dependencies with the Jest plugin (3)', async () => { + const { counters } = await main({ + ...baseArguments, + cwd, + }); + + assert.deepEqual(counters, { + ...baseCounters, + processed: 2, + total: 2, + }); +});