#Brand new project fails unit test
6 messages · Page 1 of 1 (latest)
Need some code.
or just read good stuff like this: https://trilon.io/blog/advanced-testing-strategies-with-mocks-in-nestjs
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!');
});
});
});
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?
Nest needs to be compiled with the emit decorators ts compiler option set to true.