jest: Jest snapshot testing fails for tag.

Reporting a BUG

What is the current behavior? When running my test I get the following error:

  ● loading icon

    TypeError: val.entries is not a function

      at Object.<anonymous>.test (src/__tests__/core/components/loading.spec.tsx:12:26)
          at new Promise (<anonymous>)
          at <anonymous>

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

This is the component I’m testing.

var image = require('../../public/assets/img/loading.svg');

export class LoadIcon extends Component {
    render() {
        return (
            <div className='loading-icon'>
                <img src={image} />
            </div>
        );
    }
}

Here’s my test:

test('loading icon', () => {
    const loadingicon = renderer.create(
        <LoadingIcon />
    );
    expect(loadingIcon.toJSON()).toMatchSnapshot();
});

What is the expected behavior? Create a snapshot and pass test.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

Jest config:

  "jest": {
    "globals": {
      "ts-jest": {
        "skipBabel": true
      }
    },
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ],
    "transform": {
      "\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "/(__tests__)/.*\\.spec.(ts|tsx|js)$",
    "moduleNameMapper": {
      ".+\\.(css|styl|less|sass|scss|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "identity-obj-proxy"
    },
    "setupFiles": [
      "jest-fetch-mock",
      "jest-localstorage-mock",
      "./src/__tests__/__setups__/localstorage.ts"
    ]
  },

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 20 (8 by maintainers)

Most upvoted comments

@PainKKKiller / @ashok-sc I was able to fix it changing this line in the package.json:

    "moduleNameMapper": {
      ".+\\.(css|styl|less|sass|scss|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "identity-obj-proxy"

To:

    "moduleNameMapper": {
      ".+\\.(css|styl|less|sass|scss)$": "identity-obj-proxy",

      ".+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js"

And this is the content of fileMock.js:

// __mocks__/fileMock.js

module.exports = 'test-file-stub'

It seems that identity-obj-proxy is supposed to be used only for css/less files.

Hmm, unfortunately still getting that error 😦.

TypeError: val.getMockName is not a function.

Do you know what’s actually happening here?