Error in Angular Apollo V3: Jest unable to locate module 'apollo-angular'

After recently updating angular apollo from version 2.6.0 to v3.0.0, my tests have been broken.

I use jest for unit testing, and although the application compiles and runs without any issues, my tests cannot import any dependencies from 'apollo-angular'. This results in errors like the following:

  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)

The module resolution process follows a specific order, and due to 'apollo-angular' v3 using .mjs, the resolved file is typically package.json (located at

node_modules/apollo-angular/package.json</code). This <a href="https://github.com/kamilkisiela/apollo-angular/blob/master/packages/apollo-angular/package.json#L15" rel="nofollow noreferrer">package.json</a> includes the 'module' property stating:</p>
<pre class="lang-json"><code>{
  "module": "build/fesm2020/ngApollo.mjs"
}

To clarify, when importing

import * as Apollo from 'apollo-angular'
, I expect jest to resolve to
node_modules/apollo-angular/package.json
, which should point to build/fesm2020/ngApollo.mjs - the intended file for import.

While TypeScript successfully compiles the project and can import this ES module, Jest is unable to do so. What could I possibly be overlooking?

Answer №1

It turned out that the issue was caused by an outdated version of jest-preset-angular

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

After updating to

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

I encountered a minor breaking change, which I resolved by removing the line

import 'jest-preset-angular/setup-jest';
from my setup-jest.ts file, and the problem was fixed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

how to bind data to an array in Angular

I recently developed an Angular 7 application that features the capability to dynamically add and remove controls. However, I am facing challenges when it comes to binding the data to the form. In the code snippet below, I am focusing on the process of ad ...

What steps should I take to instruct the browser to utilize a cache manifest if service workers are not supported?

How can I instruct the browser to utilize a cache manifest if it does not support service workers? I am working on an Angular 4 application that must be able to run offline. While service workers are effective for this purpose, they are unfortunately not ...

Make sure the auto import feature in TypeScript Visual Studio Code Editor is set to always use the ".js" extension

At times, the auto-import completion feature includes the .js extension, but inconsistently. When this extension is missing in the TypeScript source, the emitted JavaScript file may encounter runtime issues like module not found error since the tsc compile ...

I require assistance in displaying a dynamic, multi-level nested object in HTML using AngularJS

I have a complex dynamic object and I need to extract all keys in a nested ul li format using AngularJS. The object looks like this: [{"key":"campaign_1","values":[{"key":"Furniture","values":[{"key":"Gene Hale","values":[{}],"rowLevel":2},{"key":"Ruben A ...

What is the best way to set HTML content in the ElementRef's nativeElement?

I am trying to use a directive to set the content of an HTML element. export class AdvertisementDirective { constructor(el: ElementRef) { el.nativeElement.style.background = 'yellow'; el.nativeElement.content = '<p>Hello Wo ...

Tips for effectively passing an array to props in Vue when leveraging Typescript and the class component decorator

I'm currently struggling to understand the proper method of passing an array as a prop to a component in Vue, using Typescript and the class component library. Following the official template, I attempted the following approach: <script lang="ts"& ...

Utilizing an InjectionToken to supply another InjectionToken

I'm puzzled as to why the following code isn't functioning properly. When I manually set a string for APP_BASE_HREF, everything works smoothly. main.ts export function main(baseHref: string) { platformBrowserDynamic([ { provide: MY ...

What is the best way to prevent users from entering a zero in the first position of a text box using JavaScript

Although I am aware this may be a duplicate issue, the existing solution does not seem to work for me. The field should accept values like: valid - 123,33.00, 100,897,99, 8000 10334 9800,564,88.36 invalid - 001, 0 ...

How do I find the location of /typings when upgrading from version 0.x to 1.x?

As I develop an Angular 2 app using TypeScript, I have been following the guidance found in the 5 Min Quickstart guide. Recently, the npm typings package was updated from version 0.x to 1.x. Following the instructions on the official website, I deleted the ...

Angular API snapshot error: The type 'IJobs' does not match the expected type 'IJobs[]'

Currently, I am in the process of learning and attempting to construct a job board using Angular 10. Although my API setup seems to be functioning properly, when navigating to the job detail page on Chrome, an error is displayed: ERROR in src/app/job-det ...

Is it possible to visually distinguish the selected mat-grid-tile? Particularly when they are being created dynamically

On the user interface, I have a dynamic display of mat-grid-tile within a mat-grid-list. These tiles change in number and data based on backend values. When a user clicks on a mat-grid-tile, it triggers a function that receives the tile's data. My goa ...

When trying to access the key value of a dynamically generated object, it returns as undefined

I am facing a challenge with my student object structure... { Freshmen: [{id: 3}, {id: 5}], Sophomores: [{id: 2}, {id: 6}], Juniors: [{id: 1}, {id: 8}], Seniors: [{id: 9}, {id: 4}, {id: 7}] } My goal is to retrieve full student objects from the d ...

The value of an Angular rxjs BehaviorSubject can be updated using the value property directly, without calling

While testing my code, I stumbled upon unexpected mutation. Perhaps I am doing something wrong. User constructor( public id: number, public education: Education[] ){} UserStateService private user = a new BehaviorSubject<User>(null); setUser(us ...

Replicating entities in TypeScript

I am currently developing an Angular 2 application using TypeScript. In a User Management component, I have implemented a table that displays all the users in my system. When a user is clicked on within the table, a form appears with their complete set of ...

Error parsing Angular2 with ngFor directive

I'm attempting to utilize the ngFor directive in an angular 2 (alpha 35) project, but I keep encountering the error EXCEPTION: Can't bind to 'ngforOf' since it isn't a known property of the '' element and there are no mat ...

My type is slipping away with Typescript and text conversion to lowercase

Here is a simplified version of the issue I'm facing: const demo = { aaa: 'aaa', bbb: 'bbb', } const input = 'AAA' console.log(demo[input.toLowerCase()]) Playground While plain JS works fine by converting &apo ...

Guide on positioning a span element to the left using the margin auto property in CSS for Angular 4

Having trouble with moving numbers highlighted to the left with names in CSS. I've tried using flex direction and margin auto but can't achieve the desired result. https://i.sstatic.net/kRJOb.png Here is my HTML code: <section class="favorit ...

Observable<void> fails to trigger the subscriber

I am currently facing a challenge with implementing a unit test for an Observable in order to signal the completion of a process. While there is no asynchronous code in the logout function yet, I plan to include it once the full logic is implemented. The m ...

Guide on importing an ES6 package into an Express Typescript Project that is being utilized by a Vite React package

My goal is to efficiently share zod models and JS functions between the backend (Express & TS) and frontend (Vite React) using a shared library stored on a gcloud npm repository. Although the shared library works flawlessly on the frontend, I continue to e ...

What to do when calling disabled() on a FormControlName causes all form fields to become invalid?

While working with a reactive form, I have observed that setting a formControlName to disabled() can cause the entire form to become invalid. Is there a way to ensure the form remains valid even after disabling a control? console.log('Before:' ...