Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
111
rated 0 times [  114] [ 3]  / answers: 1 / hits: 35258  / 2 Years ago, sat, january 22, 2022, 12:00:00

I'm using jest to test a react TypeScript app.


This is the test I'm running:


import { render, screen } from '@testing-library/react'
import { toBeInTheDocument } from '@testing-library/jest-dom'

import ContextProvider from '../../context/ContextProvider'
import { BrowserRouter } from 'react-router-dom'
import BlogPage from './BlogPage'

describe('BlogPage', () => {

test('Render blog page', () => {
render(
<ContextProvider>
<BrowserRouter>
<BlogPage/>
</BrowserRouter>
</ContextProvider>
)

expect(screen.getByText('In this page you can see some of the last articles I wrote.')).toBeInTheDocument()
})

})

And this is the error I'm getting:


FAIL  src/components/blogPage/BlogPage.test.js
● Test suite failed to run

Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation

Details:

/home/German/Desktop/ger/code/projects/my-website/node_modules/react-markdown/index.js:6
export {uriTransformer} from './lib/uri-transformer.js'
^^^^^^

SyntaxError: Unexpected token 'export'

> 1 | import ReactMarkdown from 'react-markdown'
| ^
2 | import Accordion from 'react-bootstrap/Accordion'
3 |
4 | interface Props {

I understand this is because the library I'm using (react-markdown) doesn't have pre-compiled source code. The thing is I followed the docs (https://jestjs.io/docs/tutorial-react-native#transformignorepatterns-customization) and added the react-markdown folder to the transformIgnorePatterns config and I still get the error.


This is my jest.config.ts file:


import type { Config } from '@jest/types'

const config: Config.InitialOptions = {
verbose: true,
transform: {
'^.+\.ts?$': 'ts-jest'
},
transformIgnorePatterns: [
'node_modules/(?!react-markdown/)'
]
}
export default config

I tried adding <rootDir> like <rootDir>/node_modules/(?!react-markdown/) and It didn't make a difference.


I also tried configuring jest directly from package.json instead of a jest.config file and It didn't make a difference either.


Then I found this question: Jest transformIgnorePatterns not working, which mentions you need to configure Babel.


I created my app with create-react-app so I didn't have Babel on my app. I installed it and created a babel.config.js file inside of which I put:


module.exports = {
"presets": [
"@babel/preset-env"
]
};

But I still get the error...
I'm running out of ideas. Any clue of how could I solve this?


Full code can be found here: https://github.com/coccagerman/my-website


More From » reactjs

 Answers
2

react-markdown is shipped as js, add babel-jest as a transformer in your jest config


  transform: {
'^.+\.ts?$': 'ts-jest',
"^.+\.(js|jsx)$": "babel-jest"
},


[#50078] Wednesday, December 8, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ignacio

Total Points: 467
Total Questions: 128
Total Answers: 79

Location: Luxembourg
Member since Tue, Mar 14, 2023
1 Year ago
;