question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot get ts-jest to create Snapshots when using React-native

See original GitHub issue

I have been trying various work arounds but cannot get ts-jest and to create snapshots for my react-native components. The following tests fail and say that it can’t read property ‘createElement’ of undefined

import { Text } from "react-native";
import React, { Component } from "react";

// Note: test renderer must be required after react-native.
import { create } from "react-test-renderer";

describe("Test Component", () => {
  it("renders correctly", () => {
    const tree = create(
      <TestComponent />
    ).toJSON();
    expect(tree).toMatchSnapshot();
  });
});

class TestComponent extends Component<any, any>{
  render() {
    return <Text>Test</Text>;
  }
}

Here is the output: image

Here is my jest configuration

 "jest": {
    "preset": "react-native",
    "transform": {
      "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
      ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
   "testRegex": "(src\/(.*)(spec|test).(tsx|ts))$",
    "testResultsProcessor": "<rootDir>/node_modules/ts-jest/coverageprocessor.js",
    "transformIgnorePatterns": [
      "/node_modules/(?!native-base)/"
    ],
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json"
    ]
  },

And here are my dependencies:

"dependencies": {
   "react": "16.0.0-alpha.6",
   "react-native": "0.43.1"
 },
 "devDependencies": {
   "@expo/vector-icons": "^4.0.0",
   "@types/enzyme": "^2.7.7",
   "@types/jest": "^19.2.2",
   "@types/react": "^15.0.22",
   "@types/react-native": "^0.42.16",
   "@types/react-test-renderer": "^15.4.5",
   "babel-jest": "^19.0.0",
   "babel-preset-react-native": "^1.9.1",
   "enzyme": "^2.8.2",
   "enzyme-to-json": "^1.5.1",
   "gulp": "^3.9.1",
   "gulp-tslint": "^8.0.0",
   "gulp-typescript": "^3.1.6",
   "jest": "19.0.2",
   "jest-cli": "^19.0.2",
   "jest-preset-typescript-react-native": "^1.0.1",
   "jest-react-native": "^18.0.0",
   "native-base": "^2.1.2",
   "react-dom": "^15.4.2",
   "react-native-mock": "^0.3.1",
   "react-native-vector-icons": "^4.0.1",
   "react-test-renderer": "^15.5.4",
   "ts-jest": "^19.0.10",
   "tslint": "^5.1.0",
   "typescript": "^2.2.2"
 }

Here is my .babelrc:

{
"presets": ["react-native"]
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jaredmahancommented, Apr 26, 2017

So it had to be a version mismatch issue, i deleted my node_modules folder and ran npm i and now it is creating snapshots! Thanks @ds300 for being willing to take a look!

0reactions
jaredmahancommented, Apr 26, 2017

Yes I will try to create a minimal repo. @ds300: I do have that property set to true. Below is my tsconfig.json

{
    "compilerOptions": {
        "module": "es6",
        "target": "es6",
        "jsx": "react",
        "outDir": "build",
        "rootDir": "src",
        "allowSyntheticDefaultImports": true,
        "noImplicitAny": true,
        "experimentalDecorators": true,
        "preserveConstEnums": true,
        "allowJs": true,
        "sourceMap": true,
        "moduleResolution": "node"
    },
    "include": [
        "type-declarations/**/*.d.ts",
        "src/**/*.ts",
        "src/**/*.tsx"
    ],
    "exclude": [
        "index.android.js",
        "index.ios.js",
        "build",
        "node_modules",
        "__tests__",
        "coverage"
    ],
    "compileOnSave": true
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't run Jest with typescript in React Native - Stack Overflow
I'm trying to setup Jest with Typescript on my React Native project but it ... I have followed the documentation on both jest/ts-jest...
Read more >
Creating snapshots in Jest for testing React applications
In this tutorial, I will lead you through using Jest, a JavaScript testing framework, to create snapshots for testing a simple React web ......
Read more >
Jest unit and snapshot testing with TypeScript in a next.js app
Running the test script with npm run test lets us know, that jest cannot find any test. Makes sense, since we have not...
Read more >
Unit Testing - Gatsby
While Jest is a general-purpose JavaScript unit testing framework, it has lots of features that make it work particularly well with React.
Read more >
Using TypeScript with React Native
ts if the file doesn't contain any JSX). If you tried to run the app now, you'd get an error like object prototype...
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