@tsed/platform-serverless-testing
Version:
Module to test Serverless function with Ts.ED
137 lines (136 loc) • 4.61 kB
JavaScript
import { nameOf } from "@tsed/core";
import { destroyInjector, DITest } from "@tsed/di";
import { createFakeContext } from "./createFakeContext.js";
import { createFakeEvent } from "./createFakeEvent.js";
export class LambdaClientRequest extends Promise {
constructor() {
super(...arguments);
this.event = createFakeEvent();
this.context = createFakeContext();
}
static call(lambdaName) {
const resolvers = {};
const promise = new LambdaClientRequest((resolve, reject) => {
resolvers.resolve = resolve;
resolvers.reject = reject;
});
promise.init(lambdaName, resolvers.resolve, resolvers.reject);
return promise;
}
static get(path, options = {}) {
return this.call("handler").get(path, options);
}
static post(path, body, options = {}) {
return this.call("handler").post(path, body, options);
}
static put(path, body, options = {}) {
return this.call("handler").put(path, body, options);
}
static patch(path, body, options = {}) {
return this.call("handler").patch(path, body, options);
}
static delete(path, body, options = {}) {
return this.call("handler").delete(path, body, options);
}
get(path, options = {}) {
Object.assign(this.event, options);
this.event.path = path;
this.event.httpMethod = "GET";
return this;
}
post(path, body = {}, options = {}) {
Object.assign(this.event, options);
this.event.path = path;
this.event.httpMethod = "POST";
this.body(body);
return this;
}
patch(path, body = {}, options = {}) {
Object.assign(this.event, options);
this.event.path = path;
this.event.httpMethod = "PATCH";
this.body(body);
return this;
}
put(path, body = {}, options = {}) {
Object.assign(this.event, options);
this.event.path = path;
this.event.httpMethod = "PUT";
this.body(body);
return this;
}
delete(path, body = {}, options = {}) {
Object.assign(this.event, options);
this.event.path = path;
this.event.httpMethod = "DELETE";
this.body(body);
return this;
}
query(query) {
this.event.queryStringParameters = {
...this.event.queryStringParameters,
...JSON.parse(JSON.stringify(query))
};
return this;
}
params(pathParameters) {
this.event.pathParameters = {
...this.event.pathParameters,
...JSON.parse(JSON.stringify(pathParameters))
};
return this;
}
headers(headers) {
this.event.headers = {
...this.event.headers,
...JSON.parse(JSON.stringify(headers))
};
return this;
}
body(body) {
if (body !== undefined) {
this.event.headers["content-type"] = "application/json";
this.event.body = JSON.stringify(body);
}
return this;
}
init(lambda, resolve, reject) {
setTimeout(async () => {
try {
const result = await PlatformServerlessTest.callbacks[lambda](this.event, this.context, resolve);
resolve(result);
}
catch (er) {
reject(er);
}
});
}
}
export class PlatformServerlessTest extends DITest {
static { this.callbacks = {}; }
static { this.request = LambdaClientRequest; }
static bootstrap(serverless, { server, ...settings }) {
return function before() {
settings = DITest.configure(settings);
const isServerlessHttp = nameOf(serverless).includes("Http");
// @ts-ignore
const instance = isServerlessHttp ? serverless.bootstrap(server, settings) : serverless.bootstrap(settings);
PlatformServerlessTest.instance = instance;
PlatformServerlessTest.callbacks = {};
if (!isServerlessHttp) {
PlatformServerlessTest.callbacks = instance.callbacks();
}
PlatformServerlessTest.callbacks.handler = instance.handler();
return instance.promise;
};
}
/**
* Resets the test injector of the test context, so it won't pollute your next test. Call this in your `tearDown` logic.
*/
static async reset() {
if (PlatformServerlessTest.instance) {
await PlatformServerlessTest.instance.stop();
}
await destroyInjector();
}
}