简体   繁体   English

Angular Apollo V3 - Jest 错误:找不到模块“apollo-angular”

[英]Angular Apollo V3 - Jest error: Cannot find module 'apollo-angular'

I recently updated angular apollo from 2.6.0 to the v3.0.0 and this update broke my tests.我最近将 angular apollo 从 2.6.0 更新到 v3.0.0,此更新破坏了我的测试。

I am using jest for my unit tests, while the app compiles and runs fine, my tests are unable to import any dependency from 'apollo-angular', and I get errors like this:我在我的单元测试中使用 jest,虽然应用程序编译并运行良好,但我的测试无法从“apollo-angular”导入任何依赖项,并且出现如下错误:

  Test suite failed to run

    Cannot find module 'apollo-angular' from 'src/graphql/generated/graphql.ts'

    Require stack:
      src/graphql/generated/graphql.ts
      src/app/pages/test/test.component.ts
      src/app/pages/test/test.component.spec.ts

      1 | /* eslint-disable */
    > 2 | import { gql } from 'apollo-angular';
        | ^
      3 | import { Injectable } from '@angular/core';
      4 | import * as Apollo from 'apollo-angular';
      5 | export type Maybe<T> = T | null;

      at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:324:11)
      at Object.<anonymous> (src/graphql/generated/graphql.ts:2:1)

There is an order on the module resolution and because 'apollo-angular' v3 is using .mjs the resolved file will be package.json (in node_modules/apollo-angular/package.json ), the package.json containing the 'module' property 模块解析有一个顺序,因为“apollo-angular”v3 使用的是.mjs ,解析后的文件将为package.json (在node_modules/apollo-angular/package.json中), package.json包含“模块”属性

{
  "module": "build/fesm2020/ngApollo.mjs",
}

In short, when importing import * as Apollo from 'apollo-angular' I expect jest to resolve node_modules/apollo-angular/package.json which one will point to build/fesm2020/ngApollo.mjs which is the expected file to import.简而言之,当import * as Apollo from 'apollo-angular'时,我希望 jest 能够解析node_modules/apollo-angular/package.json ,其中一个将指向build/fesm2020/ngApollo.mjs ,这是预期要导入的文件。

Again, TS is compiling the project properly and is able to import this es module but jest it not.同样,TS 正在正确编译项目并且能够导入这个 es 模块,但开玩笑说它没有。 What did I miss?我错过了什么?

Seems that I was using an old version of jest-preset-angular似乎我使用的是旧版本的jest-preset-angular

"jest-preset-angular": "^9.0.7",

I updated to我更新到

"jest-preset-angular": "^11.1.1",

I then had a minor breaking change that I fixed removing the import 'jest-preset-angular/setup-jest';然后我做了一个小的破坏性更改,我修复了删除import 'jest-preset-angular/setup-jest'; line from my setup-jest.ts file and the bug is gone.我的setup-jest.ts文件中的行,错误消失了。

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

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