NextJS-auth0 发生意外令牌错误

问题描述 投票:0回答:1

我有一个使用 nextjs-auth0 包的 nextjs 应用程序。当我尝试使用此导入测试文件时:

import { getSession } from '@auth0/nextjs-auth0';

我收到以下错误:

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:

    /Users/someuser/app-project/node_modules/@panva/hkdf/dist/web/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import derive from './runtime/hkdf.js';

    SyntaxError: Cannot use import statement outside a module

  15 |
  16 | export default apiHandler(handler);
> 17 |
     | ^
  18 | export async function handler(
  19 |     req: NextApiRequest,
  20 |     res: NextApiResponse<User[] | ArityErrorResponse>

仅当测试的文件具有与

@auth0/nextjs-auth0
包相关的导入时,才会发生此错误。

我尝试忽略 jest 配置(

@panva
)中的相关包或 nextjs auth0 包本身,但没有成功

transformIgnorePatterns: ['/node_modules/@auth0/nextjs-auth0/']

我试图嘲笑这个包,但不幸的是它不允许我这样做。我怀疑

nextjs-auth0
包正在使用现代 JS,而 jest 使用
commonjs
,这对于 jest 来说可能是冲突。

有人遇到过这个包和笑话的问题吗?

reactjs typescript next.js jestjs
1个回答
0
投票

我找到了解决方案,我在

moduleNameMapper
文件中的
jest.config
内添加了这个配置

// jest.config
const customJestConfig = {
  // ...
  moduleNameMapper: {
    // ...
    "^@panva/hkdf": require.resolve("@panva/hkdf"),
    // ...
  },
  // ...
};

这解决了

@panva
中的 Common JS 模块,而不是使用现代 JS 的笑话。

© www.soinside.com 2019 - 2024. All rights reserved.