如何使Nrwl Nest.js Jest工作区与Firebase ES6包一起运行?

huangapple go评论53阅读模式
英文:

How to get Nrwl Nest.js Jest workspace working with firebase es6 packages?

问题

我有一个通过npx create-nx-workspace设置的 nrwl Angular-Jest 应用程序。

在开发这个项目的过程中,我已经从以下版本升级到:

"firebase": "9.1.3",
"firebase-admin": "9.12.0",
"firebase-functions": "3.15.7",

升级到

"firebase": "9.16.0",
"firebase-admin": "11.5.0",
"firebase-functions": "4.2.0",

以使扩展模拟器在本地工作。尽管它可以工作,但现在我在运行 jest 时遇到问题。它说 firebase 现在有 es6 导入,无法工作。以下是错误消息:

● 测试套件运行失败

Jest 遇到了意外的标记

Jest 无法解析文件。当您的代码或其依赖项使用非标准的 JavaScript 语法,或者当 Jest 没有配置来支持这种语法时,就会发生这种情况。

默认情况下,Jest 支持 Babel,将根据您的 Babel 配置将文件转换为有效的 JS。

默认情况下,"node_modules" 文件夹将被转换器忽略。

您可以做以下事情:
 • 如果您要使用 ECMAScript 模块,请参阅 https://jestjs.io/docs/ecmascript-modules 以了解如何启用它。
 • 如果您要使用 TypeScript,请参阅 https://jestjs.io/docs/getting-started#using-typescript
 • 要将某些 "node_modules" 文件转换,请在您的配置中指定自定义 "transformIgnorePatterns"。
 • 如果您需要自定义转换,请在您的配置中指定 "transform" 选项。
 • 如果您只想模拟非 JS 模块(例如二进制资源),您可以使用 "moduleNameMapper" 配置选项将它们替换为存根。

您可以在文档中找到有关这些配置选项的更多详细信息和示例:
https://jestjs.io/docs/configuration
有关自定义转换的信息,请参阅:
https://jestjs.io/docs/code-transformation

详细信息:

/Users/[justOnlyMe]/Desktop/[nrwlProject]/node_modules/firebase-admin/lib/esm/auth/index.js:1
({ "Object.<anonymous>": function(module,exports,require,__dirname,__filename,jest){import mod from "../../auth/index.js";
                                                                                  ^^^^^^

语法错误:无法在模块之外使用 import 语句

  在 Runtime.createScriptFromCode (../../../node_modules/jest-runtime/build/index.js:1728:14)
  在 Object.<anonymous> (../../../node_modules/firebase-functions/lib/common/providers/identity.js:25:14)

我还尝试使用 transformIgnorePatterns 为 firebase 的 node_modules 获取一个转换,但现在 jest 继续运行并似乎无响应。(我认为这可能与 jest 中的 ts-jest 有关)。

问题:有人知道如何在 nrwl nest.js TypeScript 项目中让 es6 node_modules 与 jest 协同工作吗?

我的 jest 配置:

module.exports = {
  displayName: '[projectName]',
  preset: '../../../jest.preset.js',
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
    },
  },
  transform: {
    '^.+[tj]s$': 'ts-jest',
  },
  setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
  moduleFileExtensions: ['ts', 'js', 'html'],
  coverageDirectory: '../../../coverage/apps/back/[projectName]',
  testEnvironment: 'node',
}
英文:

I have a nrwl Angular-Jest application setup via npx create-nx-workspace.

While working on this project, I had upgraded from:

&quot;firebase&quot;: &quot;9.1.3&quot;,
&quot;firebase-admin&quot;: &quot;9.12.0&quot;,
&quot;firebase-functions&quot;: &quot;3.15.7&quot;,

to

&quot;firebase&quot;: &quot;9.16.0&quot;,
&quot;firebase-admin&quot;: &quot;11.5.0&quot;,
&quot;firebase-functions&quot;: &quot;4.2.0&quot;,

to get the extension emulator working locally. Although it works, I now have issues running jest. It says that firebase now has es6 imports and wont work. Error Message Below:

  ● 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 &quot;node_modules&quot; folder is ignored by transformers.

Here&#39;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 &quot;node_modules&quot; files transformed, you can specify a custom &quot;transformIgnorePatterns&quot; in your config.
 • If you need a custom transformation specify a &quot;transform&quot; 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 &quot;moduleNameMapper&quot; config option.

You&#39;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/[justOnlyMe]/Desktop/[nrwlProject]/node_modules/firebase-admin/lib/esm/auth/index.js:1
({&quot;Object.&lt;anonymous&gt;&quot;:function(module,exports,require,__dirname,__filename,jest){import mod from &quot;../../auth/index.js&quot;;
                                                                                  ^^^^^^

SyntaxError: Cannot use import statement outside a module

  at Runtime.createScriptFromCode (../../../node_modules/jest-runtime/build/index.js:1728:14)
  at Object.&lt;anonymous&gt; (../../../node_modules/firebase-functions/lib/common/providers/identity.js:25:14)

I've also tried using transformIgnorePatterns to get a transformation just for firebase node_modules but now jest continues to run and seems unresponsive. (I believe this may be an issue with ts-jest in jest).

transform: {
    &#39;^.+\\.[tj]s$&#39;: &#39;ts-jest&#39;,
  },
  transformIgnorePatterns: [
    &#39;../../../node_modules/(?!@firebase.*/)&#39;,
    &#39;../../../node_modules/(?!firebase/)&#39;,
    &#39;../../../node_modules/(?!firebase-admin/)&#39;,
    &#39;../../../node_modules/(?!firebase-functions/)&#39;,
  ],

Question: Does anyone know how to get es6 node_modules to work with jest in a nrwl nest.js typescript project?

My jest config:

module.exports = {
  displayName: &#39;[projectName]&#39;,
  preset: &#39;../../../jest.preset.js&#39;,
  globals: {
    &#39;ts-jest&#39;: {
      tsconfig: &#39;&lt;rootDir&gt;/tsconfig.spec.json&#39;,
    },
  },
  transform: {
    &#39;^.+\\.[tj]s$&#39;: &#39;ts-jest&#39;,
  },
  setupFilesAfterEnv: [&#39;&lt;rootDir&gt;/jest.setup.ts&#39;],
  moduleFileExtensions: [&#39;ts&#39;, &#39;js&#39;, &#39;html&#39;],
  coverageDirectory: &#39;../../../coverage/apps/back/[projectName]&#39;,
  testEnvironment: &#39;node&#39;,
}

答案1

得分: 1

在你的Jest配置中,在transform对象中添加babel-jest配置:

"transform": {
  "^.+\\.js?$": "babel-jest",
  "^.+\\.ts?$": "ts-jest"
}
英文:

In your jest config add in the transform object the babel-jest config:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&quot;transform&quot;: {
  &quot;^.+\\.js?$&quot;: &quot;babel-jest&quot;,
  &quot;^.+\\.ts?$&quot;: &quot;ts-jest&quot;
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 07:59:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定