score:1
update: while this was accepted, the much better answer was provided by @alphamz below (.tsx vs .ts). that said i would still suggest create-react-app or create-next-app (much better in 2021) for react newbies who want to get up and running quickly, then you're starting with good time-tested settings.
original answer:
for someone new to react
and jest
like yourself, i would strongly suggest starting with create-react-app
with the typescript
extension
https://facebook.github.io/create-react-app/docs/adding-typescript
this gives a good baseline for ts usage with jest support (has pretty strong app defaults as well)
yarn create react-app my-app --typescript
from there, use react-testing-library
here:
https://testing-library.com/docs/react-testing-library/intro
and you can write tests like this
import { render } from 'react-testing-library';
test('init gametotaler', () => {
const myname: string = 'foo';
const { getbytext } = render(
<app name={foo} />
);
expect(getbytext(`hello, ${foo}`)).tobetruthy();
});
@testing-library (that react-testing-library is part of) is extremely well written and supported by kent c. dodds, who is a remarkably strong author of react articles and on testing in general.
he has recently written why shallow rendering (which is what is done in enzyme) can be an anti-pattern (at least for him) - https://kentcdodds.com/blog/why-i-never-use-shallow-rendering
note that create-react-app
does hide things inside its generated framework, but is well supported.
one strong plus - if you want to completely pull out of create-react-app once it's generated your codebase, you can run react-scripts eject
and then you can see the secret sauce while still having a fully buildable codebase.
hope this helps - good luck!
score:0
reactjs.org recommends react testing library.
if you want to use it with jest and also need to test typescript code, then you might have a look at crisp-react sample project. it has a react client and the tests can be found here.
score:7
i know it's a old question, but i bumped into the same problem and it has a simple solution: your file name must have the .tsx extension for using jsx tag.
Source: stackoverflow.com
Related Query
- How to test with jest and typescript with types a basic react function component
- How to test a React component that has Router, Redux and two HOCs... with Jest and Enzyme?
- How to check the value of a nested React component in a unit test with Enzyme and Jest
- How to test styles and media queries rendered by a React component with Jest and/or Enzyme
- How to test a React component that update over time with Jest and Enzyme?
- How to Test a function with Jest and React
- Jest with React - How do you render an entire component and test it?
- How To Unit Test React Component With react-intl, react-router-dom v4 and TypeScript
- How to test React component with children using jest and enzyme?
- How to test asynchronous function with rtl and jest in React
- How to test if state of a component is changed by the function using jest and Enzyme in React TDD
- how to test component while passing boolean value with react testing library and jest
- How to test a className with the Jest and React testing library
- How to mock React component methods with jest and enzyme
- Test a React Component function with Jest
- React prop types with TypeScript - how to have a function type?
- How to test a component with a nested container with React and Redux?
- How to test form submission in React with Jest and Enzyme? Cannot read property 'preventDefault' of undefined
- How to test snapshots with Jest and new React lazy 16.6 API
- How to use jest.spyOn with React function component using Typescript
- How to test the state of a functional component in React with Jest (No Enzyme)
- How to read console.log from a mounted component with Enzyme and Jest in Create React App
- Testing debounced function in React component with Jest and Enzyme
- How to mock a React component lifecycle method with Jest and Enzyme?
- how can I test if useState hook has been called with jest and react testing library?
- How to test if a React component contains another component with Tape and Enzyme?
- How to test redux state update with react testing library and jest
- How to test a React component that uses context like in react-router 2.0 with Jest 0.8.x
- How to test functions in React with Jest and Enzyme
- Stateful React component with async function failing Jest test
More Query from same tag
- useRef() error: cannot set property disabled of null when trying to focus on a textarea
- Uncaught Error: Cannot find module "./RandomWords"
- How to remove items in React?
- React JS - What is a good way to handle children click event?
- How to change cart button "ADD" to some other name
- Warning: Encountered two children with the same key for react infinite scroll
- Puppeteer - how to use page.evaluateHandle
- Specifying complex PropTypes for children
- Pass Props to this.props.children
- React-Spring: Invalid Hooks Call
- Uncaught (in promise) Error: Network Error at createError
- DELETE user Express side works, but can't make it work on front-end side (using React)
- Update a state in react redux reducer with nested .map() for nested arrays
- Primereact: Warning: Encountered two children with the same key,
- react component this.value is null even when it's populated
- TypeScript React Js, Type 'void' is not assignable to type '((event: MouseEvent<HTMLDivElement, MouseEvent>) => void) | undefined'
- React state adds 1 but subtracts 2
- How to pass functions as props to React Custom Modal?
- React router nested not working
- How to find out memory leaks in react native app?
- read cookies in front (Reactjs) that comes from nodejs
- Who to follow suggestions function in Nodejs?
- MaterialUI - 'StylesProvider' cannot be used as a JSX component
- React JS add 'disabled' class to multiple indexes
- How to make a border-radius for Skeleton by react-loading-skeleton?
- React: pull dependent data from custom hooks into component without re-rendering when non-dependent data changes
- Problem to render a component dynamically with React Router
- How should I use redux with nested subcomponents that won't be reused?
- Delete `,` prettier/prettier and Parse errors in imported module './routes'
- How do I ensure I won't replace a content to an old response?