Cannot get ts-jest to create Snapshots when using React-native
See original GitHub issueI 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:
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:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!Yes I will try to create a minimal repo. @ds300: I do have that property set to true. Below is my tsconfig.json