Q:

Testing a controller API that refirects

I have a controller in Nest.JS that redirects:

    @Get('route/:value')
    async route(@Param('value') value: string) {
      const result = await this.someService.doSomethingWithValue(value);
      if (result) {
        return { url: 'http://example.com/success.html' };
      } else {
        return { url: 'http://example.com/fail.html' };
      }
    } 

How can I test in controller.spec.ts the correct redirect response?
i.e.:

describe('test', () => {
    it('should show success page', async() => {
      service.doSomethingWithValue = jest.fn(() => Promise.resolve(true));
      expect(controller.route('value')).toBe(?????);
    });
  });

A:

It should be exactly what you expect it to be. You are returning values, it should be those values, right? Actually those objects, so you should use toEqual instead of toBe. In this case, you're returning true so you should have expect(controller.route('value')).toEqual({ url: 'http://example.com/success.html' }). Start using return types on everything you can, it'll help you in the long run.

