reactjs 参考错误:当尝试在nx monorepo中的库中运行测试时,jest未定义

xe55xuns  于 2023-04-05  发布在  React
关注(0)|答案(1)|浏览(103)

我一直在尝试从NX monorepo在lib中运行测试,但它不起作用。首先,它找不到类型,所以我添加了/// <reference types="@types/jest" />,它被解决了。然后,当我运行测试时,我得到了错误:参考错误:jest未定义。Jest安装在monorepo的根目录下,我也尝试为lib安装jest,也删除了node_modules。对我来说没有任何效果。

/// <reference types="@types/jest" />

import { handleTokenAuthorization } from '.';

describe('auth funtions', () => {
  it('Should mount and replace url correctly to login screen', () => {
    const url = `${process.env.NX_APP_CLIENT_ID}/authorize?response_type=code&client_id=${process.env.NX_FRONT_URL}&scope=openid+profile&redirect_uri=${process.env.VITE_FRONT_URL}/receive-auth-token`;
    const replaceFn = jest.spyOn(window.location, 'replace');

    handleTokenAuthorization();

    expect(replaceFn).toBeCalledWith(url);
  });
});

9ceoxa92

9ceoxa921#

我通过@nrwl/jest:jest切换了vitest,所以我从我的库中在project.json中添加了以下配置,测试运行器工作正常。

"test": {
      "executor": "@nrwl/jest:jest",
      "outputs": [
        "{workspaceRoot}/coverage/{projectRoot}"
      ],
      "options": {
        "jestConfig": "libs/util/jest.config.ts",
        "passWithNoTests": true,
        "coverageDirectory": "coverage/libs/util"
      },
      "configurations": {
        "ci": {
          "ci": true,
          "codeCoverage": true
        }
      }
    }

相关问题