test-sdk/ts/index.ts

86 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-07-04 15:22:44 +00:00
import * as plugins from './test-sdk.plugins';
2020-07-05 17:53:49 +00:00
import { AgTestServer } from './test-sdk.classes.testserver';
2020-09-24 00:45:23 +00:00
import { AAgHandler } from '@apiglobal/sdk';
2020-07-04 15:22:44 +00:00
2020-07-25 13:34:26 +00:00
export {
AgTestServer
2020-07-25 15:47:34 +00:00
};
2020-07-05 17:53:49 +00:00
let testServer: AgTestServer;
2020-09-24 00:45:23 +00:00
let handler: AAgHandler<any>;
2020-10-20 20:28:33 +00:00
export const createTestServer = async (handlerArg: { new(envManagerArg: plugins.agSdk.AgEnvironment): plugins.agSdk.AAgHandler<any> }) => {
2020-09-23 23:30:54 +00:00
class AgEnvironement extends plugins.agSdk.AgEnvironment {
2020-09-24 00:32:54 +00:00
public qenv = new plugins.qenv.Qenv('./', './.nogit');
2020-09-23 23:30:54 +00:00
public async getEnvVar(nameArg: string) {
2020-09-24 00:32:54 +00:00
return this.qenv.getEnvVarOnDemand(nameArg);
2020-09-23 23:30:54 +00:00
}
}
2020-09-24 00:59:01 +00:00
handler = new handlerArg(new AgEnvironement());
console.log(`now checking requirements for handler with slug ${handler.slug}...`);
await handler.checkRequirements();
2020-09-24 00:45:23 +00:00
testServer = new AgTestServer(handler);
2020-07-05 17:53:49 +00:00
await testServer.start();
return testServer;
};
2020-07-25 13:34:26 +00:00
export const testFire = <
A extends plugins.agSdk.AAgHandler<any>,
T extends plugins.typedrequestInterfaces.ITypedRequest
>(
2020-07-25 15:53:45 +00:00
slugArg: A['slug'],
2020-07-25 13:34:26 +00:00
methodArg: T['method'],
requestArg: T['request']
) => {
if (!testServer) {
throw new Error('you need to create and start a testServer first!');
}
if (testServer.server.serverStatus !== 'running') {
throw new Error('you need to start the testServer first!');
}
const typedRequest = new plugins.typedrequest.TypedRequest<T>(
2020-07-25 15:53:45 +00:00
`http://localhost:${testServer.server.options.port}/${slugArg}`,
2020-07-25 13:34:26 +00:00
methodArg
);
const responsePromise = typedRequest.fire(requestArg);
const expect = async (expectedResponseArg: T['response']) => {
const actualResponse = await responsePromise;
const comparisonResult = plugins.smartobject.compareObjects(expectedResponseArg, actualResponse);
let throwErrorBool = false;
if (comparisonResult.divergingProperties.length > 0) {
console.log(`The following properties diverged:`);
console.log(comparisonResult.divergingProperties);
2020-07-26 14:34:47 +00:00
for (const divProperty of comparisonResult.divergingProperties) {
2020-07-26 15:02:24 +00:00
if (expectedResponseArg[divProperty] !== 'maydiverge') {
2020-07-26 14:34:47 +00:00
throwErrorBool = true;
} else {
console.log(`${divProperty} may diverge, not throwing for this one`);
}
}
2020-07-25 13:34:26 +00:00
}
if (comparisonResult.missingProperties.length > 0) {
console.log(`The following properties diverged:`);
console.log(comparisonResult.divergingProperties);
throwErrorBool = true;
}
if (throwErrorBool) {
2020-10-20 20:28:33 +00:00
console.log('Result: response did not comply');
console.log('-> expected:');
console.log(expectedResponseArg);
console.log('-> but actually received:');
console.log(actualResponse);
2020-07-25 13:34:26 +00:00
throw new Error('response did not comply');
}
2020-07-28 17:24:53 +00:00
return actualResponse;
2020-07-25 13:34:26 +00:00
};
2020-07-25 15:40:10 +00:00
return {
expect
};
2020-07-25 13:34:26 +00:00
};
2020-07-05 17:53:49 +00:00
export const stopTestServer = async () => {
if (testServer) {
await testServer.stop();
2020-09-24 00:45:23 +00:00
await handler.stop();
2020-07-05 17:53:49 +00:00
}
};