kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.

Home Page:https://kulshekhar.github.io/ts-jest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for decorators like @Injectable

oscarcalderonc opened this issue · comments

I'm currently using Nestjs together with jest and ts-jest. I have a PrismaService class with the @Injectable decorator, and for some reason jest complains when I run a test that requires that service to be injected. I'm not sure what to do in order to allow support for decorators. Here are my files.

jest.config.json

{
    "moduleFileExtensions": [
        "js",
        "json",
        "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
        "^.+\\.(t|j)s$": "ts-jest"
    },
    "transformIgnorePatterns": [
        "/node_modules/",
        "\\.pnp\\.[^\\\/]+$",
        "/prisma/",
        "/test/"
    ],
    "collectCoverageFrom": [
        "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node",
    "collectCoverage": true,
    "coverageThreshold": {
        "global": {
            "lines": 70
        }
    },
    "clearMocks": true,
    "errorOnDeprecated": true,
    "moduleNameMapper": {
        "^~/(.*)$": "<rootDir>/$1",
        "^@/(.*)$": "<rootDir>/$1"
    }
}

prisma.service.ts

import { INestApplication, Injectable, OnModuleInit } from "@nestjs/common";
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
  async onModuleInit(): Promise<void> {
    await this.$connect();
  }

  // eslint-disable-next-line @typescript-eslint/require-await
  async enableShutdownHooks(app: INestApplication): Promise<void> {
    this.$on('beforeExit', async () => {
      await app.close();
    });
  }

stacktrace

Running coverage on untested files...Failed to collect coverage from /Users/oscarcalderon/Documents/praxent/projects/fruitful-backend/src/prisma/prisma.service.ts
ERROR: /Users/oscarcalderon/Documents/praxent/projects/fruitful-backend/src/prisma/prisma.service.ts: Support for the experimental syntax 'decorators' isn't currently enabled (4:1):

  2 | import { PrismaClient } from '@prisma/client';
  3 |
> 4 | @Injectable()
    | ^
  5 | export class PrismaService extends PrismaClient implements OnModuleInit {
  6 |   async onModuleInit(): Promise<void> {
  7 |     await this.$connect();

Add @babel/plugin-proposal-decorators (https://github.com/babel/babel/tree/main/packages/babel-plugin-proposal-decorators) to the 'plugins' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-decorators (https://github.com/babel/babel/tree/main/packages/babel-plugin-syntax-decorators) to the 'plugins' section to enable parsing.
commented

Decorators like the ones in NestJs require TypeScript Program to do some magic behind. ts-jest has limited support on this since Program requires lots of memory to have enough information to perform magic.

The typical case of such magic decorators are like Angular or Nest which involves some AST transformers.

Technically, if you know which AST transformers Nest use, you can declare them in ts-jest config. Or write your own Jest transformer to include those AST transformers by default just like how jest-preset-angular does. However, caveat is it won’t guarantee since each Jest worker has insufficient amount of information needed for TypeScript Program to do its job.

Normal decorators which don’t require TypeScript Program we do support.