Ger*_*cca 32 javascript testing unit-testing reactjs jestjs
我正在使用 jest 来测试 React TypeScript 应用程序。
\n这是我正在运行的测试:
\nimport { render, screen } from \'@testing-library/react\'\nimport { toBeInTheDocument } from \'@testing-library/jest-dom\'\n\nimport ContextProvider from \'../../context/ContextProvider\'\nimport { BrowserRouter } from \'react-router-dom\'\nimport BlogPage from \'./BlogPage\'\n\ndescribe(\'BlogPage\', () => {\n\n test(\'Render blog page\', () => {\n render(\n <ContextProvider>\n <BrowserRouter>\n <BlogPage/>\n </BrowserRouter>\n </ContextProvider>\n )\n\n expect(screen.getByText(\'In this page you can see some of the last articles I wrote.\')).toBeInTheDocument()\n })\n\n})\nRun Code Online (Sandbox Code Playgroud)\n这是我收到的错误:
\nFAIL src/components/blogPage/BlogPage.test.js\n \xe2\x97\x8f Test suite failed to run\n\n Jest encountered an unexpected token\n\n Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.\n\n Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.\n\n By default "node_modules" folder is ignored by transformers.\n\n Here\'s what you can do:\n \xe2\x80\xa2 If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.\n \xe2\x80\xa2 If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript\n \xe2\x80\xa2 To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.\n \xe2\x80\xa2 If you need a custom transformation specify a "transform" option in your config.\n \xe2\x80\xa2 If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.\n\n You\'ll find more details and examples of these config options in the docs:\n https://jestjs.io/docs/configuration\n For information about custom transformations, see:\n https://jestjs.io/docs/code-transformation\n\n Details:\n\n /home/German/Desktop/ger/code/projects/my-website/node_modules/react-markdown/index.js:6\n export {uriTransformer} from \'./lib/uri-transformer.js\'\n ^^^^^^\n\n SyntaxError: Unexpected token \'export\'\n\n > 1 | import ReactMarkdown from \'react-markdown\'\n | ^\n 2 | import Accordion from \'react-bootstrap/Accordion\'\n 3 |\n 4 | interface Props {\nRun Code Online (Sandbox Code Playgroud)\n我明白这是因为我正在使用的库(react-markdown)没有预编译的源代码。问题是我遵循了文档(https://jestjs.io/docs/tutorial-react-native#transformignorepatterns-customization)并将react-markdown文件夹添加到transformIgnorePatterns配置中,但我仍然收到错误。
\n这是我的 jest.config.ts 文件:
\nimport type { Config } from \'@jest/types\'\n\nconst config: Config.InitialOptions = {\n verbose: true,\n transform: {\n \'^.+\\\\.ts?$\': \'ts-jest\'\n },\n transformIgnorePatterns: [\n \'node_modules/(?!react-markdown/)\'\n ]\n}\nexport default config\nRun Code Online (Sandbox Code Playgroud)\n我尝试添加<rootDir>类似内容<rootDir>/node_modules/(?!react-markdown/),但没有什么区别。
我还尝试直接从 package.json 而不是 jest.config 文件配置 jest,它也没有什么区别。
\n然后我发现这个问题:Jest transformIgnorePatterns not running,其中提到你需要配置 Babel。
\n我使用 create-react-app 创建了我的应用程序,所以我的应用程序上没有 Babel。我安装了它并创建了一个 babel.config.js 文件,在其中放入:
\nmodule.exports = {\n "presets": [\n "@babel/preset-env"\n ]\n};\nRun Code Online (Sandbox Code Playgroud)\n但我仍然收到错误...\n我已经没有想法了。我该如何解决这个问题有任何线索吗?
\n完整代码可以在这里找到: https: //github.com/coccagerman/my-website
\nAda*_*dam 15
react-markdown作为 js 提供,babel-jest作为变压器添加到你的笑话配置中
transform: {
'^.+\\.ts?$': 'ts-jest',
"^.+\\.(js|jsx)$": "babel-jest"
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50809 次 |
| 最近记录: |