【问题标题】:Check function return with jest用玩笑检查函数返回
【发布时间】:2021-03-09 06:52:38
【问题描述】:

你好,我开始用 jest 研究单元测试,我有一个类有一个返回函数我想测试该函数的返回,但我不知道正确的方法是什么

类:

interface HelloWord {
    hello: string
}

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

好的:返回:

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

测试:

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

我失败了:

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

已编辑:

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

【问题讨论】:

  • handleasync。你需要await它。
  • @Christian 我的错谢谢你。

标签: node.js typescript jestjs


【解决方案1】:

你需要监视你的方法。

如果你要添加更多的测试,用beforeEach窥探应该没问题,用afterEach恢复它们,这样你就可以控制返回值了。

你的方法是异步的,所以你的测试也应该是这样的:

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

【讨论】:

  • 但出于这个原因,我使用 beforeEach 和 afterEach 是为了什么?
  • 我用我制作的代码编辑了它,你能看看我是否可以改进一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 2022-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-30
相关资源
最近更新 更多