title: NestJS Testing: decorator is not a function
body: <p>My dependencies:</p>
  <pre><code>&quot;@nestjs/common&quot;: &quot;7.4.4&quot;,
  &quot;@nestjs/core&quot;: &quot;7.4.4&quot;,
  &quot;typescript&quot;: &quot;4.0.2&quot;,
  &quot;jest&quot;: &quot;26.4.2&quot;,
  &quot;jest-junit&quot;: &quot;11.1.0&quot;,
  &quot;ts-jest&quot;: &quot;26.3.0&quot;,
  </code></pre>
  <p>I have a custom decorator:</p>
  <pre class="lang-js prettyprint-override"><code>import { Inject } from &quot;@nestjs/common&quot;;
  
  import { DATABASE_SERVICE } from &quot;./database.constants&quot;;
  
  export const InjectDatabase = () =&gt; Inject(DATABASE_SERVICE);
  </code></pre>
  <p>I use it in constructor for a service I'm willing to cover with tests:</p>
  <pre class="lang-js prettyprint-override"><code>import {
    DatabaseService,
    InjectDatabase
  } from &quot;@infra/modules&quot;;
  import { Injectable } from &quot;@nestjs/common&quot;;
  
  @Injectable()
  export class MyService {
    constructor(
      @InjectDatabase() private readonly db: DatabaseService
    ) {}
  }
  </code></pre>
  <p>here is how I setup testing module:</p>
  <pre class="lang-js prettyprint-override"><code>import { Test } from &quot;@nestjs/testing&quot;;
  
  import { MyService } from &quot;../../../src/shared/services/my.service&quot;;
  import { SharedModule } from &quot;../../../src/shared/shared.module&quot;;
  
  describe(`MyService`, () =&gt; {
    let myService: MyService;
  
    beforeEach(async () =&gt; {
      const moduleRef = await Test.createTestingModule({
        imports: [SharedModule]
      }).compile();
  
      myService = moduleRef.get&lt;MyService&gt;(MyService);
    });
    test(&quot;tests&quot;, () =&gt; {
      // console.log(&quot;test&quot;);
    });
  });
  </code></pre>
  <p>SharedModule is a module with <code>@Global()</code> decorator and includes <code>MyService</code> in providers/exports.</p>
  <p>When I try to execute my test, I get error:</p>
  <pre><code>TypeError: modules_1.InjectDatabase is not a function
  
        51 | 
        52 |   constructor(
      &gt; 53 |     @InjectDatabase() private readonly adb_db: DatabaseService,
           |      ^
        56 |   ) {}
  
        at Object.&lt;anonymous&gt; (apps/workers/my/src/shared/services/my.service.ts:53:6)
        at Object.&lt;anonymous&gt; (apps/workers/my/test/shared/services/my.service.unit.test.ts:8:1)
  </code></pre>
  <p>I followed the trace and steps as you can see above:</p>
  <ul>
  <li>Import file with service in test:
  <code>import { MyService } from &quot;../../../src/shared/services/my.service&quot;;</code></li>
  <li>Raise error at <code>@InjectDatabase()</code> in constructor of service</li>
  </ul>
  <p>If I switch from <code>@InjectDatabase</code> to <code>@Inject(DATABASE_SERVICE)</code>:</p>
  <pre class="lang-js prettyprint-override"><code>@Injectable()
  export class MyService {
    constructor(
      @Inject(DATABASE_SERVICE) private readonly db: DatabaseService
    ) {}
  }
  </code></pre>
  <p>I get another error:</p>
  <pre><code>Error: Nest can't resolve dependencies of the CacheService (?). Please make sure that the argument dependency at index [0] is available in the RootTestModule context.
  </code></pre>
  <p>I tried to setup testing module like this:</p>
  <pre class="lang-js prettyprint-override"><code>    const moduleRef = await Test.createTestingModule({
        imports: [DatabaseModule],
        providers: [
          {
            provide: DATABASE_SERVICE,
            useClass: DatabaseService
          },
          MyService
        ]
      }).compile();
  </code></pre>
  <p>But it didn't help, same issue:</p>
  <pre><code>Error: Nest can't resolve dependencies of the CacheService (?).
  </code></pre>
  <p>My <code>tsconfig.json</code> compiler options:</p>
  <pre class="lang-json prettyprint-override"><code>  &quot;compilerOptions&quot;: {
      &quot;target&quot;: &quot;es2018&quot;,
      &quot;module&quot;: &quot;commonjs&quot;,
      &quot;outDir&quot;: &quot;dist/&quot;,
      &quot;esModuleInterop&quot;: true,
      &quot;strict&quot;: true,
      &quot;sourceRoot&quot;: &quot;/&quot;,
      &quot;sourceMap&quot;: true,
      &quot;experimentalDecorators&quot;: true,
      &quot;emitDecoratorMetadata&quot;: true,
      &quot;strictPropertyInitialization&quot;: false,
      &quot;removeComments&quot;: true,
      &quot;resolveJsonModule&quot;: true
    }
  </code></pre>
  <p>I have in <code>jest.config.js</code>:</p>
  <pre class="lang-js prettyprint-override"><code>setupFiles: [&quot;./jest-setup-file.ts&quot;],
  </code></pre>
  <p>This file has only import of reflect lib:</p>
  <pre class="lang-js prettyprint-override"><code>import 'reflect-metadata';
  </code></pre>
  <p>I tried to add to <code>my.service.ts</code> before <code>MyService</code> declaration:</p>
  <pre class="lang-js prettyprint-override"><code>console.log('InjectDatabase', InjectDatabase)
  </code></pre>
  <p>and received:</p>
  <pre><code>InjectDatabase: undefined
  </code></pre>
  <p>So it seems either Jest cannot resolve imports o something is wrong with  typescript config.</p>
  <p>Decorator function imported in <code>MyService</code> as:</p>
  <pre class="lang-js prettyprint-override"><code>import {
    DatabaseService,
    InjectDatabase
  } from &quot;@infra/modules&quot;;
  </code></pre>
  <p>My <code>jest.config.js</code>:</p>
  <pre class="lang-js prettyprint-override"><code>module.exports = {
    bail: 0,
    verbose: true,
    preset: &quot;ts-jest&quot;,
    testEnvironment: &quot;node&quot;,
    roots: [&quot;&lt;rootDir&gt;/libs&quot;, &quot;&lt;rootDir&gt;/apps&quot;],
    testRegex: &quot;.*\\.test\\.ts$&quot;,
    testPathIgnorePatterns: [&quot;/node_modules/&quot;, &quot;/dist/&quot;, &quot;/build/&quot;],
    moduleFileExtensions: [&quot;ts&quot;, &quot;js&quot;, &quot;json&quot;],
    moduleNameMapper: {
      &quot;@common&quot;: &quot;&lt;rootDir&gt;/libs/common/src&quot;,
      &quot;@common/(.*)&quot;: &quot;&lt;rootDir&gt;/libs/common/src/$1&quot;,
      &quot;@infra&quot;: &quot;&lt;rootDir&gt;/libs/infrastructure/src&quot;,
      &quot;@infra/(.*)&quot;: &quot;&lt;rootDir&gt;/libs/infrastructure/src/$1&quot;
    },
    transform: {
      &quot;^.+\\.ts?$&quot;: &quot;ts-jest&quot;
    },
    setupFiles: [&quot;./jest-setup-file.ts&quot;],
    collectCoverage: true,
    coverageThreshold: {
      global: {
        branches: 80,
        functions: 80,
        lines: 80,
        statements: 80
      }
    },
    coverageReporters: [&quot;json&quot;, &quot;lcov&quot;, &quot;text&quot;, &quot;clover&quot;],
    coveragePathIgnorePatterns: [&quot;/node_modules/&quot;]
  };
  </code></pre>
  <p>Probably <code>moduleNameMapper</code> doesn't resolve correctly <code>&quot;@infra/(.*)&quot;: &quot;&lt;rootDir&gt;/libs/infrastructure/src/$1&quot;</code> for <code>&quot;@infra/modules&quot;</code>?</p>
  <p>Outside of tests - everything works as intended.</p>
  <p>How can I setup correctly jest/typescript/nest so my custom decorators recognized correctly?</p>