jest: Getting "TypeError: Path must be a string. Received undefined" preventing me from upgrading to Jest v24 in some repos

๐Ÿ› Bug Report

I have a number of repos in our enterprise setup that are running into the following error:

  โ— Test suite failed to run

    TypeError: Path must be a string. Received undefined

      at buffer.reduce (node_modules/jest-util/build/getConsoleOutput.js:54:52)
          at Array.reduce (<anonymous>)

As far as I can tell, the trend seems to be related to when something in the test needs to output via the console, but a mock may be involved that either spies something related to a console log, or spies the console itself. This doesnโ€™t happen with all tests, only a few. And when running the tests one off, I get the same error, but with a stack trace that is more defined than the above.

An example of one of the failing tests

  test('should exit if config assertion fails', () => {
    const message = 'dummy error';
    assertConfig.mockImplementation(() => {
      throw new Error(message);
    });
    // no-op exit spy to stop the actual process exit.
    jest.spyOn(process, 'exit').mockImplementation(() => {});
    jest.spyOn(console, 'log');

    config.loadConfig();

    expect(process.exit).toBeCalled();
    expect(process.exit).not.toBeCalledWith(0);
    // eslint-disable-next-line no-console
    expect(console.log).toBeCalledWith(message);

    process.exit.mockRestore();
    // eslint-disable-next-line no-console
    console.log.mockRestore();
  });

Itโ€™s possible this is fixed by https://github.com/facebook/jest/pull/7844/files, @SimenB maybe you can let me know? If so, I can wait till the next minor to validate.

EDIT: Doing some more digging it seems the config context is getting lost. When I run console.log on this line in my node_modules https://github.com/facebook/jest/blob/b49075ede2ec0b26bc626e25c5d383df31770413/packages/jest-runner/src/index.js#L82

I get cwd is undefined. When I run jest --showConfig, I donโ€™t see that cwd is defined.

To Reproduce

Steps to reproduce the behavior:

  1. Upgrade all jest and jest related modules to v24 or later
  2. Run tests

Expected behavior

All tests should pass

Link to repl or repo (highly encouraged)

I canโ€™t do this because the repl uses v22 of Jest

Please provide either a repl.it demo or a minimal repository on GitHub.

Issues without a reproduction link are likely to stall.

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS 10.14.3
    CPU: (8) x64 Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz
  Binaries:
    Node: 8.12.0 - ~/.nvm/versions/node/v8.12.0/bin/node
    Yarn: 1.13.0 - ~/git/repo/node_modules/.bin/yarn
    npm: 6.5.0 - ~/git/repo/node_modules/.bin/npm
  npmPackages:
    jest: 24.1.0 => 24.1.0

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 4
  • Comments: 17 (14 by maintainers)

Most upvoted comments

IMO it just makes things confusing if cwd becomes anything other than what the OS considers the cwd of the process. For all purposes that require setting the the base directory manually, we have rootDir.