Jest 在没有 root 权限的情况下运行时抛出异常

Jest throws exception when running without root permission

提问人:aholake 提问时间:11/10/2023 最后编辑:Mattaholake 更新时间:11/10/2023 访问量:10

问:

我在 Typescript React 项目中遇到 Jest 问题。

节点:16.10.0 开玩笑:26.6.3 巴别塔:26.6.3

当我运行命令对文件执行测试时,我收到带有堆栈跟踪的错误消息,如下所示:jest <file-name

Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • 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/en/configuration.html

    Details:

    /Users/<username>/<my_project>/test/jest.setup.ts:11
    import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)

似乎转换器无法使用 esm6 模块加载打字稿文件。 我意识到这种情况发生在我机器上的所有项目上,而其他队友则不会。 特别是,当我获得许可运行时,不会发生此问题。babel-jestsudo

这些是我尝试过的一些操作:

  • 全部删除并重新安装。node_modules
  • 重新安装的节点
  • 更改某些可疑文件夹和文件的所有者。就像从 root -> 当前用户进行全局node_module一样。
  • 添加到package.json。type: module
reactjs node.js jestjs babeljs babel-jest

评论


答: 暂无答案