diff --git a/testHelpers/getResults.ts b/testHelpers/getResults.ts index 89da9e42..64a6e1d8 100644 --- a/testHelpers/getResults.ts +++ b/testHelpers/getResults.ts @@ -13,7 +13,7 @@ type GeneratorClientMethod = (...args: unknown[]) => AsyncGenerator; * @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 }args - The arguments to pass to the client method + * @param {Array} args - The arguments to pass to the client method * * @return {Promise>} The results of the client method */ @@ -21,7 +21,7 @@ export const getResults = async ( generator: boolean, client: T, clientMethod: keyof T, - args: [unknown?, unknown?, unknown?] = [], + args: Array = [], ): Promise> => { if (!client) { throw new Error('Client is not defined'); diff --git a/testHelpers/types/SDKTestCase.ts b/testHelpers/types/SDKTestCase.ts index a72af66b..eb54bd06 100644 --- a/testHelpers/types/SDKTestCase.ts +++ b/testHelpers/types/SDKTestCase.ts @@ -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 */ @@ -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)?, ]; /** * 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 */ @@ -34,7 +34,7 @@ type TestRequest = [ /** * The body of the request */ - nock.RequestBodyMatcher?, + nock.RequestBodyMatcher, ]; export type SDKTestCase = { @@ -76,7 +76,7 @@ export type SDKTestCase = { /** * The parameters that will be passed to the client method */ - parameters?: [unknown, unknown, unknown]; + parameters?: Array; /** * Tell the test that the client method is a generator @@ -86,7 +86,7 @@ export type SDKTestCase = { /** * 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 diff --git a/testHelpers/types/TestTuple.ts b/testHelpers/types/TestTuple.ts index bb6f2367..21495c79 100644 --- a/testHelpers/types/TestTuple.ts +++ b/testHelpers/types/TestTuple.ts @@ -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 = { /** * The name of the test suite. (Used for the describe block in the test file) */ @@ -12,5 +12,5 @@ export type TestTuple = { /** * An array of SDKTestCase objects. */ - tests: Array>; + tests: Array>; }; diff --git a/testHelpers/vonageTest.ts b/testHelpers/vonageTest.ts index 9bf5cea7..fc228af6 100644 --- a/testHelpers/vonageTest.ts +++ b/testHelpers/vonageTest.ts @@ -8,9 +8,23 @@ import { getResults } from './getResults'; * * @param {TestTuple} testDataSets - An array of test data sets */ -export const VonageTest = (testDataSets: TestTuple[]) => { - describe.each(testDataSets)('$label', ({ tests }) => { - test.each>(tests)( +export const VonageTest = (testDataSets: TestTuple[]) => { + describe.each>(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>(successTests)( 'Can $label', async ({ baseUrl, @@ -42,8 +56,49 @@ export const VonageTest = (testDataSets: TestTuple[]) => { expect(results).toEqual(expected); expect(nock.isDone()).toBeTruthy(); + }, + ); + + // We might always have a failure test + if (failureTests.length < 1) { + return; + } + + test.each>(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( + false, + client, + clientMethod, + parameters, + )).rejects.toThrow( + error, + ); - expect(results).toEqual(expected); expect(nock.isDone()).toBeTruthy(); }, );