Skip to content

Commit

Permalink
fix: move test utilities to lib/ and rely directly on expect
Browse files Browse the repository at this point in the history
this removes any need to include test code in the published package and avoids mixing dev dependencies into the published dependencies

also, update axios-cookie-support to use the latest release and add tough-cookie, as it is now a required peer dependency
  • Loading branch information
dpopp07 committed Jul 22, 2020
1 parent 5ac3950 commit 75f0902
Show file tree
Hide file tree
Showing 7 changed files with 2,114 additions and 2,493 deletions.
4 changes: 1 addition & 3 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
test/resources
test/unit
test/.eslintrc.js
test
coverage
.jshintignore
.hshintrc
Expand Down
4 changes: 3 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ export * from './lib/helper';
export { default as qs } from './lib/querystring';
export { default as contentType } from './lib/content-type';
export * from './lib/stream-to-promise';
export { SdkUnitTestUtilities } from './lib/sdk-test-helpers';

import * as unitTestUtils from './lib/sdk-test-helpers';
export { unitTestUtils };
177 changes: 77 additions & 100 deletions lib/sdk-test-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2019 IBM Corp. All Rights Reserved.
* Copyright 2019, 2020 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,113 +14,90 @@
* limitations under the License.
*/

/* istanbul ignore file */

/** Configuration options for the testing utilities. */
export interface Options {
/** The Jest expect function to use */
expect: Function;
}
import expect = require('expect');

/**
* This class provides a set of helper methods used to reduce code duplication in the generated unit tests
* This module provides a set of helper methods used to reduce code duplication in the generated unit tests
* for the SDKs that depend on this core package. Note that these methods are not used by the tests for this
* package - they are meant to be exported and made available to dependent libraries.
*/
export class SdkUnitTestUtilities {
private expect: Function;

/**
* Create a new BasicAuthenticator instance. !!!!!!!
*
* @param {object} options Configuration options for basic authentication. !!!!!!!!!!
* @param {string} options.username The username portion of basic authentication. !!!!!!!!!!
* @param {string} options.password The password portion of basic authentication. !!!!!!!!!!
* @throws {Error} The configuration options are not valid. !!!!!!!!!!
*/
constructor(options: Options) {
// injecting the dependency on the "expect" method from `jest` so that
// this module does not rely on "devDependencies"
this.expect = options.expect;
}

/**
* Takes the request options constructed by the SDK and checks that the `url` and `method` properties
* were set to their correct values.
*
* @param {Object} options - the options object put together by the SDK, retrieved from the createRequest mock
* @param {String} url - The URL path of the service endpoint, from the paths section of the API definition
* @param {String} string - The HTTP method for the request, from the API definition
* @returns {void}
*/
public checkUrlAndMethod(options, url: string, method: any) {
this.expect(options.url).toEqual(url);
this.expect(options.method).toEqual(method);
};
/**
* Takes the request options constructed by the SDK and checks that the `url` and `method` properties
* were set to their correct values.
*
* @param {Object} options - the options object put together by the SDK, retrieved from the createRequest mock
* @param {String} url - The URL path of the service endpoint, from the paths section of the API definition
* @param {String} string - The HTTP method for the request, from the API definition
* @returns {void}
*/
export function checkUrlAndMethod(options, url: string, method: any) {
expect(options.url).toEqual(url);
expect(options.method).toEqual(method);
};

/**
* Takes the mock object for the `createRequest` method, extracts the headers that were sent with the call,
* and checks for the expected values for `Accept` and `Content-Type`. This to verify that the SDK sets
* the correct values in the code.
*
* @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class
* @param {String} accept - the expected value for the `Accept` header
* @param {String} contentType - the expected value for the `Content-Type` header
* @returns {void}
*/
public checkMediaHeaders(createRequestMock, accept: string, contentType: string) {
const headers = createRequestMock.mock.calls[0][0].defaultOptions.headers;
this.expect(headers.Accept).toEqual(accept);
this.expect(headers['Content-Type']).toEqual(contentType);
};
/**
* Takes the mock object for the `createRequest` method, extracts the headers that were sent with the call,
* and checks for the expected values for `Accept` and `Content-Type`. This to verify that the SDK sets
* the correct values in the code.
*
* @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class
* @param {String} accept - the expected value for the `Accept` header
* @param {String} contentType - the expected value for the `Content-Type` header
* @returns {void}
*/
export function checkMediaHeaders(createRequestMock, accept: string, contentType: string) {
const headers = createRequestMock.mock.calls[0][0].defaultOptions.headers;
expect(headers.Accept).toEqual(accept);
expect(headers['Content-Type']).toEqual(contentType);
};

/**
* Takes the mock object for the `createRequest` method, extracts the headers that were sent with the call,
* and checks for the expected value for a user-defined header. This is verify that the SDK accepts header
* parameters and sends them as headers in the request.
*
* @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class
* @param {String} userHeaderName - the name of the header passed by the user, e.g. `Contained-Content-Type`
* @param {String} userHeaderValue - the expected value for the header passed by the user
* @returns {void}
*/
public checkUserHeader(createRequestMock, userHeaderName: string, userHeaderValue: string) {
const headers = createRequestMock.mock.calls[0][0].defaultOptions.headers;
this.expect(headers[userHeaderName]).toEqual(userHeaderValue);
};
/**
* Takes the mock object for the `createRequest` method, extracts the headers that were sent with the call,
* and checks for the expected value for a user-defined header. This is verify that the SDK accepts header
* parameters and sends them as headers in the request.
*
* @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class
* @param {String} userHeaderName - the name of the header passed by the user, e.g. `Contained-Content-Type`
* @param {String} userHeaderValue - the expected value for the header passed by the user
* @returns {void}
*/
export function checkUserHeader(createRequestMock, userHeaderName: string, userHeaderValue: string) {
const headers = createRequestMock.mock.calls[0][0].defaultOptions.headers;
expect(headers[userHeaderName]).toEqual(userHeaderValue);
};

/**
* This method simply ensures that the method executed without any issues by extracting
* the argument from the mock object for the `createRequest` method and verifying that it is an object.
*
* @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class
* @returns {void}
*/
public checkForSuccessfulExecution(createRequestMock) {
const sdkParams = createRequestMock.mock.calls[0][0];
this.expect(typeof sdkParams).toEqual('object');
};
/**
* This method simply ensures that the method executed without any issues by extracting
* the argument from the mock object for the `createRequest` method and verifying that it is an object.
*
* @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class
* @returns {void}
*/
export function checkForSuccessfulExecution(createRequestMock) {
const sdkParams = createRequestMock.mock.calls[0][0];
expect(typeof sdkParams).toEqual('object');
};

/**
* This method extracts the `options` property from the object passed into `createRequest`. This property is
* an object containing all of the SDK method-specific information (like `path` and `body`) used to build a request.
* This method is just a convenience method for the unit tests to be able to make assertions on the items in the request.
*
* @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class
* @returns {Object}
*/
public getOptions(createRequestMock) {
return createRequestMock.mock.calls[0][0].options;
};
/**
* This method extracts the `options` property from the object passed into `createRequest`. This property is
* an object containing all of the SDK method-specific information (like `path` and `body`) used to build a request.
* This method is just a convenience method for the unit tests to be able to make assertions on the items in the request.
*
* @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class
* @returns {Object}
*/
export function getOptions(createRequestMock) {
return createRequestMock.mock.calls[0][0].options;
};

/**
* This method simply ensures that the SDK methods return Promises by checking for
* the `then` function - common way to assess whether or not an object is a Promise.
*
* @param {Promise<any>} sdkPromise - the Promise returned by an SDK method
* @returns {void}
*/
public expectToBePromise(sdkPromise) {
this.expect(typeof sdkPromise.then).toBe('function');
};
}
/**
* This method simply ensures that the SDK methods return Promises by checking for
* the `then` function - common way to assess whether or not an object is a Promise.
*
* @param {Promise<any>} sdkPromise - the Promise returned by an SDK method
* @returns {void}
*/
export function expectToBePromise(sdkPromise) {
expect(typeof sdkPromise.then).toBe('function');
};
Loading

0 comments on commit 75f0902

Please sign in to comment.