简体   繁体   English

Next.js 无法在第三方库的模块外使用导入语句

[英]Next.js Cannot use import statement outside a module in third party library

Summary概括

I'm using Next.js to create both API and and Frontend app.我正在使用 Next.js 创建 API 和前端应用程序。 Frontend uses react.前端使用反应。 To unit testing I'm using next/jest preset and jest as an unit testing library.对于单元测试,我使用next/jest预设和 jest 作为单元测试库。 I'm using @tenstack/react-query to state management and when I want to test page, it throws an error.我正在使用@tenstack/react-query进行 state 管理,当我想测试页面时,它会引发错误。 In tests where I'm using jsdom environment I add在我使用jsdom环境的测试中,我添加了

/**
 * @jest-environment jsdom
 */

in tests to API I'm using babel-jest that is default and no need anything在对 API 的测试中,我使用的是默认babel-jest ,不需要任何东西

error that throws抛出的错误


    /path/to/project/node_modules/@tanstack/react-query/src/QueryClientProvider.tsx:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import * as React from 'react'
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

The error appers in test where I want to render page that uses react-query错误出现在我想渲染使用react-query页面的测试中

jest.config.js jest.config.js

// jest.config.js
const nextJest = require('next/jest');

const createJestConfig = nextJest({
  dir: './'
});

const customJestConfig = {
  moduleDirectories: ['node_modules', '<rootDir>/'],
  transform: {
    '^.+\\.[t|j]sx?$': 'ts-jest'
  }
};

module.exports = createJestConfig(customJestConfig);

.babelrc .babelrc

{
  "presets": ["next/babel","@babel/preset-env", "@babel/preset-typescript"],
  "plugins": [
    "babel-plugin-transform-typescript-metadata",
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    "babel-plugin-parameter-decorator"
  ]
}

I've tried these answers but unfortunatelly it didn't help.我已经尝试过这些答案,但不幸的是它没有帮助。

I solved myself an issue by mocking library @tenstack/react-query .我通过 mocking library @tenstack/react-query解决了自己的一个问题。 Here is an sample code这是一个示例代码

jest.mock('@tanstack/react-query/src/QueryClientProvider', () => ({
  QueryClientProvider: ({ children }) => children
}));

jest.mock('@tanstack/react-query', () => ({
  QueryClient: jest.fn(),
  useQueryClient: jest.fn(),
  useQuery: jest.fn(),
  useMutation: jest.fn().mockReturnValue({
    mutate: jest.fn(),
    isLoading: false
  })
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Next.js 中导入 react-hook-mousetrap 时出现“无法在模块外使用导入语句”错误 - "Cannot use import statement outside a module" error when importing react-hook-mousetrap in Next.js Next.js 和 Jest:语法错误:无法在模块外使用导入语句 - Next.js and Jest: SyntaxError: Cannot use import statement outside a module JS 导入导出语法错误:不能在模块外使用导入语句 - JS import export SyntaxError: Cannot use import statement outside a module SyntaxError:无法在 node.js 上的模块外部使用 import 语句 - SyntaxError: Cannot use import statement outside a module on node.js 运行 js 测试 - 得到“不能在模块外使用 import 语句” - Running js tests - getting “Cannot use import statement outside of a module” Node.js:语法错误:无法在模块外使用导入语句 - Node.js: SyntaxError: Cannot use import statement outside a module Nodejs运行js报错:Cannot use import statement outside a module - Nodejs run js error: Cannot use import statement outside a module 无法在 node.js 中的模块外使用 import 语句 - Cannot use import statement outside a module in node.js JS npm 包,不能在模块外使用 import 语句 - JS npm package, Cannot use import statement outside a module 未捕获的语法错误:无法在节点 js 中的模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module in node js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM