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

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

Closed
Aghassi opened this issue Feb 11, 2019 · 17 comments Β· Fixed by #7923

Comments

@Aghassi
Copy link
Contributor

Aghassi commented Feb 11, 2019

πŸ› 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
@Aghassi
Copy link
Contributor Author

Aghassi commented Feb 14, 2019

To add more context to this issue, I've been able to circumvent this problem currently by doing the following in each file that shows this issue:

/* eslint-disable no-console */
const originalLog = console.log;

describe('suite', () => {
  beforeAll(() => {
    // TODO: Remove this once jest resolves https://github.com/facebook/jest/issues/7857
    console.log = jest.fn((input) => {
      process.stdout.write(`${input} \n`);
    });
  });

  afterEach(() => {
    console.log = originalLog;
    /* eslint-enable */
  });

I'm still not sure why this is happening though

@SimenB
Copy link
Member

SimenB commented Feb 14, 2019

Please put together a repository we can pull down and check. If just using Jest 24 broke, we'd be getting a lot more reports πŸ˜‰ Do you have a custom transformer?

#7844 will not help - the code basically identical.

@Aghassi
Copy link
Contributor Author

Aghassi commented Feb 14, 2019

@SimenB I will work on that,

As for the transformer I have

// Set the basic transform
defaults.transform = {
  '^.+\\.(js|jsx)$': require.resolve('babel-jest'),
};

And the version in use is 24.1.0

@SimenB
Copy link
Member

SimenB commented Feb 14, 2019

Cool, that shouldn't be a problem. Looking forward to the repo!

@Aghassi
Copy link
Contributor Author

Aghassi commented Feb 14, 2019

@SimenB Here is my example repo where I was able to reproduce the issue https://github.com/Aghassi/oclif-jest-demo-failure

@Aghassi
Copy link
Contributor Author

Aghassi commented Feb 14, 2019

Here is the output I got locally

mynewcli on ξ‚  master is πŸ“¦ v0.0.0 via β¬’ v8.12.0 at ☸️ docker-for-desktop
➜ yarn jest
yarn run v1.13.0
$ /Users/daghassi/git/oclif-jest-demo/mynewcli/node_modules/.bin/jest
 PASS  test/index.test.js
hello world from ./src/index.js
 PASS  test/index.2.test.js
 FAIL  test/index.2.test.js
  ● 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>)

Test Suites: 1 failed, 2 passed, 3 of 2 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        1.683s
Ran all test suites.
hello world from ./src/index.js
hello world from ./src/index.js
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

@jeysal
Copy link
Contributor

jeysal commented Feb 17, 2019

This is happening because you set cwd in your Jest config.
Usually, Jest has a default for cwd, but funnily enough, that default becomes undefined if you set it yourself. I'll make a PR so that the cwd is not lost.

@SimenB is this supposed to be a supported option? It's not in the docs or in InitialConfig

@jeysal
Copy link
Contributor

jeysal commented Feb 17, 2019

Caused by #7742 adding cwd to ValidConfig

@realeve
Copy link

realeve commented Feb 18, 2019

the same question with you @Aghassi

@Aghassi
Copy link
Contributor Author

Aghassi commented Feb 19, 2019

Yes I guess the question is is cwd something we should be able to set? I think I may be just setting it to default to process.cwd right now, which may be unnecessary seeing as I think that is what it defaults to anyway.

@jeysal
Copy link
Contributor

jeysal commented Feb 19, 2019

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.

@Aghassi
Copy link
Contributor Author

Aghassi commented Feb 19, 2019

@jeysal that's fair. I can't think of anything off hand where I want the working directory to be different. I guess even if we are using it in a CLI (which we are), we could do a require.resolve to get the path of a package in the CLI as oppose to just a plain require which would take from the cwd of the running process.

@ycjcl868
Copy link

I added mock console, solve the problem temporarily.

  Object.defineProperty(window, 'console', {
    writable: true,
    value: {
      debug: () => {},
      info: () => {},
      error: () => {},
      log: () => {},
    }
  });

@jeysal
Copy link
Contributor

jeysal commented Mar 26, 2019

The issue shown by the repro should be fixed in the next release. I still suspect your internal repo might have another problem though based on what you said in the PR.

@Aghassi
Copy link
Contributor Author

Aghassi commented Mar 26, 2019

@jeysal hopefully not, but if that's the case I will have less trouble pin pointing it now that this is resolved πŸ‘ . Looking forward to the next version!

@Aghassi
Copy link
Contributor Author

Aghassi commented Apr 4, 2019

Just to post back here to close this issue from my end. One of the other reasons I was seeing this issue was that someone on our team has done jest.mock('fs') at a top level, which I think indirectly broken an internal jest mechanism. We are going to refactor to fix this. Seems like a potential issue though (not sure it is avoidable) that Jest can break when someone mocks a main node library like that.

@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 May 12, 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.

5 participants