Ts-jest: Getting unexpedted token for jsx. [Question], [Help].

Created on 7 Sep 2017  路  2Comments  路  Source: kulshekhar/ts-jest

With the following ...

// __tests___/index.tsx
import {Text} from 'react-native';
import React from 'react';
import renderer from 'react-test-renderer';

it('renders correctly', () => {
  const tree = renderer.create( <Text /> ).toJSON();
  expect(tree).toMatchSnapshot();
});
// package.json
    "jest": {
      "preset": "react-native",
      "moduleDirectories": [ "node_modules", "<rootDir>" ],
      "transform": {
        ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
      },
      "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
      "moduleFileExtensions": [ "ts", "tsx", "js", "json" ]
    }

I get this :/

        3 | import renderer from 'react-test-renderer';
        4 | it('renders correctly', () => {
      > 5 |     const tree = renderer.create(<Text />).toJSON();
          |                                  ^
        6 |     expect(tree).toMatchSnapshot();
        7 | });

Most helpful comment

Changing "jsx": "react-native" to just "jsx": "react" in my tsconfig.json fixed the issue.

something like

// tsconfig-for-jest.json
{
  "compilerOptions": {
    "target": "es2015",
    "module": "es2015",
    "jsx": "react",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true
  }
}

And ...

// package.json

"jest": {
  "globals": {
    "ts-jest": {
      "tsConfigFile": "tsconfig-for-jest.json"
    }
  },
  "preset": "react-native",
  "transform": {
    "^.+\\.jsx?$": "<rootDir>/node_modules/babel-jest",
    "^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
  },
  "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json"
  ]
}

All 2 comments

Changing "jsx": "react-native" to just "jsx": "react" in my tsconfig.json fixed the issue.

something like

// tsconfig-for-jest.json
{
  "compilerOptions": {
    "target": "es2015",
    "module": "es2015",
    "jsx": "react",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true
  }
}

And ...

// package.json

"jest": {
  "globals": {
    "ts-jest": {
      "tsConfigFile": "tsconfig-for-jest.json"
    }
  },
  "preset": "react-native",
  "transform": {
    "^.+\\.jsx?$": "<rootDir>/node_modules/babel-jest",
    "^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
  },
  "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json"
  ]
}

why need rewrite "jsx": "react-native", to "jsx": "react"? can explain?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stephenotalora picture stephenotalora  路  3Comments

Slessi picture Slessi  路  3Comments

golddranks picture golddranks  路  3Comments

RiJung picture RiJung  路  4Comments

artola picture artola  路  3Comments