<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Flavio Gomes da Silva Lisboa</title>
    <description>The latest articles on DEV Community by Flavio Gomes da Silva Lisboa (@fgsl).</description>
    <link>https://dev.to/fgsl</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1688400%2F4265331d-82a1-48c5-a010-3397dfd93072.png</url>
      <title>DEV Community: Flavio Gomes da Silva Lisboa</title>
      <link>https://dev.to/fgsl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fgsl"/>
    <language>en</language>
    <item>
      <title>Could not locate module in test using @app</title>
      <dc:creator>Flavio Gomes da Silva Lisboa</dc:creator>
      <pubDate>Wed, 26 Jun 2024 16:58:39 +0000</pubDate>
      <link>https://dev.to/fgsl/could-not-locate-module-in-test-using-app-2im8</link>
      <guid>https://dev.to/fgsl/could-not-locate-module-in-test-using-app-2im8</guid>
      <description>&lt;p&gt;I'm trying to work with tests in NestJS now and I couldn't move forward due to a problem with the location of the modules.&lt;br&gt;
The application was already responding, the problems started when I tried to run the tests.&lt;/p&gt;

&lt;p&gt;The first try gave me several errors like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cannot find module 'src/common/dto/paginated-response.dto' from 'applications/applications.service.ts'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;According my reading of &lt;a href="https://stackoverflow.com/questions/53963007/error-while-running-nestjs-in-production-mode-cannot-find-module"&gt;Tejas&lt;/a&gt;, I understood that the problem was the relative path starting in 'src/...'. &lt;/p&gt;

&lt;p&gt;Then, I read the tutorial by &lt;a href="https://amirmustafaofficial.medium.com/nest-js-converting-relative-path-to-absolute-path-fea5b22dce47"&gt;Amir Mustafa&lt;/a&gt; for converting the relative paths to absolute paths from the configuration.&lt;/p&gt;

&lt;p&gt;After replacing the relative paths with the &lt;code&gt;@app&lt;/code&gt; key, I got to run the application, but the tests failed again. This time, the errors were like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cannot find module '@app/users/users.controller' from 'users/users.controller.spec.ts'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After reading the question/answer by &lt;a href="https://stackoverflow.com/questions/61356672/error-when-i-try-to-test-with-nestjs-jest-and-graphqlfederationmodule"&gt;dragons0458&lt;/a&gt;, I understood that I should to configure moduleNameMapper inside jest section of package.json. The section is so now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node",
    "moduleNameMapper": {
      "^@app/(.*)": "&amp;lt;rootDir&amp;gt;./dist/$1"
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The third try gave me new errors, below, but now I don't know what I should do. It looks to me that $1 in moduleNameMapper is not converted to path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Configuration error:

    Could not locate module @app/gateways/gateways.controller mapped as:
    [MY HOME]/backend_nestjs/src/dist/$1.

    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^@app\/(.*)/": "[MY HOME]/backend_nestjs/src/dist/$1"
      },
      "resolver": undefined
    }

      1 | import { Test, TestingModule } from '@nestjs/testing';
    &amp;gt; 2 | import { GatewaysController } from '@app/gateways/gateways.controller';
        | ^
      3 | import { GatewaysService } from '@app/gateways/gateways.service';
      4 |
      5 | describe('GatewaysController', () =&amp;gt; {

      at createNoMappedModuleFoundError (../node_modules/jest-resolve/build/resolver.js:759:17)
      at Object.&amp;lt;anonymous&amp;gt; (gateways/gateways.controller.spec.ts:2:1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The application is answering in 3000 port, but the tests (12 tests) fail. What is lacking for the correct configuration for modules?&lt;/p&gt;

&lt;p&gt;I am pasting here two files that I think can help to understand my configuration:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;tsconfig.json&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": true,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "paths": {
      "@app/*": ["./src/*"]
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;jest-e2e.json&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "moduleFileExtensions": ["js", "json", "ts"],
  "rootDir": ".",
  "testEnvironment": "node",
  "testRegex": ".e2e-spec.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you very much.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
