[英]Error when running jest on a react native + typescript app (Jest encountered an unexpected token)
[英]React native typescript throws error when running jest test command
我已经对使用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 | });
看起来您的代码基于本文底部的代码段:
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()
})
如果您的代码与您复制到问题中的代码匹配,那么您将缺少}
和)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.