运行Jest测试命令时,React本机打字稿会引发错误

时间:2018-07-17 12:51:55

标签: reactjs typescript react-native jestjs

我对使用Typescript运行的本机项目有反应,我正在尝试设置测试。我按照本文介绍了如何进行设置,直到运行测试命令,一切似乎都可以正常工作。

jest

tsx运行文件时,出现此错误

测试运行但失败,因为开玩笑无法重新处理tsx文件

TypeScript compiler encountered syntax errors while transpiling. Errors: '}' expected.

  at createTranspilationError (node_modules/ts-jest/dist/transpiler.js:32:12)
  at transpileViaTranspileModule (node_modules/ts-jest/dist/transpiler.js:24:15)
  at Object.transpileTypescript (node_modules/ts-jest/dist/transpiler.js:7:27)
  at process (node_modules/ts-jest/dist/preprocessor.js:27:40)
  at Object.process (node_modules/ts-jest/index.js:8:51)

知道可能是什么问题吗?

测试用例

import React from "react"
import "react-native"
import renderer from "react-test-renderer"
import App from "../App"

test("renders <App /> without crashing", () => {
  const tree = renderer.create(
    <App />,
  )

  expect(tree).toBeDefined()

文章 https://medium.com/@rintoj/react-native-with-typescript-40355a90a5d7

已更新

运行测试,但由于无法解析打字稿文件而开玩笑

错误

 FAIL  src/__tests__/App.test.tsx
  ● Test suite failed to run

    /Users/marcussimmesgard/driver-application/src/__tests__/App.test.tsx: Unexpected token (11:54)

      Jest encountered an unexpected token
      This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
      By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
      Here's what you can do:
       • 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/en/configuration.html
      Details:
         9 | const react_1 = __importDefault(require("react"));
        10 | test('renders correctly', () => {
      > 11 |     const tree = react_test_renderer_1.default.create(<App_1.default />);
           |                                                       ^
        12 |     expect(tree).toBeDefined();
        13 | });

1 个答案:

答案 0 :(得分:0)

您的代码似乎基于文章底部的代码段

import 'react-native'
import renderer from 'react-test-renderer'
import { App } from './app'
import React from 'react'

test('renders correctly', () => {
  const tree = renderer.create(
    <App />
  )
  expect(tree).toBeDefined()
})

如果您的代码与您复制到问题中的代码匹配,那么您将缺少结尾}和结尾)