swc-project / jest

Super-fast alternative for babel-jest or ts-jest without type checking. Please use main repository for issues
758 stars 37 forks source link

semicolon bug with nestjs #150

Closed sdg9670 closed 1 year ago

sdg9670 commented 1 year ago
// app.controller.ts
import { Controller, Get } from '@nestjs/common'
import { AppService } from './app.service' // here

@Controller()
export class AppController {
    constructor(private readonly appService: AppService) {}

    @Get()
    getHello(): string {
        return this.appService.getHello()
    }
}
// app.service.ts
import { Injectable } from '@nestjs/common'

@Injectable()
export class AppService {
    getHello(): string {
        return 'Hello World!'
    }
}
// app.controller.spec.ts
import { Test, TestingModule } from '@nestjs/testing'
import { AppController } from '@src/app.controller'
import { AppService } from '@src/app.service'

describe('AppController', () => {
    let appController: AppController

    beforeEach(async () => {
        const app: TestingModule = await Test.createTestingModule({
            controllers: [AppController],
            providers: [AppService],
        }).compile()

        appController = app.get<AppController>(AppController)
    })

    afterEach(async () => {
        jest.clearAllMocks()
    })

    describe('root', () => {
        it('should return "Hello World!"', () => {
            expect(appController.getHello()).toBe('Hello World!')
        })
    })
})

Config

{
    "$schema": "https://json.schemastore.org/swcrc",
    "sourceMaps": true,
    "jsc": {
      "parser": {
        "syntax": "typescript",
        "decorators": true,
        "dynamicImport": true
      },
      "transform": {
        "legacyDecorator": true,
        "decoratorMetadata": true
      },
      "baseUrl": "./"
    },
    "minify": false
  }

If There was a semicolon at line 2 in app.controller.ts import { AppService } from './app.service'; // here Then, There would be no error.

But, If There was no semicolon at the same line import { AppService } from './app.service' // here Then, I could get error like this. I think AppService wasn't injected to AppController

    TypeError: Cannot read properties of undefined (reading 'getHello')

       8 |     @Get()
       9 |     getHello(): string {
    > 10 |         return this.appService.getHello()
         |                                ^
      11 |     }
      12 | }
      13 |

There should be no errors with or without semicolons.

Also, It had worked with ts-node