Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when I try to test with NestJS, Jest and GraphQLFederationModule

When I try to test with this module it gives me the following error:

 FAIL  test/test.e2e-spec.ts (28.561s)
  AppController (e2e)
    × test (4555ms)

  ● AppController (e2e) › test

    Configuration error:

    Could not locate module ./src/index-minimal mapped as:
    C:\absolute\path\server\company\src\$1.

    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/src\/(.*)/": "C:\absolute\path\server\company\src\$1"
      },
      "resolver": undefined
    }

      at createNoMappedModuleFoundError (../node_modules/jest-resolve/build/index.js:545:17)
      at Object.<anonymous> (../node_modules/@apollo/protobufjs/minimal.js:4:18)

The jest config file is:

{
  "moduleFileExtensions": ["js", "json", "ts"],
  "rootDir": ".",
  "testRegex": ".e2e-spec.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  },
  "testEnvironment": "node",
  "moduleDirectories": ["node_modules", "src"],
  "moduleNameMapper": {
    "src/(.*)": "<rootDir>/../src/$1"
  }
}

The app.module file:

@Module({
  imports: [
    ...,
    GraphQLFederationModule.forRootAsync({
      imports: [CustomConfigModule],
      inject: [EnvironmentStateService],
      useFactory: (environmentStateService: EnvironmentStateService) => ({
        introspection: environmentStateService.isDev,
        debug: environmentStateService.isDev,
        playground: environmentStateService.isDev,
        autoSchemaFile: true,
        tracing: environmentStateService.isDev,
      }),
    }),
    ...,
  ],
})
export class AppModule {}

When I try the following settings in app.module it works correctly:

@Module({
  imports: [
    ...,
    GraphQLModule.forRootAsync({
      imports: [CustomConfigModule],
      inject: [EnvironmentStateService],
      useFactory: (environmentStateService: EnvironmentStateService) => ({
        introspection: environmentStateService.isDev,
        debug: environmentStateService.isDev,
        playground: environmentStateService.isDev,
        autoSchemaFile: true,
        tracing: environmentStateService.isDev,
      }),
    }),
    ...,
  ],
})
export class AppModule {}

I have been reviewing the file where it gives me the error and I find the following protobufjs code in node_modules (minimal.js):

// minimal library entry point.

"use strict";
module.exports = require("./src/index-minimal");

Does anyone know what may be happening? Thank you!

like image 400
dragons0458 Avatar asked Dec 18 '25 19:12

dragons0458


1 Answers

I have changed my paths in the form: "src/folder/..." to "@App/folder/..." and in the end it works fine, but I'm left curious why this happens and if there is any solution To this, I will be attentive to possible solutions and recommendations, thank you very much!

In case someone wants to see the solution, it is as follows:

The tsconfig.json:

{
  "compilerOptions": {
    ...
    "baseUrl": "./",
    "paths": {
      "@App/*": ["src/*"]
    },
    ...
  },
  ...
}

The jest config file:

{
  "moduleFileExtensions": ["js", "json", "ts"],
  "rootDir": ".",
  "testRegex": ".e2e-spec.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  },
  "testEnvironment": "node",
  "moduleNameMapper": {
    "^@App/(.*)$": "<rootDir>/../src/$1"
  }
}

Regards!

like image 104
dragons0458 Avatar answered Dec 21 '25 02:12

dragons0458



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!