Skip to content

Commit

Permalink
test: adding error testing and a nock clean
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck committed Jun 18, 2024
1 parent 159112c commit d65cef8
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
4 changes: 2 additions & 2 deletions testHelpers/getResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ type GeneratorClientMethod = (...args: unknown[]) => AsyncGenerator<unknown>;
* @param {boolean} generator - Whether the client method is a generator
* @param {T} client - The client to call the method on
* @param {string} clientMethod - The method to call on the clientMethod
* @param {Array<unknown> }args - The arguments to pass to the client method
* @param {Array<unknown>} args - The arguments to pass to the client method
*
* @return {Promise<unknown | Array<unknown>>} The results of the client method
*/
export const getResults = async <T>(
generator: boolean,
client: T,
clientMethod: keyof T,
args: [unknown?, unknown?, unknown?] = [],
args: Array<unknown> = [],
): Promise<unknown | Array<unknown>> => {
if (!client) {
throw new Error('Client is not defined');
Expand Down
12 changes: 6 additions & 6 deletions testHelpers/types/SDKTestCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import nock from 'nock';
/**
* A Tuple that contains the status code of the response and the nock interceptor function
*/
type TestResponse = [
export type TestResponse = [
/**
* The status code of the response
*/
Expand All @@ -14,13 +14,13 @@ type TestResponse = [
*
* This can either be the body of the response or a nock interceptor function
*/
nock.InterceptFunction?,
(nock.InterceptFunction | Record<string, unknown> | string)?,
];

/**
* A Tuple that contains the method, path, and body of a request nock will intercept.
*/
type TestRequest = [
export type TestRequest = [
/**
* The path of the request
*/
Expand All @@ -34,7 +34,7 @@ type TestRequest = [
/**
* The body of the request
*/
nock.RequestBodyMatcher?,
nock.RequestBodyMatcher,
];

export type SDKTestCase<T> = {
Expand Down Expand Up @@ -76,7 +76,7 @@ export type SDKTestCase<T> = {
/**
* The parameters that will be passed to the client method
*/
parameters?: [unknown, unknown, unknown];
parameters?: Array<unknown>;

/**
* Tell the test that the client method is a generator
Expand All @@ -86,7 +86,7 @@ export type SDKTestCase<T> = {
/**
* Tell the test that the response is going to be an error
*/
error: boolean;
error: boolean | Error | string;

/**
* The expected response from the call to the client method
Expand Down
4 changes: 2 additions & 2 deletions testHelpers/types/TestTuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SDKTestCase } from './SDKTestCase';
/**
* TestTuple is a tuple that contains the name of the test suite and an array of SDKTestCase objects.
*/
export type TestTuple = {
export type TestTuple<T> = {
/**
* The name of the test suite. (Used for the describe block in the test file)
*/
Expand All @@ -12,5 +12,5 @@ export type TestTuple = {
/**
* An array of SDKTestCase objects.
*/
tests: Array<SDKTestCase<unknown>>;
tests: Array<SDKTestCase<T>>;
};
63 changes: 59 additions & 4 deletions testHelpers/vonageTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@ import { getResults } from './getResults';
*
* @param {TestTuple} testDataSets - An array of test data sets
*/
export const VonageTest = <T>(testDataSets: TestTuple[]) => {
describe.each<TestTuple>(testDataSets)('$label', ({ tests }) => {
test.each<SDKTestCase<T>>(tests)(
export const VonageTest = <T>(testDataSets: TestTuple<T>[]) => {
describe.each<TestTuple<T>>(testDataSets)('$name', ({ tests }) => {
afterEach(function () {
nock.cleanAll();
});

const successTests = tests.filter(
({ error }) => !error,
);

const failureTests = tests.filter(
({ error }) => !!error,
);


// JEST will error out if there are no tests to run
test.each<SDKTestCase<T>>(successTests)(
'Can $label',
async ({
baseUrl,
Expand Down Expand Up @@ -42,8 +56,49 @@ export const VonageTest = <T>(testDataSets: TestTuple[]) => {

expect(results).toEqual(expected);
expect(nock.isDone()).toBeTruthy();
},
);

// We might always have a failure test
if (failureTests.length < 1) {
return;
}

test.each<SDKTestCase<T>>(failureTests)(
'Will throw $label',
async ({
baseUrl,
reqHeaders,
requests,
responses,
client,
clientMethod,
parameters,
error,
}) => {
if ((error !instanceof Error || typeof error !== 'string')) {
throw new Error('Error must be a string or an instance of Error');
}

const scope = nock(baseUrl, {
reqheaders: reqHeaders,
});

requests.forEach((request, index) => {
(scope as nock.Scope)
.intercept(...request)
.reply(...responses[index]);
});

await expect(() => getResults<T>(
false,
client,
clientMethod,
parameters,
)).rejects.toThrow(
error,
);

expect(results).toEqual(expected);
expect(nock.isDone()).toBeTruthy();
},
);
Expand Down

0 comments on commit d65cef8

Please sign in to comment.