score:-1
if you're testing jsx(e.g components) you should use the .tsx extension if you're not testing jsx(e.g reducers) you should use .ts
you should also have a jest.config.js file, find the key "modulefileextensions" to se all available file extensions that jest accepts
score:0
you should use .test.tsx extension if a test file contains embedded xml-like jsx code (but not because you are testing component).
according to is there any downside to using .tsx instead of .ts all the times in typescript?
with the .tsx files you could use the all jsx (javascript xml) code.
in the .ts file you can only use just typescript.
if you have test written in pure typescript, .test.tsx is a wrong extension.
extensions are used to describe type of content in the file. if it is a test file written in typescript, it should be .test.ts, if it is written in javascript, it should be .test.js.
the extension indicates a characteristic of the file contents or its intended use. from https://en.wikipedia.org/wiki/filename_extension
if you try to use .test.ts extension with test files that include jsx/html elements declarations, it can cause errors as described in "navbar refers to a value, but is being used as a type here" when trying to render a shallow copy of my component when testing. for such files using .test.tsx is appropriate.
note that for javascript .test.js extension is good for any type of tests, with or without jsx elements
score:4
can i use the
tsx
extension for test files if using react with typescript?
yes. you can use the *.tsx
extension for test files if using react with typescript.
any leads would be appreciated.
there are multiple ways to setup jest
with typescript. for one example try running this script from a terminal:
npx create-react-app my-app --typescript
cd my-app
npm run eject
that will generate a project that is configured to run tests with the *.tsx
extension. you can see its jest
configuration inside the src/package.json
file and an example test inside the src/app.test.tsx
file. the latter looks like this:
import react from 'react';
import reactdom from 'react-dom';
import app from './app';
it('renders without crashing', () => {
const div = document.createelement('div');
reactdom.render(<app />, div);
reactdom.unmountcomponentatnode(div);
});
Source: stackoverflow.com
Related Query
- can i use the tsx extension for test files if using react with typescript
- How to use typescript with the definition of custom styles for a select using react-select
- Jest test for a copy to clipboard method using react with typescript
- how can my call the action in my component using typescript with react redux?
- How to setup a generic interface for the state when using React useReducer with typescript
- When using react cdn links for a site does it require the use of class components or can I use function components?
- I'm using React using typescript with useContext and useReducer in my app, but for some reason the click won't trigger the dispatch
- How can I use different properties for the same component using Styled Component for React JS?
- What is the difference between .ts and .tsx extensions. Both are used as extensions for typescript files in react. So where should we use them?
- How can I test a change handler for a file-type input in React using Jest/Enzyme?
- Getting Error: "All files must be modules when the '--isolatedModules' flag is provide" in React with TypeScript
- How to use forwardRef with FunctionComponent in React Native using TypeScript
- Parcel: using separate SCSS files for each React component, but with a variables file
- How to use jest.spyOn with React function component using Typescript
- Use ts for tsx react files
- Connecting a component with OwnProps using react redux and typescript in the .NET Core 2.0 reactredux template project
- How can I define the type for a <video> reference in React using Typescript?
- Recharts is not working for the React with typescript
- How can I test css properties for a React component using react-testing-library?
- Can I use Create React App Jest Testing with a test reporter without ejecting?
- Using the same component for different Routes with React Router
- How to set up a chrome extension using React and TypeScript with multiple pages and entry points?
- Minified React error #310; use the non-minified dev environment for full errors and additional helpful warnings using Chrome through Selenium in React
- How can I uglify my bundle files for React with gulp?
- react create app, typescript unit test with mocha and chai what is the correct setup to support es6 modules?
- How can i write a test using React Testing Library to check if hover changes the components style?
- Test suits failed with "SyntaxError: Unexpected token 'export" ' react typescript using jest
- How can I render the react app with redux using the ReactJS.NET?
- How can I use create-react-app for React version 16.x.x with TypeScript?
- How to use React.forwardRef with TypeScript when using different html elements in the render
More Query from same tag
- In React applications, does the front-end receive the original React code or plain html?
- Potentially unhandled rejection [1] TypeError: Cannot read property 'setState' of undefined
- Img bullet points appear above paragraph React.js
- React: Passing state to child component
- How to useFetch with a for loop?
- React-Router Switch with <Router>..</Router> block (component with props) cannot read URL param with "props.match.params"
- How to recursively inject props to each React component in a tree?
- POST method not supported using Laravel 6 and ReactJS?
- Prevent page scrolling when mouse is over one particular div
- A react `useReducer` with some dependencies
- I am unable to get my login page to disappear after I hit "Sign In"
- React functional component, how to pass attributes to an input element dynamically via an onChange event
- How to make a collapse text view using pure Reactjs?
- Why can't I access the sub-properties of a JSON object fetched from my API in express
- Storybook cannot pass register prop into stories
- How to trigger a callback after a redux action finishes and updates redux store?
- Typescript - How Multiple Type situation in React
- npm serial-port get the output to react component
- React - Fill select dropdown with data from API
- Strange behaviour in React component that includes message listener
- How to pull all objects from an array of unknown length and place them into a state variable in React?
- How to update multiple form values with one reducer action?
- ReactJS + Apollo Client - Get result without rendering
- React Native ScrollView Wrapping for Complex Elements
- ReactDOM.render(element, container [,callback])
- How can a unique instance of a React component respond to the result of an async action using Flux?
- Jest test: Awating multiple promises in connected component
- Axios can't get object by ID react
- How to update state in child component with context value from parent (class components)
- How can I make my filtering and mapping of arrays more compact and less repetitive?