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

[Bug]: testEnvironmentOptions documentation/types are wrong #11900

Closed
DetachHead opened this issue Sep 28, 2021 · 4 comments · Fixed by #11902
Closed

[Bug]: testEnvironmentOptions documentation/types are wrong #11900

DetachHead opened this issue Sep 28, 2021 · 4 comments · Fixed by #11902

Comments

@DetachHead
Copy link

DetachHead commented Sep 28, 2021

Version

27.2.2

Steps to reproduce

  1. define a custom environment, eg.
    class CustomEnvironment extends NodeEnvironment {
        constructor(config) {
            super(config)
            const options = config.testEnvironmentOptions //type is Record<string, unknown>
            console.log(typeof options)
        }
    }
    (this step propbably isn't necessary to reproduce the issue but makes it easier to see the problem)
  2. run jest -h
          --testEnvironmentOptions      Test environment options that will be passed
                                        to the testEnvironment. The relevant options
                                        depend on the environment.          [string]
    
    notice the type string
  3. pass a value to --testEnvironmentOptions, assuming it gets JSON.parsed into a Record:
    jest --testEnvironmentOptions="{ foo: 'bar' }"
    

Expected behavior

if the type passed in the cli is stringbut the type in ts is Record<string, unknown>, i'd expect it to parse the JSON into a Record or something. therefore, the console.log from the custom environment's constructor outputs "object"

Actual behavior

the console.log from the custom environment's constructor outputs "string"

Additional context

testEnvironmentOptions isn't even present here. are you even supposed to be able to pass it in the command line?

Environment

System:
    OS: Windows 10 10.0.18363
    CPU: (6) x64 Intel(R) Core(TM) i5-8600 CPU @ 3.10GHz
  Binaries:
    Node: 16.9.1 - C:\Program Files\nodejs\node.EXE
    npm: 7.24.1 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: ^27.2.2 => 27.2.2
@mrazauskas
Copy link
Contributor

The string type comes from here: https://github.com/facebook/jest/blob/8024306c365cdf8b17b2256c73f1f4c9f23f8f77/packages/jest-cli/src/cli/args.ts#L550-L555

There are other similar options which take JSON string as an argument: --config, --coverageThreshold, --globals, --haste, and more.

Looking at the whole picture, I think it would be better to improve the description prop.

@DetachHead
Copy link
Author

i think either this needs to also be changed to Record<string, unknown> | string, or it needs to JSON.parse the string when testEnvironmentOptions is passed from the cli so that it's always a Record (which i think would be the better choice)
https://github.com/facebook/jest/blob/e0b33b74b5afd738edc183858b5c34053cfc26dd/packages/jest-types/src/Config.ts#L385

@SimenB
Copy link
Member

SimenB commented Sep 28, 2021

Your expectation is correct - it should be passed through JSON.parse.

(note that --testEnvironmentOptions="{ foo: 'bar' }" should fail as it's JS literal and not JSON - should be --testEnvironmentOptions='{ "foo": "bar" }').

testEnvironmentOptions isn't even present here. are you even supposed to be able to pass it in the command line?

We haven't been thorough enough when documenting all options - it should be there 🙂

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants