Jest TypeError: (0 , _jestUtil(...).tryRealpath) is not a function

See original GitHub issue

πŸ› Bug Report

I am trying to come up with correct jest config to test a simple screen:

// TestScreen.tsx
import React from 'react';
import { View, Text } from 'react-native';

export default function TestScreen() {
  return (
    <View data-test={'component-app'}>
      <Text>Test Screen</Text>
    </View>
  );
}

This is my test:

// App.test.js
import React from 'react';
import {shallow} from 'enzyme';
import TestScreen from '../src/screens/TestScreen';

/**
 * Return node(s) with the given data-test attribute.
 * @param {ShallowWrapper} wrapper - Enzyme shallow wrapper.
 * @param {string} val - Value of data-test attribute for search.
 * @returns {ShallowWrapper}
 */
export const findByTestAttr = (wrapper, val) => {
  return wrapper.find(`[data-test="${val}"]`);
};

/**
 * Factory function to create a ShallowWrapper for the App component
 * @function setup
 * @returns {ShallowWrapper}
 */
const setup = () => {
  return shallow(<TestScreen />);
};

test('renders without error', () => {
  const wrapper = setup();
  const component = findByTestAttr(wrapper, 'component-app');
  expect(component.length).toBe(1);
});

However, when running the test, I get this error:

yarn run v1.22.4
$ jest __tests__/App.test.js
 FAIL  __tests__/App.test.js
  ● Test suite failed to run

    TypeError: (0 , _jestUtil(...).tryRealpath) is not a function

      at ScriptTransformer.transformSource (node_modules/jest-runtime/node_modules/@jest/transform/build/ScriptTransformer.js:424:50)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.909s
Ran all test suites matching /__tests__\/App.test.js/i.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

To Reproduce

Steps to reproduce the behavior:

Expected behavior

The test should run without error.

Link to repl or repo (highly encouraged)

This is my jest setup configuration

// jest.config.js
module.exports = {
  preset: 'react-native',
  collectCoverage: true,
  moduleDirectories: ['node_modules', 'src'],
  transform: {
    '^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js',
  },
  setupFiles: ['<rootDir>setup-tests.js'],
  transformIgnorePatterns: ['node_modules/(?!(jest-)?react-native)'],
  coveragePathIgnorePatterns: ['/node_modules/', '/jest'],
  testEnvironment: 'jsdom',
};
// setup-tests.js
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

envinfo

  System:
    OS: macOS 10.15.3
    CPU: (4) x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
  Binaries:
    Node: 12.13.1 - ~/.nvm/versions/node/v12.13.1/bin/node
    Yarn: 1.22.4 - ~/Documents/youpendo-app-bareworkflow/node_modules/.bin/yarn
    npm: 6.12.1 - ~/.nvm/versions/node/v12.13.1/bin/npm
  npmPackages:
    jest: ^26.4.2 => 26.4.2
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.4",
    "jsdom": "^16.4.0",

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

11reactions
ghasemikasra39commented, Sep 4, 2020

Dear @SimenB Issue fixed by doing the following, it’s very strange, but it worked: Just delete the node_modules and run yarn install/npm install and the error is gone

0reactions
github-actions[bot]commented, May 11, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

(0 , (_jestUtil || _load_jestUtil(...)).validateCLIOptions) is not a ...
Trying to pin down the source of this error when running jest: TypeError: (0 , (_jestUtil ...
Read more >
Jest error TypeError: (0 , _jest.test) is not a function-Reactjs
I've got it running with flow using import expect from "expect"; const test = window.test;. Maybe it helps you, too. It seems to...
Read more >
Why do I keep getting type error, "x" is not a function ... - Reddit
However I can't seem to test a simple function that adds two numbers. I keep getting, TypeError: (0 , _sum2.default) is not a...
Read more >
typescriptError: (0 , _reactRedux.connect) is not a function
Hi I am using jest and enzyme for unit testing and in this when I run test cases getting this error in login...
Read more >
TypeError: (0 , _dom.configure) is not a function #1055
TypeError : (0 , _dom.configure) is not a function 1 | import React from ... { useForm } from 'react-hook-form'; 5 | import...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found