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


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

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


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);

Related Query

More Query from same tag