imp*_*tle 3 testing commonjs jestjs es6-modules react-testing-library
我将 tRPC 与 Typescript 和 Next.js 结合使用。直到最近我的测试才失败(我认为我没有碰过它们)。我得到了错误Unexpected token 'export'。我尝试过更改我的jest.config.mjs添加transformIgnorePatterns: ["!node_modules/"]以及babel-jest
transform: {\n "^.+\\\\.tsx?$": "ts-jest",\n "^.+\\\\.(js|jsx)$": "babel-jest",\n "node_modules/variables/.+\\\\.(j|t)sx?$": "babel-jest"\n },\nRun Code Online (Sandbox Code Playgroud)\n但无济于事:(有人可以帮助我吗?这是我的错误日志。
\nTest 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/runner/work/workout-app/workout-app/node_modules/jose/dist/browser/index.js:1\n ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { compactDecrypt } from './jwe/compact/decrypt.js';\n ^^^^^^\n\n SyntaxError: Unexpected token 'export'\n\n 19 | return `http://localhost:${process.env.PORT ?? 3000}`\n 20 | }\n > 21 |\n | ^\n 22 | export const trpc = createTRPCNext<AppRouter>({\n 23 | // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars\n 24 | config(opts) {\n\n at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1495:14)\n at Object.<anonymous> (node_modules/@supabase/auth-helpers-shared/src/utils/cookies.ts:3:27)\n at Object.<anonymous> (node_modules/@supabase/auth-helpers-nextjs/src/clientComponentClient.ts:6:8)\n at Object.<anonymous> (src/utils/trpc.ts:21:28)\n at Object.<anonymous> (src/hooks/useMutationDeleteWorkoutPlan.tsx:11:15)\n at Object.<anonymous> (src/components/Workouts/index.tsx:16:78)\n at Object.<anonymous> (src/__tests__/jest/Workouts.test.tsx:9:58)\nRun Code Online (Sandbox Code Playgroud)\n这是我的jest.config.mjs
import nextJest from "next/jest.js"\n\nconst createJestConfig = nextJest({\n // Provide the path to your Next.js app to load next.config.js and .env files in your test environment\n dir: "./",\n})\n\n// Add any custom config to be passed to Jest\n/** @type {import('jest').Config} */\nconst config = {\n // Add more setup options before each test is run\n // setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],\n testEnvironment: "jest-environment-jsdom",\n}\n\n// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async\nexport default createJestConfig(config)\n\nRun Code Online (Sandbox Code Playgroud)\n
小智 5
对于那些希望找到解决方案的人来说,这是我的:
添加"^jose": require.resolve("jose"),到moduleNameMapper笑话配置(以及具有相同问题的其他模块......)。我还必须使用玩笑设置文件来修复 TextEncoder 问题。
// jest.config
const customJestConfig = {
setupFiles: ["./setup.jest.ts"],
moduleNameMapper: {
"^jose": require.resolve("jose"),
"^@panva/hkdf": require.resolve("@panva/hkdf"),
"^preact-render-to-string": require.resolve("preact-render-to-string"),
"^preact": require.resolve("preact"),
"^uuid": require.resolve("uuid")
},
//...
};
// setup.jest.ts
const { TextEncoder, TextDecoder } = require("util");
Object.assign(global, { TextDecoder, TextEncoder });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1610 次 |
| 最近记录: |