Jest: Jest: Cannot use configuration as an object without a file path

Created on 30 Jan 2018  路  6Comments  路  Source: facebook/jest

I'm getting the error described in this test:

https://github.com/facebook/jest/blob/a43977281802b192b6cdb57a1dc806bdf42169d1/packages/jest-config/src/__tests__/read_config.test.js#L12

I have no clue on what it means or what it wants me to do, I'm trying to use the multi project configuration to run eslint along with the normal tests.

May someone explain this error? I tried to set rootDir in my root config but it doesn't seem to do anything

This is my config:

{
  "collectCoverageFrom": [
    "src/**/*.{js,jsx,mjs}"
  ],
  "setupFiles": [
    "raf/polyfill",
    "node_modules/react-scripts/config/polyfills.js"
  ],
  "setupTestFrameworkScriptFile": "<rootDir>/src/setupTests.js",
  "testMatch": [
    "<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}",
    "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"
  ],
  "testEnvironment": "node",
  "testURL": "http://localhost",
  "transform": {
    "^.+\\.(js|jsx|mjs)$": "node_modules/react-app-rewired/scripts/utils/babelTransform.js",
    "^.+\\.css$": "node_modules/react-scripts/config/jest/cssTransform.js",
    "^(?!.*\\.(js|jsx|mjs|css|json)$)": "node_modules/react-scripts/config/jest/fileTransform.js"
  },
  "transformIgnorePatterns": [
    "[/\\\\]node_modules(?!/@foobar/react-components)[/\\\\].+\\.(js|jsx)$"
  ],
  "moduleNameMapper": {
    "^.+\\.(css|sss)$": "node_modules/@foobar/react-app-rewire-foobar/node_modules/identity-obj-proxy/src/index.js"
  },
  "moduleFileExtensions": [
    "web.js",
    "mjs",
    "js",
    "json",
    "web.jsx",
    "jsx",
    "node"
  ],
  "rootDir": ".",
  "coverageThreshold": {
    "global": {
      "statements": 100,
      "functions": 100,
      "branches": 100,
      "lines": 100
    }
  },
  "coveragePathIgnorePatterns": [
    "index\\.js$",
    ".*demo\\.jsx$",
    "src/demo/*",
    "src/components/Modal/*"
  ],
  "projects": [
    {
      "displayName": "unit",
      "rootDir": "."
    },
    {
      "displayName": "lint",
      "rootDir": ".",
      "runner": "jest-runner-eslint",
      "testMatch": [
        "<rootDir>/**/*.{js,jsx}"
      ]
    }
  ]
}
Needs More Info

Most helpful comment

The issue is that this error is cryptic and doesn't help in any way to understand what's going on...
With or without a repro, this error should be improved to help the consumer to understand what's the required action to fix it.

All 6 comments

:wave: Hey @FezVrasta

If I could debug your use case I could provide a better help.
Can you recreate this issue in a smaller example project, so I could see it locally and debug it?


If you rather debug it yourself, here is some information:

This error gets thrown when calling the readConfig function with an object, and parentConfigPath is not defined.

https://github.com/facebook/jest/blob/bda02e64d4117bcc6cf80a7a7f185b5108607ca0/packages/jest-config/src/index.js#L45-L53

As this is a result of your try to use the projects option, the error is probably thrown from one of the projects, while iterating through them.

https://github.com/facebook/jest/blob/4ceed46b4e9dcfe5a9b1944bc8a7f5a4d4ccbb92/packages/jest-cli/src/cli/index.js#L264

Yup, without a repro this is not actionable.

Happy to reopen when provided though!

The issue is that this error is cryptic and doesn't help in any way to understand what's going on...
With or without a repro, this error should be improved to help the consumer to understand what's the required action to fix it.

To me it looks like an internal one that shouldn't surface to the user (at least in such form). Having a repro would be helpful in fixing that.

Hey, I just came across this error too. From digging around the links from @ranyitz I think I figured out why this occurs. Basically my situation is a fork of the test script in create-react-app. Which in essence is: jest.run('--config ' + JSON.stringify(someConfig)). So if I set projects in someConfig to be

[ {
    displayName: 'Typography',
    rootDir: 'packages/Typography'
} ]

which admittedly now that I'm looking through it is _not_ how this is supposed to be used I get the above error.
Simply this is because of the following line, which is not defined in a parsed config.
https://github.com/facebook/jest/blob/d6192817d59c4a0a84b0d31e3ac9b65f4f5b14f2/packages/jest-cli/src/cli/index.js#L230

I guess what I'm trying to do here is run the same config on multiple projects (a monorepo) and just append the displayName before for easier reporting.

I'm just reading the two very different setups and assuming they could be used together, what I want is probably actually the following but with the ability to add displayNames to it. (Or just completely off the wrong end of this one).

{
  "projects": ["<rootDir>/packages/Typography/src", "<rootDir>/packages/Other/src"]
}

Hopefully, this helps anyone else who hits this particular situation.

Was this page helpful?
0 / 5 - 0 ratings