Jest: "SyntaxError: Unexpected token = " . Error in this file and line " ../node_modules/react-native/jest/mockComponent.js:20 static displayName = 'Component'; " . Code is in the first comment.

Created on 24 Jan 2019  路  3Comments  路  Source: facebook/jest

_Mockcomponent.js_

/**

  • Copyright (c) 2013-present, Facebook, Inc.
    *
  • This source code is licensed under the MIT license found in the
  • LICENSE file in the root directory of this source tree.
    *
  • @format
    */

'use strict';

module.exports = (moduleName, instanceMethods) => {
const RealComponent = require.requireActual(moduleName);
const React = require('react');

const SuperClass =
typeof RealComponent === 'function' ? RealComponent : React.Component;

const Component = class extends SuperClass {
static displayName = 'Component';

render() {
  const name =
    RealComponent.displayName ||
    RealComponent.name ||
    (RealComponent.render // handle React.forwardRef
      ? RealComponent.render.displayName || RealComponent.render.name
      : 'Unknown');

  const props = Object.assign({}, RealComponent.defaultProps);

  if (this.props) {
    Object.keys(this.props).forEach(prop => {
      // We can't just assign props on top of defaultProps
      // because React treats undefined as special and different from null.
      // If a prop is specified but set to undefined it is ignored and the
      // default prop is used instead. If it is set to null, then the
      // null value overwrites the default value.
      if (this.props[prop] !== undefined) {
        props[prop] = this.props[prop];
      }
    });
  }

  return React.createElement(
    name.replace(/^(RCT|RK)/, ''),
    props,
    this.props.children,
  );
}

};

Object.keys(RealComponent).forEach(classStatic => {
Component[classStatic] = RealComponent[classStatic];
});

if (instanceMethods != null) {
Object.assign(Component.prototype, instanceMethods);
}

return Component;
};

_Package.json:_

{
"name": "react_native_template",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
"react-devtools": "react-devtools"
},
"dependencies": {
"@types/react": "^16.7.20",
"babel-preset-react-native": "^4.0.1",
"fetch-intercept": "^2.3.1",
"native-base": "^2.10.0",
"react": "16.6.1",
"react-native": "0.57.5",
"react-native-device-info": "^0.24.3",
"react-native-easy-grid": "^0.2.0",
"react-native-touch-id": "^4.3.0",
"react-native-vector-icons": "^6.2.0",
"react-navigation": "^2.18.3",
"react-redux": "^5.1.1",
"redux": "^4.0.1",
"redux-persist": "^5.10.0",
"redux-thunk": "^2.3.0",
"wd": "^1.11.1"
},
"devDependencies": {
"@babel/core": "^7.2.2",
"babel-jest": "23.6.0",
"jest": "23.6.0",
"metro-react-native-babel-preset": "0.49.1",
"react-devtools": "^3.6.0",
"react-test-renderer": "16.6.1"
},
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"/node_modules/react-native/.+"
]
}
}

All 3 comments

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

I would like to know what is causing the issue though. I seemed to have tried everything. Yet no use.

I understand, but, again, this isn't a help forum - it's an issue tracker. You're having issues transpiling - that's a configuration error, not an issue with Jest

Was this page helpful?
0 / 5 - 0 ratings