[code]Button.spec.tsx[/code]
Код: Выделить всё
import {describe, it, expect} from "@jest/globals";
import {render} from "@testing-library/react-native";
import Button from "./Button.tsx";
import React from "react";
describe("Button", () => {
it("renders the button with text", () => {
expect(true).toBe(true);
render(Click me);
});
});
< /code>
Тем не менее, тест не проходит с длительной ошибкой. Вот важная часть: < /p>
/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/react-native/index.js:28
import typeof * as ReactNativePublicAPI from './index.js.flow';
^^^^^^
SyntaxError: Cannot use import statement outside a module
1 | import {describe, it, expect} from "@jest/globals";
> 2 | import {render} from "@testing-library/react-native";
| ^
3 | import Button from "./Button.tsx";
4 | import React from "react";
5 |
< /code>
Я много пытался исправить эту ошибку и чувствую, что я попал в стену. Вот все:
[list]
[*] Я попытался создать свою конфигурацию с нуля с помощью Jest CLI
[*] Использование react-dative
[*] Использование jswithbabelpreset
[*] use-jest с Transfors
[*] TS-JEST Config Helper, см. Здесь
[*] Использование расширений файлов модулей
[*] Вместо этого используя вавильс /> Использование вабеля/плагина-трансформ-модул-commonjs
[*] Использование экспериментальных VM-модулей
[*] Преобразование всех Node_Modules, которые привели только к новым ошибкам
[/list]
вот мои текущие файлы конфигурации:
.
Код: Выделить всё
babel.config.js
Код: Выделить всё
/** @type {import('@babel/core').ConfigFunction} */
export default {
presets: ["module:metro-react-native-babel-preset"],
plugins: [
"@babel/plugin-transform-flow-strip-types",
"@babel/plugin-transform-modules-commonjs",
],
};
< /code>
[h4]jest.config.ts
Код: Выделить всё
import type {Config} from "jest";
import {createJsWithBabelPreset} from "ts-jest";
const jsWithBabelPreset = createJsWithBabelPreset({
tsconfig: "tsconfig.json",
babelConfig: true,
});
const jestConfig: Config = {
preset: "react-native",
transform: jsWithBabelPreset.transform,
transformIgnorePatterns: [
"/node_modules/(?!(@react-native|react-native)/).*/",
],
moduleFileExtensions: ["ts", "tsx", "js", "flow"],
};
export default jestConfig;
< /code>
[h4]tsconfig.json
Код: Выделить всё
{
"extends": "../typescript-config/react-library.json",
"compilerOptions": {
"jsx": "react",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ESNext",
"esModuleInterop": true,
"allowImportingTsExtensions": true,
"allowJs": true
}
}
< /code>
[h4]tsconfig.json
Код: Выделить всё
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@react-native/typescript-config",
"compilerOptions": {
"jsx": "react-jsx",
"allowImportingTsExtensions": true,
"noEmit": true
}
}
< /code>
[h4]Test command[/h4]
node --experimental-vm-modules node_modules/jest/bin/jest.js
Код: Выделить всё
"@testing-library/react-native": "^12.9.0",
"babel-jest": "^30.0.1",
"jest": "^30.0.0",
"metro-react-native-babel-preset": "^0.77.0",
"ts-jest": "^29.4.0",
"@babel/core": "catalog:",
"@babel/plugin-transform-flow-strip-types": "^7.27.1",
"@babel/plugin-transform-private-methods": "^7.27.1",
"@babel/plugin-transform-private-property-in-object": "^7.27.1",
"@babel/plugin-transform-react-jsx": "^7.27.1",
"@babel/preset-env": "^7.27.2",
"@babel/preset-react": "^7.27.1",
"@jest/globals": "^30.0.0",
System specs:
[*]Node v23.4.0
[*]macOS 15.5
[*]pnpm 10.12.1
turbo 2.5.4 < /li>
< /ul>
Что я должен сделать, чтобы исправить это. < /P>
p.s. Мне уже пришлось исправить код потока нативного пакета в рамках.>
Подробнее здесь: https://stackoverflow.com/questions/796 ... f-a-module