首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >运行jest test命令时,React native typescript抛出错误

运行jest test命令时,React native typescript抛出错误
EN

Stack Overflow用户
提问于 2018-07-17 20:51:55
回答 0查看 1.1K关注 0票数 0

我已经使用typescript运行react原生项目,并且我正在尝试设置测试。我遵循了这篇关于如何设置它的文章,在我运行test命令之前,一切似乎都正常。

jest

tsx运行文件时,我收到以下错误

测试运行失败,因为jest无法重新打开tsx文件

代码语言:javascript
代码运行次数:0
运行
复制
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)

你知道问题出在哪里吗?

测试用例

代码语言:javascript
代码运行次数:0
运行
复制
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

已更新

测试运行,但jest失败,因为它不能解析typescript文件

错误

代码语言:javascript
代码运行次数:0
运行
复制
 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 | });
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51381882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档