首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Next.js: Jest无法在`node_modules`中处理文件,从而导致错误“不能使用模块外的导入语句”

Next.js: Jest无法在`node_modules`中处理文件,从而导致错误“不能使用模块外的导入语句”
EN

Stack Overflow用户
提问于 2022-04-11 22:55:07
回答 1查看 1.8K关注 0票数 2

我使用Next.js,并使用正式指示安装Jest。但是,当我对使用remark模块的组件运行测试时,会得到以下错误:

代码语言:javascript
复制
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:

/Users/mk/Code/github/hollowverse/hollowverse/node_modules/remark/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import {unified} from 'unified'
                                                                                  ^^^^^^

SyntaxError: Cannot use import statement outside a module

  1 | import matter from 'gray-matter';
> 2 | import { remark } from 'remark';
    |                                ^
  3 | import remarkHtml from 'remark-html';
  4 | import { sanityClient } from '~/lib/components/sanityio';
  5 | import { groqRelatedPeople } from '~/lib/[celeb]/getStaticProps/groqRelatedPeople';

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
  at Object.<anonymous> (lib/[celeb]/getStaticProps/getParsedOldContent.ts:2:32)

我的jest.config.js如下

代码语言:javascript
复制
// jest.config.js
const tsconfig = require('./tsconfig.json');
const moduleNameMapper = require('tsconfig-paths-jest')(tsconfig);
const nextJest = require('next/jest');

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
const customJestConfig = {
  // Add more setup options before each test is run
  // setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
  // if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
  moduleDirectories: ['node_modules', '<rootDir>/'],
  testEnvironment: 'jest-environment-jsdom',
  moduleNameMapper: {
    ...moduleNameMapper,
    '^lodash-es$': 'lodash',
  },
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig);

有人知道怎么解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2022-04-12 04:43:19

在使用Webpack创建的React应用程序中设置Jest时,我也遇到了同样的错误。我不得不加上@babel/预设-env,它是固定的。我也写了一篇关于同样的博客文章。

代码语言:javascript
复制
{ 
  "presets": ["@babel/react", "@babel/env"]
}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71835186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档