简体   繁体   English

Angular 14 带有 Jest 28 和 Nx 的升级有 Jest 测试问题“找不到模块‘apollo-angular’”

[英]Angular 14 Upgrade w/ Jest 28 and Nx Having Issues with Jest Tests "Cannot find module 'apollo-angular'"

I have a very peculiar issue that I've been working on for quite some time.我有一个非常特殊的问题,我已经研究了很长时间。 I am in the process of upgrading our main frontend app from Angular 12 to Angular 14. All the migrations were successful.我正在将我们的主要前端应用程序从 Angular 12 升级到 Angular 14。所有迁移都成功了。 The app builds and runs just fine.该应用程序构建并运行得很好。 However, the tests are failing due to being unable to find the apollo-angular module.但是,由于无法找到apollo-angular模块,测试失败了。 We use apollo-angular for making GQL requests to our backend services.我们使用 apollo-angular 向我们的后端服务发出 GQL 请求。 The app makes the requests just fine.该应用程序使请求很好。 However, when I try to run my unit tests in the project, they always fail because they cannot find the apollo-angular module.但是,当我尝试在项目中运行我的单元测试时,它们总是失败,因为它们找不到 apollo-angular 模块。

Error:错误: 在此处输入图像描述

Here is my current setup.这是我当前的设置。 Please let me know if I am missing any useful files and I will add them.如果我遗漏了任何有用的文件,请告诉我,我会添加它们。

./jest.preset.ts ./jest.preset.ts

const nxPreset = require('@nrwl/jest/preset').default;

module.exports = {
  ...nxPreset,
};

./jest.config.ts ./jest.config.ts

const { getJestProjects } = require('@nrwl/jest');

export default {
  projects: getJestProjects(),
};

./libs/shared/ui-components/jest.config.ts ./libs/shared/ui-components/jest.config.ts

import { pathsToModuleNameMapper } from 'ts-jest';
const { paths } = require('../../../tsconfig.base.json').compilerOptions;

export default {
  displayName: 'ui-components',
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['../../../jest-setup.ts'],
  moduleNameMapper: pathsToModuleNameMapper(paths, {
    prefix: '<rootDir>/../../..',
  }),
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
    },
  },
  transform: {
    '^.+.(ts|mjs|js|html)$': 'jest-preset-angular',
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
};

Any help would be greatly appreciated.任何帮助将不胜感激。 I've been spinning my wheels on this for far too long.我一直在这上面旋转我的轮子太久了。

Cheers, Nate干杯,内特

A solution has appeared, After a ton of research.经过大量研究,出现了一个解决方案。 I found that adding the following resolver to my jest.config.ts allowed the tests to execute successfully.我发现将以下解析器添加到我的 jest.config.ts 允许测试成功执行。

/* eslint-disable */
import { pathsToModuleNameMapper } from 'ts-jest';
const { paths } = require('../../../tsconfig.base.json').compilerOptions;

export default {
  displayName: 'shared-data-access-auth',
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['../../../jest-setup.ts'],
  moduleNameMapper: pathsToModuleNameMapper(paths, {
    prefix: '<rootDir>/../../..',
  }),
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
    },
  },
  transform: {
    '^.+.(ts|mjs|js|html)$': 'jest-preset-angular',
  },
  transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
  resolver: 'jest-preset-angular/build/resolvers/ng-jest-resolver.js',
};

The key factors of this fix were the transformIgnorePatterns of ['node_modules/(?..*\\,mjs$)'], and the resolver of 'jest-preset-angular/build/resolvers/ng-jest-resolver.js',此修复的关键因素是['node_modules/(?..*\\,mjs$)'],'jest-preset-angular/build/resolvers/ng-jest-resolver.js',

Hope this helps someone else!希望这可以帮助别人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM