React-app-rewired: Getting Weired `path` Error on test

Created on 12 Dec 2017  路  14Comments  路  Source: timarney/react-app-rewired

Sample Repo

Node Version: v9.2.1 | v8.4.0

Issue Details:

  • npm run test (using direct react-scripts) is running well.
  • npm run test:rewired is getting TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string error.

Screenshot

Most helpful comment

indeed, replacing jest with jest-cli (^21.2.1) did resolve the issue. Thanks :-)

All 14 comments

I was wondering whether anyone has any updates on it?

@jimzhan Thank you for the test repo.

I've dug about a bit, and found that the cause of the error is actually changes made in babel-jest version 21.2.0. React-scripts is using version 20.0.3, which is why their version is working and the react-app-rewired one is not.

Version 21.2.0 of babel-jest requires at least jest-cli version 21.x.x - I've tested with 21.2.0 - for it to work. Version 21.0.0 and lower of babel-jest can work with the currently installed jest-cli versions of 20.x.x.

So to resolve this issue, either downgrade babel-jest to version 21.0.0, or add jest-cli@^21.2.0 to your devDependencies. Both of those options still allow the npm run test to work as well.

indeed, replacing jest with jest-cli (^21.2.1) did resolve the issue. Thanks :-)

This was not the case for me. I replaced jest with jest-cli and I get the same error: TypeError: Path must be a string. Received undefined

@rardoz - what versions of jest-cli and babel-jest are installed? Do you have an example repository I can look at?

I don't but after tinkering with it, its because of my lerna setup. Jest is kind of messed up I think in one of my packages. Its hard to debug, but thats what I think is going on.

When I was debugging the sample repository from the original bug report, the cause was because the version of babel-jest that got installed by the repository was expecting to receive an object containing two keys - instrument and rootDir - and the older version of jest-cli that had been installed by create-react-app was only passing the instrument key. This meant that when babel-jest tried to use the rootDir variable to create a path to a file it couldn't do so because rootDir was undefined.

The issue is not actually caused by react-app-rewired, but due to a problem with the mismatch in versions between jest-cli and babel-jest. I don't know anything about lerna myself, but I hope that gives you some additional detail in what was happening so that you can track version numbers through what is being installed/used by lerna.

@rardoz Thinking about it, if there are multiple packages that are installing different versions you may be better with downgrading babel-jest to the 21.0.0 version (because it only looks for the instrument key) rather than increasing jest-cli to a higher version. That way the version of babel-jest installed should work directly with what lerna is installing - assuming that lerna is using an earlier version of jest/jest-cli.

Thanks @dawnmist I will give that a try.

I am seeing this problem with [email protected] and [email protected]

The message itself means that something _somewhere_ is providing undefined for a file path and then trying to access that path. In the example project, it was caused by a mismatch in the jest-cli and babel-jest APIs for versions being installed "by default".

It actually isn't _caused by_ react-app-rewired. The message itself is a generic node-js message that is shown whenever someone uses an undefined file path. Without an example repo, I cannot help find where the undefined path is coming from for your project - but it is highly unlikely to be coming from react-app-rewired itself. The most likely issue is a mismatch in package versions for other npm packages installed by your project.

@dkent600 solution to use same major version of jest and babel-jest and jest worked for me.
ex: ^22.4.3
Thanks!

@dkent600 solution to use same _major_ version of jest and babel-jest and jest worked for me.
ex: ^22.4.3
Thanks!

I'm curious as to how this worked for you at all? From what I understand, we cannot utilize a higher or different version of babel-jest with react-app-rewired due to a lower version of jest-/jest-cli:

-- [email protected]
  -- [email protected]
    -- [email protected]
      -- [email protected]
        -- [email protected]

@cjones26 If you were to add jest-cli@^22.4.3 in your devDependencies, that specified version should be the one that gets used when running tests through react-app-rewired instead of the version specified in react-scripts. At least, that's how it worked with the original sample repository I was provided for debugging this issue.

Was this page helpful?
0 / 5 - 0 ratings