【问题标题】:Adding to globals in package.json jest config在 package.json jest 配置中添加到全局变量
【发布时间】:2022-06-16 04:00:32
【问题描述】:

我最近开始在我的 create-react-app 应用程序中使用 azure 活动目录,当我尝试运行测试时遇到了这个错误。

    BrowserAuthError: crypto_nonexistent: The crypto object or function is not available. Detail:Browser crypto or msCrypto object not available.

      25 | import { NavigationProvider } from './contexts/index';
      26 |
    > 27 | export const msalInstance = new PublicClientApplication(msalConfig);

我找到了React jest and MSAL getting BrowserAuthError : crypto,这似乎是我的确切问题,但是我的笑话配置在我的 package.json 中并且看起来像这样:

  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx}",
      "!src/**/*.{config,stories}.js",
      "!src/**/index.js"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 80,
        "functions": 80,
        "lines": 80,
        "statements": 80
      }
    }
  },

在阅读了一些文档之后,我仍然很难弄清楚如何在我的 package.json 中将 crypto 添加到我的 jest 配置中。我还尝试将我的 jest 配置迁移到独立的 jest.config.js 并将我的测试脚本切换到 react-scripts test -- --config jest.config.js 但无法让它工作。

解决此问题的最佳方法是什么?我假设这是一个简单的修复,但我只是无法为任一版本的配置找出正确的语法。谢谢!

【问题讨论】:

    标签: reactjs jestjs azure-active-directory


    【解决方案1】:

    jest 会自动查找在运行测试之前自动执行的src/setupTests.js 文件。这意味着您可以将global.crypto = require('crypto'); 添加到setupTests.js 文件中,它的作用与将全局部分添加到jest.config.js 中的作用相同。

    【讨论】:

      猜你喜欢
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      相关资源
      最近更新 更多