我将 tRPC 与 Typescript 和 Next.js 结合使用。直到最近我的测试才失败(我认为我没有碰过它们)。我收到错误
Unexpected token 'export'
。我尝试更改我的 jest.config.mjs
以添加 transformIgnorePatterns: ["!node_modules/"]
以及 babel-jest
和
transform: {
"^.+\\.tsx?$": "ts-jest",
"^.+\\.(js|jsx)$": "babel-jest",
"node_modules/variables/.+\\.(j|t)sx?$": "babel-jest"
},
但无济于事:(有人可以帮助我吗?这是我的错误日志。
Test suite failed to run
Jest encountered an unexpected token
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.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• 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/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/home/runner/work/workout-app/workout-app/node_modules/jose/dist/browser/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { compactDecrypt } from './jwe/compact/decrypt.js';
^^^^^^
SyntaxError: Unexpected token 'export'
19 | return `http://localhost:${process.env.PORT ?? 3000}`
20 | }
> 21 |
| ^
22 | export const trpc = createTRPCNext<AppRouter>({
23 | // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
24 | config(opts) {
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1495:14)
at Object.<anonymous> (node_modules/@supabase/auth-helpers-shared/src/utils/cookies.ts:3:27)
at Object.<anonymous> (node_modules/@supabase/auth-helpers-nextjs/src/clientComponentClient.ts:6:8)
at Object.<anonymous> (src/utils/trpc.ts:21:28)
at Object.<anonymous> (src/hooks/useMutationDeleteWorkoutPlan.tsx:11:15)
at Object.<anonymous> (src/components/Workouts/index.tsx:16:78)
at Object.<anonymous> (src/__tests__/jest/Workouts.test.tsx:9:58)
这是我的
jest.config.mjs
import nextJest from "next/jest.js"
const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: "./",
})
// Add any custom config to be passed to Jest
/** @type {import('jest').Config} */
const config = {
// Add more setup options before each test is run
// setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
testEnvironment: "jest-environment-jsdom",
}
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
export default createJestConfig(config)
对于那些希望找到解决方案的人来说,这是我的:
将
"^jose": require.resolve("jose"),
添加到 jest 配置的 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 });