我已经使用typescript运行react原生项目,并且我正在尝试设置测试。我遵循了这篇关于如何设置它的文章,在我运行test命令之前,一切似乎都正常。
jest
当tsx
运行文件时,我收到以下错误
测试运行失败,因为jest无法重新打开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
已更新
测试运行,但jest失败,因为它不能解析typescript文件
错误
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 | });
https://stackoverflow.com/questions/51381882
复制