0

tell me please why appController working and itemsController no (from imported module)

I learn nestjs and i did it according to documentation. This controller working its uncomment endpoint.

import {Controller, Get} from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor() {}
  
  // @Get()
  // getHome():string {
  //    return 'Hello world!';
  // }
  
}

itemsController.ts - working if change appController.ts on this

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);
    }
    
}

Test jest working:

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('/')
                .expect(200)
                .expect({
                    "items": [
                        {
                            id: '0',
                            title: '',
                            message: ''
                        }
                    ]
                });
            
        });
        
        it('/GET Status 500', () => {
            
            return request(app.getHttpServer())
                .get('/')
                .expect(500)
                .expect({
                    items: 'Items length: 0',
                    status: 500
                });
            
        });
    });
});

I pushed all on github, you can see all code

1 Answer 1

1

After looking at your source code, you're missing the @ for @Module() in your ItemsModule

Sign up to request clarification or add additional context in comments.

1 Comment

yes, so stupid error with my side :D Thx u

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.