#Brand new project fails unit test

6 messages · Page 1 of 1 (latest)

whole sky
#

When setting up a brand new project with nest new, the AppController test fails with the following error. It looks like it's not properly injecting dependencies?

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

Need some code.

warped steeple
whole sky
#

The code is no different than the starter app.

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

    @Get()
    getHello(): string {
        return this.appService.getHello();
    }
}

@Injectable()
export class AppService {
    getHello(): string {
        return 'Hello World!';
    }
}

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

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

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

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

Oh, I'm using Vitest instead of Jest. Perhaps there isn't yet support for NestJS due to decorators. I'm not sure if there's a workaround that exists other than switching back to Jest?

carmine leaf
#

Nest needs to be compiled with the emit decorators ts compiler option set to true.