Check function return with jest

Hello I am starting to study unit tests with jest and I have a class that has a function with a return I wanted to test the return of that function, but I don’t know what the correct way to do this

class:

interface HelloWord {
    hello: string
}

export class HelloWordController extends Controller{
    async handle (request: HelloWord): Promise<HttpResponse> {
        return ok({message: 'hello word'})
    }
}

OK: return:

export const ok = (data: any): HttpResponse => ({
  statusCode: 200,
  body: data
})

test:

describe("Hello word Controller ", () => {
  it("should receive a string with name: hello, and value: hello word", () => {
        const logger = new adptLogger({})
        const hellowordController = new HelloWordController(logger)
        expect(hellowordController.handle({hello: 'hello word'})).toEqual(ok)
    });
});

i got failed:

Expected: [Function ok]
Received: {}

   7 |              const logger = new adptLogger({})
   8 |              const hellowordController = new HelloWordController(logger)
>  9 |              expect(hellowordController.handle({hello: 'hello word'})).toEqual(ok)
     |                                                                        ^
  10 |      });
  11 | });

edited:

describe("Hello word Controller ", () => {
  let controller = {} as HelloWordController;
  let logger: adptLogger = {} as adptLogger;
  beforeEach(() => {
    logger = new adptLogger({});
    controller = new HelloWordController(logger);
  });
  afterEach(() => {
    jest.restoreAllMocks();
  });

  it("should receive a string with name: hello, and value: hello word", async () => {
    const expedtedReturn = {
      statusCode: 200,
      body: { message: "hello word" },
    };
    expect(await controller.handle({ hello: "hello word" })).toEqual(
      expedtedReturn
    );
  });

  it("should receive a bad string, and receive a bad request", async () => {
    const expedtedError = {
      statusCode: 400,
      body: new Error(`bad Request`),
    };
    expect(await controller.handle({ hello: "aaa" })).toEqual(expedtedError);
  });
});

Answer

You would need to spy your method.

If you are going to add more tests, use beforeEach to spy it should be okey, and restore them with afterEach, so you could control the returned value.

Your method is async, so your test should also, something like:

describe("Hello word Controller ", () => {
  const mockedResponse = { message: "hello word" };

  beforeEach(() => {
    const controllerSpy = jest.spyOn(HelloWordController, "handle");
    controllerSpy.mockReturnValue(Promise.resolve(mockedResponse));
  });

  afterEach(() => {
    jest.restoreAllMocks();
  });

  it("should receive a string with name: hello, and value: hello word", async () => {
        const logger = new adptLogger({})
        const hellowordController = new HelloWordController(logger)
        expect(await hellowordController.handle()).toEqual(mockedResponse);
    });
});

Leave a Reply

Your email address will not be published. Required fields are marked *