jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.19k stars 6.46k forks source link

[Bug]: Validation warnings for options like preset or coverage directory #13576

Open MichaelBorde opened 1 year ago

MichaelBorde commented 1 year ago

Version

29.3.0

Steps to reproduce

Use a jest.config.ts like:

export default {
  preset: './jest.preset.js',
  coverageDirectory: './coverage',
};

Expected behavior

Run tests using ./node_modules/.bin/jest without any warnings.

Actual behavior

● Validation Warning:

  Unknown option "preset" with value "./jest.preset.js" was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration

● Validation Warning:

  Unknown option "coverageDirectory" with value "./coverage" was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration

Additional context

Might be related to the missing default option for preset or coverage in packages/jest-config/src/ValidConfig.ts.

Environment

System:
    OS: macOS 13.0
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 16.18.0 - ~/.volta/tools/image/node/16.18.0/bin/node
    Yarn: 1.22.19 - ~/.volta/tools/image/yarn/1.22.19/bin/yarn
    npm: 8.19.2 - ~/.volta/tools/image/node/16.18.0/bin/npm
  npmPackages:
    jest: ^29.3.0 => 29.3.0
drewzh commented 1 year ago

jest.preset.js...

const nxPreset = require("@nx/jest/preset").default;

const globalConf = {
  collectCoverage: true,
  coverageDirectory: `${process.env.NX_WORKSPACE_ROOT}/coverage/${process.env["NX_TASK_TARGET_PROJECT"]}`,
};

module.exports = { ...nxPreset, ...globalConf };
AndreyTheWeb commented 1 year ago

Still throwing errors, but the config itself works fine

● Validation Warning: Unknown option "collectCoverage" with value true was found. This is probably a typing mistake. Fixing it will remove this message. Configuration Documentation: https://jestjs.io/docs/configuration ● Validation Warning: Unknown option "coverageDirectory" with value "../../.cache/coverage/apps/hooks" was found. This is probably a typing mistake. Fixing it will remove this message. Configuration Documentation: https://jestjs.io/docs/configuration ● Validation Warning: Unknown option "collectCoverageFrom" with value ["src/*/.{js,ts,jsx,tsx}", "!/index.(js|ts|jsx|tsx)", "!/types.(js|ts|jsx|tsx)"] was found. This is probably a typing mistake. Fixing it will remove this message. Configuration Documentation: https://jestjs.io/docs/configuration ● Validation Warning: Unknown option "coverageReporters" with value ["html", "json"] was found. This is probably a typing mistake. Fixing it will remove this message. Configuration Documentation: https://jestjs.io/docs/configuration

xobotyi commented 1 year ago

I had Unknown option "default" with value ... issue with my ESM ("type": "module") project.

This issue occurs due to the way jest checks if imported object is ES module

if (obj.__esModule) return obj:
else return { default: obj }

To fix t you have two options:

SPokhriyal123 commented 1 year ago

Hi I am also seeing the similar issue while try to run : npx jest --coverage RelativePath image

I have following versions installed:

Can someone please help me here?

wSedlacek commented 1 year ago

Seems to be fixed in 29.6.1 🎉

webark commented 1 year ago

Appears it might have been #14054

ghost commented 1 year ago

Seems to be fixed in 29.6.1 🎉

nope, still happening

CleanShot 2023-07-17 at 12 56 11@2x
wSedlacek commented 1 year ago

Okay, so SOME options appear to be fixed, the one I was using coverageDirectory is fixed.

kmaid commented 1 year ago

Coverage configuration warnings were removed for me 🎉. All of my remaining warnings are for watchPlugins

khause commented 1 year ago

Still getting this warning for the reporters option even after upgrading to version 29.6.1. Upgrading did fix it for coverageDirectory though.

woppa684 commented 1 year ago

That's correct, only a PR was made for 2 of the affected options ... See https://github.com/jestjs/jest/pull/14180

lalitkumarbTR commented 1 year ago

I upgraded to jest@29.6.2 but I still see warnings for "collectCoverage" and "coverageReporters":

● Validation Warning:
  Unknown option "collectCoverage" with value true was found.
  This is probably a typing mistake. Fixing it will remove this message.
  Configuration Documentation:
  https://jestjs.io/docs/configuration
● Validation Warning:
  Unknown option "coverageReporters" with value ["text", "cobertura", "lcov"] was found.
  This is probably a typing mistake. Fixing it will remove this message.
  Configuration Documentation:
  https://jestjs.io/docs/configuration
markuswin-elkjop commented 1 year ago

I get those warnings with 29.6.2


● Validation Warning:

  Unknown option "collectCoverage" with value false was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration

● Validation Warning:

  Unknown option "projects" with value ["<rootDir>/src"] was found.     
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration

● Validation Warning:

  Unknown option "reporters" with value ["default"] was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration

● Validation Warning:

  Unknown option "silent" with value true was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration

● Validation Warning:

  Unknown option "testSequencer" with value "./jest.sequencer.js" was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration```
kvart714 commented 1 year ago

My VS Code extension doesn't work for the same reason and I can't run tests through the UI of VS Code: image

vbuch commented 1 year ago

@kvart714 , that's rather a bug in the VS Code extension not being able to go around the warning.

kvart714 commented 1 year ago

Yes I understand it. This extension hasn't been supported for 2 years now. But unfortunately it is the best one for VS Code for me. The most popular extension has many other problems. Anyway these warnings are wrong and I just wrote one more related case

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

webark commented 1 year ago

I definitely don't think this should be closed without some kind of resolution or direction.

mrazauskas commented 1 year ago

It is just right time to fix this. @SimenB is landing breaking changes and preparing Jest 30 release.

SimenB commented 1 year ago

I wonder if we should take the opportunity to go all in on generating a json schema and just use ajv or something (like eslint does) to validate?

Ref https://github.com/jestjs/jest/issues/11963

I haven't done anything more since landing the basics: https://github.com/jestjs/jest/blob/main/packages/jest-schemas/src/index.ts

andrew-productiv commented 1 year ago

Just to be clear - reporters is a valid field in global config and project-specific configs, right? For that option (at least), the issue here is the spurious validation warnings, not that the functionality itself is (or should/will be) unsupported?

madcapnmckay commented 1 year ago

I'm facing the same issue. Is there any documentation about what is valid in project vs global config? I see that @jest/types exposes a ProjectConfig and GlobalConfig type but the jest package does not. Only InitialOptions from the Config namespace is exposed on the jest package. If i try and require the types from jest I get an error pushing me to use jest instead.

To further complicate this, the docs state

With the projects option enabled, Jest will copy the root-level configuration options to each individual child configuration during the test run, resolving its values in the child's context. This means that string tokens like will point to the child's root directory even if they are defined in the root-level configuration.

But in my testing I discovered:

I am discovering these by trial and error. It would be great to get some documentation about how root/project configs are merged and address the typing issues.

woppa684 commented 1 year ago

I had Unknown option "default" with value ... issue with my ESM ("type": "module") project.

This issue occurs due to the way jest checks if imported object is ES module

if (obj.__esModule) return obj:
else return { default: obj }

So I have been looking at the same issue for the whole day now. I have a similar project that uses the exact same versions and configuration as the one that's failing that DOES work normally and it's really frustrating. For some reason, my jest.config.ts file is not interpreted as TypeScript then I guess. What could cause this?

raphael22 commented 1 year ago

Any workaround ? Check documentation, apply example -> warning...

woppa684 commented 1 year ago

I had Unknown option "default" with value ... issue with my ESM ("type": "module") project. This issue occurs due to the way jest checks if imported object is ES module

if (obj.__esModule) return obj:
else return { default: obj }

So I have been looking at the same issue for the whole day now. I have a similar project that uses the exact same versions and configuration as the one that's failing that DOES work normally and it's really frustrating. For some reason, my jest.config.ts file is not interpreted as TypeScript then I guess. What could cause this?

Finally found what the difference was between the project that was failing and the one that was succeeding. Apparently the failing project ran jest via ts-node instead of directly (because of a custom SDK we built on top of Nx, while testing the SDK itself). Maybe it helps someone :)

BrandonSchreck commented 1 year ago

It works if you downgrade to 28.1.3...enjoy

Peter-Sparksuite commented 11 months ago

I ran into this same issue today. Very weird. Using 28.1.3 does seem to address the problem, but, I'd really prefer to be using 29.x.x. What's happening on this issue?

mkykadir commented 11 months ago

This validation warning happens for me when I utilize a base Jest config in a monorepo project and utilize that base config in sub-package config files;

root/
-- packages/
---- package1/
------ jest.config.ts
---- package2/
------ jest.config.ts
jest.config.base.ts
jest.config.ts
package.json

all jest.config.ts files are importing and extending the jest.config.base.ts file

import jestBaseConfig from "./jest.config.base";

const jestConfig: JestConfigWithTsJest = {
  ...jestBaseConfig,
  moduleNameMapper: {
    "^(\\.{1,2}/.*)\\.js$": "$1",
  },
 ...
}

many validation warnings

● Validation Warning:

  Unknown option "forceExit" with value true was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration

● Validation Warning:

  Unknown option "silent" with value false was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration

base config is

import type { JestConfigWithTsJest } from "ts-jest";

const jestBaseConfig: JestConfigWithTsJest = {
  preset: "ts-jest",
  testEnvironment: "node",
  detectOpenHandles: true,
  forceExit: true,
  silent: false,
  verbose: true,
  testPathIgnorePatterns: ["node_modules", "dist", "env"],
  moduleDirectories: ["node_modules"],
  extensionsToTreatAsEsm: [".ts"],
  transform: {
    "^.+\\.tsx?$": [
      "ts-jest",
      {
        //the content you'd placed at "global"
        useESM: true,
      },
    ],
  },
};

export default jestBaseConfig;
saguilarolmo commented 10 months ago

I am also using jest on a monorepo with turborepo and deployed in Vercel. With the verbose: true setting I get the same warning, but it seems to work. Well.. work but in a strange way. Because the logs are marked in red even if the test passes XD. I'm using the version 29.7.0 of Jest I understand that this problem may be related to this warning. Do you know more about the solution in Jest version 29?

tscislo commented 10 months ago

I updated to "ts-jest": "~29.1.1", "jest": "~29.7.0" and this still occurs

● Validation Warning:

Unknown option "coverageReporters" with value ["lcov", "text-summary"] was found. This is probably a typing mistake. Fixing it will remove this message.

Configuration Documentation: https://jestjs.io/docs/configuration

● Validation Warning:

Unknown option "collectCoverage" with value true was found. This is probably a typing mistake. Fixing it will remove this message.

Configuration Documentation: https://jestjs.io/docs/configuration

● Validation Warning:

Unknown option "maxConcurrency" with value 4 was found. This is probably a typing mistake. Fixing it will remove this message.

Configuration Documentation: https://jestjs.io/docs/configuration

● Validation Warning:

Unknown option "passWithNoTests" with value true was found. This is probably a typing mistake. Fixing it will remove this message.

Configuration Documentation: https://jestjs.io/docs/configuration

● Validation Warning:

Unknown option "reporters" with value ["default", ["C:\workspace\rfq-ui-migration\rfq-ui-migration\node_modules\jest-html-reporter\dist\index.js", {"includeFailureMsg": true , "includeSuiteFailure": true, "outputPath": "reports/tests.html", "pageTitle": "Test Report", "sort": "default", "theme": "defaultTheme"}]] was found. This is probably a typing mistake. Fixing it will remove this message.

Configuration Documentation: https://jestjs.io/docs/configuration

gligorkot commented 10 months ago

Yeah, I have this same issue with collectCoverage, following.

mhornbacher commented 10 months ago

Having this problem for reporters when using typescript its valid but the runner gives me the same issue.

MeghanBomberger commented 10 months ago

I'm getting the same error but for watchPlugins

miguelci commented 8 months ago

Same for collectCoverage while using

● Validation Warning:

Unknown option "collectCoverage" with value true was found. This is probably a typing mistake. Fixing it will remove this message.

Configuration Documentation: https://jestjs.io/docs/configuration

nbolton commented 8 months ago

Not sure if this has already been mentioned, but this patch-package patch works for us...

patches/jest-validate+29.7.0.patch:

diff --git a/node_modules/jest-validate/build/warnings.js b/node_modules/jest-validate/build/warnings.js
index 1860d5a..aef7d81 100644
--- a/node_modules/jest-validate/build/warnings.js
+++ b/node_modules/jest-validate/build/warnings.js
@@ -23,6 +23,13 @@ function _interopRequireDefault(obj) {
  */

 const unknownOptionWarning = (config, exampleConfig, option, options, path) => {
+  // HACK: do not warn about coverageReporters and reporters.
+  // they are actually valid options, but are being reported as invalid.
+  // https://github.com/jestjs/jest/issues/14701
+  if (option === "coverageReporters" || option === "reporters") {
+    return;
+  }
   const didYouMean = (0, _utils.createDidYouMeanMessage)(
     option,
     Object.keys(exampleConfig)
korpiq-tiketti commented 7 months ago

Funny how our monorepo projects get Unknown option "verbose" with value true followed by a helpful link to documentation where the examples consistently use option "verbose" with value true :D

● Validation Warning:

  Unknown option "verbose" with value true was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration
byjokese commented 7 months ago

Some problem here. Also using NX v18.1.1 and jest v29.7.0.

Validation Warning:

  Unknown option "coverageReporters" with value ["cobertura", "html", "text-summary"] was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration

Validation Warning:

  Unknown option "reporters" with value XXXXXX was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration
alexey-sh commented 6 months ago
● Validation Warning:

  Unknown option "projects" with value ["<rootDir>/src"] was found.
  This is probably a typing mistake. Fixing it will remove this message.

for the jest 29.7

It seems like the documentation contains wrong information about configuration Is it possible to fix it in 2024? How many years should be enough to fix it?

wesfc commented 6 months ago

'● Validation Warning ' Unknown option "reporters" with value ["default", ["<rootDir>/node_modules/jest-junit/index.js", {"classNameTemplate": "{classname}: {title}", "suiteName": "Unit tests (Jest)", "suiteNameTemplate": [Function suiteNameTemplate], "titleTemplate": "{classname}: {title}"}]] was found.' ' This is probably a typing mistake. Fixing it will remove this message. ' Configuration Documentation: 'https://jestjs.io/docs/configuration'

For the Jest 29.7.0

The error appears only when I run tests from the test explorer (vscode), when I run "npm t" there's no error.

bfricka commented 5 months ago

I want to correct the idea that reporters should be a global-only field. This absolutely doesn't make sense, at least in terms of the options passed. For example, it seems obvious that we should be able to configure different output directories for each project:

{
  reporters: ['default', ['jest-junit', { outputDirectory: '/path/to/projectA/junit' }]]
}
bfricka commented 5 months ago

I also want to add that, the problem here is less about superfluous warnings, and more that some of these settings I've tried simply do not work at all. For example, the current version of Jest (29.7.0), allows you to have coverageDirectory defined, without throwing an error. However, it does not respect this field at all. You can add whatever coverage directory you want, but it'll always output to coverage.

Similarly, you can shut the reporters error up by adding reporters config to jest-config/build/ValidConfig.js -> initialProjectOptions, but this doesn't mean the reporters field is respected on a per-project basis. In fact, it's completely ignored.

I'll have to dig in and see how this might be implemented. If there's appetite for this and the maintainers would like to point me in the right direction, I'd appreciate it.

Khaldor48 commented 4 months ago

+1

cumt-robin commented 4 months ago

您的邮件我已收到!  我会在第一时间内回复您!非常感谢!

unadlib commented 3 months ago

Same warning(Jest v29.7.0)

Is there any further updates?

AbhijeetDeyTR commented 3 months ago

Facing the same warning in jest 29.7.0

olegomon commented 3 months ago

I'm having a validation warning on "testTimeout" with Jest 29.7.0

woppa684 commented 3 months ago

@SimenB Since Jest 29, about two years ago, many warnings about "unknown options" have been generated for no apparent reason. At some point someone made a PR that only fixed 1 or 2 of them but the others are still there (for example "reporters", that has been biting us for the last year).

These options still work on project level and they also make terribly good sense at project level! Why are these warnings generated? What was the idea behind it? And why is this not fixed when it was reported? It's a clear regression from v28, where it all worked as well, but there are no irrelevant warnings. In relation to your comment here: https://github.com/jestjs/jest/pull/13565#issuecomment-1307957629, I imagine fixing it never happened?

Within our company it would really help to get rid of them to build up a bit of trust in Jest (over Karma, where we come from).

ezintz commented 2 months ago

@woppa684 I think the priority here is low, because Jest is showing warning but the options you set on project level are still working.

Within our company it would really help to get rid of them to build up a bit of trust in Jest (over Karma, where we come from).

From our experience Karma is worse. We have an Angular application and Karma is executing tests successfully even there are properties missing on an object (maybe just configuration? idk) - Jest fails which is good because you are sure the code is executed correctly and you have to fix your test cases correctly.

artus9033 commented 2 months ago

Long story short: there's (afaik) no bug.

I also stumbled upon this problem and it seems like this issue should concentrate on updating the documentation & typings, since (as far as I inspected) the actual implementation at the moment works as it should.

This validation code in normalize.ts#L495 causes the warnings and stands in line with what @SimenB said in the first comments. Moreover, I tried and with jest 29.7.0, placing any coverage-related configuration props in a project config does not have any effect (apart from causing a validation warning, of course), while placing it in the global config works properly and does not cause any validation warnings.

I believe this issue should result in a change to the docs to point out that some properties (such as coverageReporters, coveragePathIgnorePatterns, etc.) are only allowed in global config and cannot be used in project config.

As one of the comments in this thread mentioned, jest does not provide separate exports for ProjectConfig and GlobalConfig, but such an improvement would be great for developer experience, since just annotating a config with the proper specialized type would protect devs from using props that are not allowed in the given config type.

CC: @MichaelBorde @SimenB

sebastianhaberey commented 2 months ago

So... after two years and 100 comments the answer is RTFM?