Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest configured setup scripts reporting as unused exports when --include-entry-exports is enabled #716

Closed
shawnmcknight opened this issue Jul 5, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@shawnmcknight
Copy link

I've created a reproduction for this issue at https://github.com/shawnmcknight/knip-jest-entry-exports.

Jest allows for configuration properties globalSetup, globalTeardown, setupFiles, and setupFilesAfterEnv (possibly others, but these are the ones I am aware of and use). The jest plugin for knip will properly detect these files referenced in the jest config as being used except when --include-entry-exports is enabled. In my reproduction I've added a module for each of these configurable jest properties and referenced them in the jest config:

/** @type {import('jest').Config} */
const jestConfig = {
  globalSetup: "./test/globalSetup.ts",
  globalTeardown: "./test/globalTeardown.ts",
  setupFiles: ["./test/setupFiles.ts"],
  setupFilesAfterEnv: ["./test/setupFilesAfterEnv.ts"],
};

module.exports = jestConfig;

When running knip with --include-entry-exports off, knip reports no errors in the reproduction. However, if --include-entry-exports is enabled then knip is reporting:
image

My interpretation of the includeEntryExports documentation is that plugins shouldn't behave differently for entry files:

If enabled, Knip will report unused exports in entry source files and scripts such as those referenced in package.json. But not in entry and configuration files as configured by plugins, such as next.config.js or src/routes/+page.svelte.

Please let me know if I can provide anything else to assist with this.

@shawnmcknight shawnmcknight added the bug Something isn't working label Jul 5, 2024
@webpro webpro closed this as completed in 1cd4bd8 Jul 8, 2024
@webpro
Copy link
Collaborator

webpro commented Jul 8, 2024

🚀 This issue has been resolved in v5.24.2. See Release 5.24.2 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

@webpro
Copy link
Collaborator

webpro commented Jul 8, 2024

Thanks @shawnmcknight! Plugins don't perfectly always use toEntryPattern properly everywhere yet, but at least the ones you've shown are fixed now.

@shawnmcknight
Copy link
Author

@webpro Thanks for taking care of this. The originally reported issue is now fixed, but there has been a regression elsewhere. I can log a separate issue and try to create a reproduction if it helps -- just let me know.

In our jest.config.js for setupFiles and setupFilesAfterEnv, there are some references to a module from an internal workspace:

const jestConfig = {
...
  setupFiles: ['@storis/app_common.test/setup.ts'],
  setupFilesAfterEnv: ['@storis/app_common.test/setupAfterEnv.ts'],
...
};

module.exports = jestConfig;

The @storis/app_common.test package is a reference to another workspace in the monorepo and is declared in package.json as a dev dependency. With this latest update, that dependency is being reported as an unused dev dependency:

image

Prior to this latest change that was not being reported as an unused dependency. If you need more information let me know and I'll try to work up a reproduction. Thanks!

@webpro
Copy link
Collaborator

webpro commented Jul 8, 2024

The day has come..

// TODO: Promote to something more generic, other plugins may like it too

Will fix :)

@webpro
Copy link
Collaborator

webpro commented Jul 8, 2024

Another fix is in the latest & greatest.

@shawnmcknight
Copy link
Author

Another fix is in the latest & greatest.

That was really fast! Just tried it out and everything looking good! Thanks! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants