Здравствуйте. Учу nest и решил попробовать вынести отдельный модуль чтобы поучить jest и git, всё получалось до этого момента, ошибок 0 - работающих end-поинтов тоже 0. Не понимаю в чём проблема.
app.module.ts:
import { Module } from '@nestjs/common';
import {ItemsModule} from "./items/items.module";
@Module({
imports: [ItemsModule]
})
export class AppModule {}
items.module.ts:
import {Module} from "@nestjs/common";
import {ItemsService} from "./items.service";
import {ItemsController} from "./items.controller";
Module({
controllers: [ItemsController],
providers: [ItemsService],
})
export class ItemsModule {}
items.service.ts:
import { Injectable } from '@nestjs/common';
import {ItemDto} from "./dto/item.dto";
@Injectable()
export class ItemsService {
protected items: ItemDto[] = [];
getAll():ItemDto[] {
return this.items;
}
create(item:ItemDto):ItemDto {
item.id = `${Math.floor(Math.random() * 100)}`;
this.items.push(item);
return item;
}
update(item:ItemDto):ItemDto {
const changeItem = this.items.find(el => el.id === item.id);
for(let prop in changeItem) {
changeItem[prop] = item[prop];
}
return changeItem
}
delete(id:string):ItemDto[] {
return this.items = this.items.filter(el => el.id !== id);
}
}
items.controller.ts:
import {Controller, Get, HttpException, HttpStatus, Param, Post, Res} from "@nestjs/common";
import {Response} from "express";
import {ItemsService} from "./items.service";
import {ItemDto} from "./dto/item.dto";
@Controller()
export class ItemsController {
constructor(private readonly itemsService: ItemsService) {}
@Get()
getAll(@Res({passthrough: true}) res: Response):string | object {
const items = this.itemsService.getAll();
if(!!items.length) {
res.status(HttpStatus.OK);
return new HttpException({
items: items
}, HttpStatus.OK).getResponse();
}
res.status(HttpStatus.INTERNAL_SERVER_ERROR);
return new HttpException({
items: 'Items length: ' + items.length,
status: HttpStatus.INTERNAL_SERVER_ERROR
}, HttpStatus.INTERNAL_SERVER_ERROR).getResponse();
}
@Post()
create(@Param() params):ItemDto {
return this.itemsService.create(params);
}
}
Тесты работают как нужно
app.spec.ts:
import { Test } from '@nestjs/testing';
import { ItemsController } from './items/items.controller';
import { ItemsService } from './items/items.service';
import * as request from 'supertest';
import { INestApplication } from "@nestjs/common";
describe('ItemsModule', () => {
let itemsController: ItemsController;
let itemsService: ItemsService;
let app: INestApplication;
beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
controllers: [ItemsController],
providers: [ItemsService],
}).compile();
itemsService = moduleRef.get<ItemsService>(ItemsService);
itemsController = moduleRef.get<ItemsController>(ItemsController);
app = moduleRef.createNestApplication();
await app.init();
});
describe('End-points', () => {
it('/GET Status 200', () => {
return request(app.getHttpServer())
.get('/items')
.expect(200)
.expect({
"items": [
{
id: '0',
title: '',
message: ''
}
]
});
});
it('/GET Status 500', () => {
return request(app.getHttpServer())
.get('/items')
.expect(500)
.expect({
items: 'Items length: 0',
status: 500
});
});
});
});
404 всегда
Если вернуть appController.ts, то роут "/" = Hello world!
import {Controller, Get} from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor() {}
// @Get()
// getHome():string {
// return 'Hello world!';
// }
}
Спасибо.