ts-jest: JSX: Unexpected token

hi!

I’m using example of ‘jest’ configuration from readme.md and this simple test

const renderer = require('react-test-renderer')

it('renders correctly', () => {
  const tree = renderer.create(<div>hi!</div>).toJSON()
  expect(tree).toMatchSnapshot()
})

gives the next Error:

   var tree = renderer.create(<div>hi!</div>).toJSON();
                                   ^
    SyntaxError: Unexpected token <

      at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:285:10)
      at process._tickCallback (internal/process/next_tick.js:103:7)

Any ideas? I’d be grateful.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 20 (11 by maintainers)

Most upvoted comments

was there any resolution about this? I’m getting a similar error where I get a Syntax error while trying to use jsx within my tests 😭

That is used by the part of the preprocessor that takes care of displaying the correct line numbers in the typescript files. If you remove this, any failed tests you see will show you the line numbers in the compiled JS file.

@mkozhukharenko I’ve pushed a fix and have updated the npm package.

With these changes, I was able to successfully run npm test -- -u on the repo you shared, with one slight modification - I had to rename Button.test.ts to Button.test.ts. This is required by Typescript.

Let me know if you have any more issues with this.

Thanks for opening an issue and for creating a sample repo to help reproduce this issue!