webpro-nl / knip

✂️ Find unused files, dependencies and exports in your JavaScript and TypeScript projects. Knip it before you ship it!
https://knip.dev
ISC License
6.42k stars 146 forks source link

False positive for dev dependency listed in jest config #603

Open mountEvarus opened 4 months ago

mountEvarus commented 4 months ago

I am running knip on a TS application with the following knip & jest files:

knip.json

{
  "$schema": "https://unpkg.com/knip@5/schema.json",
  "entry": ["src/server.ts", "src/commands/command.ts", "test/**/*.ts", "!test/mocks/**/*"],
  "paths": {
    "@/src/*": ["./src/*"],
    "@/test/*": ["./test/*"]
  },
  "project": ["src/**/*.ts", "test/**/*.ts"],
  "husky": {
    "config": [".husky/pre-commit"]
  },
  "prettier": {
    "config": [".prettierrc"]
  },
  "eslint": {
    "config": [".eslintrc"]
  },
  "jest": {
    "config": ["jest.config.js"]
  },
  "ignoreDependencies": ["@instana/collector", "@side/jest-runtime"]
}

jest.config.js

const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig.json');

module.exports = {
  transform: {
    '^.+\\.ts$': '@swc/jest',
  },
  clearMocks: true,
  runtime: '@side/jest-runtime',
  testEnvironment: 'node',
  testPathIgnorePatterns: ['node_modules', 'dist'],
  verbose: false,
  collectCoverage: true,
  coverageReporters: ['clover', 'json', 'lcov', 'text', 'text-summary'],
  globalSetup: '<rootDir>/test/jest/globalSetup.ts',
  globalTeardown: '<rootDir>/test/jest/globalTeardown.ts',
  setupFiles: ['<rootDir>/test/jest/setupTests.ts'],
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),
};

I am getting a false positive, claiming @side/jest-runtime is an unused dev dependency, but is is mentioned in the jest config under the runtime key

webpro commented 4 months ago

Thanks for the report, @mountEvarus.

This is the Jest plugin: https://github.com/webpro/knip/blob/main/packages/knip/src/plugins/jest/index.ts

Any chance you're up for a PR? No worries if not!