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

TypeError: val.entries is not a function when testing for image src #5295

Closed
EdmundMai opened this issue Jan 12, 2018 · 11 comments
Closed

TypeError: val.entries is not a function when testing for image src #5295

EdmundMai opened this issue Jan 12, 2018 · 11 comments
Assignees

Comments

@EdmundMai
Copy link

I have a Logo component:

import React from "react";
import logo from "assets/images/logo.png";

const Logo = () => {
  return <img style={{ width: 50 }} src={logo} alt="logo" />;
};

export default Logo;

Test file:

import React from "react";
import Logo from "components/shared/Logo";

describe("<Logo />", () => {
  it("renders an image", () => {
    const logo = shallow(<Logo />);

    expect(logo.find("img").prop("src")).toEqual("blahh");
  });
});

But when I run the test, there is some weird error:

$ NODE_PATH=src jest
 FAIL  src/tests/Logo.test.js
  ● <Logo /> › renders an image

    TypeError: val.entries is not a function

      at printImmutableEntries (node_modules/expect/node_modules/pretty-format/build/plugins/immutable.js:44:5)
      at Object.<anonymous>.exports.serialize (node_modules/expect/node_modules/pretty-format/build/plugins/immutable.js:178:12)

How am I supposed to test that the image src === "assets/images/logo.png"?

@pedrottimark
Copy link
Contributor

@EdmundMai In which version of Jest did the error occur?

This might be a duplicate of #4922 which was fixed in #4935 available in Jest 22.

@pedrottimark pedrottimark self-assigned this Jan 12, 2018
@EdmundMai
Copy link
Author

Hi @pedrottimark ! This is what is printed when I type npm ls jest-environment-node

➜  oyster git:(master) ✗ npm ls jest-environment-node
[email protected] /Users/edmundmai/Documents/src/oyster/webinterface/oyster
├─┬ [email protected]
│ └─┬ [email protected]
│   └─┬ [email protected]
│     └── [email protected]
└─┬ [email protected]
  └─┬ [email protected]
    └─┬ [email protected]
      └─┬ [email protected]
        └── [email protected]


I remember reading somewhere that I needed to have those versions match... but when I made them match at 20.0.3 I think I saw another error. Would it be safe to just update Jest to v22?

Here's my package.json file by the way:

{
  "name": "oyster",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-redux": "^5.0.6",
    "redux": "^3.7.2",
    "redux-logger": "^3.0.6",
    "redux-observable": "^0.17.0",
    "rxjs": "^5.5.6"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-jest": "^22.0.6",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-16": "^1.1.1",
    "enzyme-to-json": "^3.3.0",
    "identity-obj-proxy": "^3.0.0",
    "jest": "21.2.1",
    "jest-enzyme": "^4.0.2",
    "react-addons-test-utils": "^15.6.2",
    "react-scripts": "1.0.17",
    "regenerator-runtime": "^0.11.1"
  },
  "scripts": {
    "start": "NODE_PATH=src react-scripts start",
    "build": "NODE_PATH=src react-scripts build",
    "test": "jest",
    "eject": "NODE_PATH=src react-scripts eject"
  },
  "jest": {
    "roots": ["<rootDir>/src"],
    "modulePaths": ["<rootDir>/src"],
    "setupTestFrameworkScriptFile": "<rootDir>/src/setupTests.js",
    "moduleNameMapper": {
      ".+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$":
        "identity-obj-proxy"
    }
  }
}

(Thanks for your help in advance)

@EdmundMai
Copy link
Author

Here's what happens when I upgrade to Jest v 22:


➜  oyster git:(master) ✗ yarn test
yarn test v0.24.6
$ jest
 FAIL  src/tests/logo.test.js
  ● Test suite failed to run

    TypeError: environment.setup is not a function

      at node_modules/jest-runner/build/run_test.js:112:23


@SimenB
Copy link
Member

SimenB commented Jan 13, 2018

When using CRA it's not supported to install your own version of jest (#5119 (comment)). Do you still get the same error after removing jest from your devDependencies?

@pedrottimark
Copy link
Contributor

pedrottimark commented Jan 13, 2018

@EdmundMai Here is a procedure that I have used to temporarily uneject on a branch to upgrade Jest until create-react-app catches up, if you dare to void your warranty :)

https://speakerdeck.com/pedrottimark/uneject-and-recreate-reactjs-charlotte

@EdmundMai
Copy link
Author

EdmundMai commented Jan 15, 2018

@SimenB After I remove jest from my devDependencies, I get this:

➜  oyster git:(em-jest-conflict) ✗ jest --version
v20.0.4
➜  oyster git:(em-jest-conflict) ✗ jest
 FAIL  src/tests/main.test.js
  ● Test suite failed to run

    TypeError: Path must be a string. Received undefined

      at assertPath (path.js:28:11)
      at Object.relative (path.js:1247:5)


When I try downloading v21.x (got this idea from: https://stackoverflow.com/questions/46220674/jest-typeerror-path-must-be-a-string-received-undefined), it gives me this error:

➜  oyster git:(em-jest-conflict) ✗ npm install -g [email protected]
/usr/local/bin/jest -> /usr/local/lib/node_modules/jest/bin/jest.js

> [email protected] install /usr/local/lib/node_modules/jest/node_modules/fsevents
> node install

[fsevents] Success: "/usr/local/lib/node_modules/jest/node_modules/fsevents/lib/binding/Release/node-v57-darwin-x64/fse.node" already installed
Pass --update-binary to reinstall or --build-from-source to recompile
+ [email protected]
added 490 packages in 13.561s


➜  oyster git:(em-jest-conflict) ✗ jest --version
v20.0.4
(node:20449) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'results' of undefined
(node:20449) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
➜  oyster git:(em-jest-conflict) ✗


➜  oyster git:(em-jest-conflict) ✗ npm --version
5.4.1
➜  oyster git:(em-jest-conflict) ✗ node --version
v8.1.4

@SimenB
Copy link
Member

SimenB commented Jan 15, 2018

Again, you cannot install your own version of jest when using CRA.

TypeError: Path must be a string. Received undefined seems weird, can you provide a reproduction repo?

@EdmundMai
Copy link
Author

@SimenB Great idea, just pushed a demo: https://github.com/EdmundMai/jest-demo

Run yarn test to see the error

@SimenB
Copy link
Member

SimenB commented Jan 15, 2018

@EdmundMai Your issue is that babel-jest@22 is not compatible with jest@20. You have to remove or downgrade babel-jest.

Your issue is on the CRA side, as you want to use some config they do not allow. I think you should open up an issue with them.

@EdmundMai
Copy link
Author

@SimenB You're a life saver!!!!!! Thanks so much for all your help!!

@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 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants