我在为模块运行Jest测试时遇到了麻烦-我有一个简单的入口点类:
import { Callback, CloudFrontRequestEvent, Context, Handler } from "aws-lambda";
import { CloudFrontHandler } from "./CloudFrontHandler";
import { OriginClient } from "./OriginClient";
import { ResponseRenderer } from "./ResponseRenderer";
import { TemplateRenderer } from "./TemplateRenderer";
export class LambdaHandler {
private readonly renderer: TemplateRenderer;
constructor(renderer: TemplateRenderer) {
this.renderer = renderer;
}
public handle: Handler = (event: CloudFrontRequestEvent, _: Context, callback: Callback): void => {
console.log(new CloudFrontHandler(null, null)); // <- just an empty object here! CloudFrontHandler {}
console.log(new OriginClient(null)); // <- properly mocked
console.log(new ResponseRenderer(null, null)); // <- properly mocked
new CloudFrontHandler(
new OriginClient(event.Records[0].cf.request),
new ResponseRenderer(callback, this.renderer),
)
.handle(); // <- because CloudFrontHandler is not mocked, an error occurs here
}
}
无论出于何种原因,CloudFrontHandler
类都不会被嘲笑(OriginClient
和ResponseRenderer
会被自动模拟)。以下是我的测试课:
import "jest";
import { Callback, CloudFrontRequest, CloudFrontRequestEvent } from "aws-lambda";
import { mocked } from "ts-jest/utils";
import { LambdaHandler, TemplateRenderer } from "../../../main/nodejs";
import { CloudFrontHandler } from "../../../main/nodejs/edge/CloudFrontHandler";
import { OriginClient } from "../../../main/nodejs/edge/OriginClient";
import { ResponseRenderer } from "../../../main/nodejs/edge/ResponseRenderer";
jest.mock("../../../main/nodejs/edge/CloudFrontHandler");
jest.mock("../../../main/nodejs/edge/OriginClient");
jest.mock("../../../main/nodejs/edge/ResponseRenderer");
const MockedCloudFrontHandler = mocked(CloudFrontHandler);
const MockedOriginClient = mocked(OriginClient);
const MockedResponseRenderer = mocked(ResponseRenderer);
describe("LambdaHandler", (): void => {
it("should handle CloudFront request", (): void => {
const templateRenderer: TemplateRenderer = {} as TemplateRenderer;
const callback: Callback = jest.fn() as Callback;
const request: CloudFrontRequest = {} as CloudFrontRequest;
const event: CloudFrontRequestEvent = {
Records: [
{
cf: {
request: request,
},
},
],
} as CloudFrontRequestEvent;
let handler: LambdaHandler = new LambdaHandler(templateRenderer);
handler.handle(event, null, callback)
// todo
expect(MockedCloudFrontHandler).toHaveBeenCalledTimes(1);
expect(MockedOriginClient).toHaveBeenCalledTimes(1);
expect(MockedResponseRenderer).toHaveBeenCalledTimes(1);
});
});
(我将源文件和测试文件分开保存,因此所有这些src/main
src/test
路径)
为什么Jest不会嘲笑这一模块类?
-编辑-
错误消息是:
TypeError: (intermediate value).handle is not a function
答案 0 :(得分:0)
我的问题下的注释使我找到了根本原因-这里的问题是CloudFrontHandler.handle
是一个箭头函数,TypeScript将该箭头函数转换为构造函数中分配的属性。由于构造函数是自动模拟的,因此不会分配该函数。为.handle()
方法创建手动模拟可以解决此问题。