dragons0458
dragons0458

Reputation: 173

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!

Upvotes: 3

Views: 4234

Answers (2)

Yao
Yao

Reputation: 449

I ran into the same problem, my jest-e2e.json file was also similar

"moduleNameMapper": {
    "src/(.*)": "<rootDir>/../src/$1"
}

The error Could not locate module ./src/index-minimal mapped as: ... occurred because the minimal.js file in protobufjs tries to import ./src/index-minimal but our Jest runner remaps the file path name to <rootDir>/../src/$1 so protobufjs threw an error because it cannot find the ./src/index-minimal.js file.

The solution is to leave src/index-minimal alone, so we don't remap its name. I added the following line to the moduleNameMapper field in jest-e2e.json and it solved the issue

"moduleNameMapper": {
    "src/index-minimal": "./src/index-minimal",
    "src/(.*)": "<rootDir>/../src/$1"
}

Just note not to create a file named index-minimal in your src folder because Jest will fail to map the filename properly.

Upvotes: 2

dragons0458
dragons0458

Reputation: 173

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!

Upvotes: 5

Related Questions