From 030597436084895f6a979cc9355c8b1b9b384585 Mon Sep 17 00:00:00 2001 From: Dustin Popp Date: Tue, 15 Oct 2019 09:00:23 -0500 Subject: [PATCH] feat: export unit test utility methods to be used in SDKs (#65) * these are not for the core tests themselves - the generated unit tests for service client sdks rely on these methods --- .gitignore | 1 + .npmignore | 4 +- index.ts | 1 + test/utils/index.js | 26 ++++++++ test/utils/unit-test-helpers.js | 106 ++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 test/utils/index.js create mode 100644 test/utils/unit-test-helpers.js diff --git a/.gitignore b/.gitignore index 900488897..f3c0010ef 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ lib/*.js auth/**/*.js iam-token-manager/*.js index.js +!test/**/index.js .nyc_output **/*.d.ts !*/blob.d.ts diff --git a/.npmignore b/.npmignore index e6b7c7c90..5feb5b752 100644 --- a/.npmignore +++ b/.npmignore @@ -1,4 +1,6 @@ -test +test/resources +test/unit +test/.eslintrc.js node_modules coverage .jshintignore diff --git a/index.ts b/index.ts index 1c24c33a9..ab6f6499e 100644 --- a/index.ts +++ b/index.ts @@ -24,3 +24,4 @@ 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 * from './test/utils'; diff --git a/test/utils/index.js b/test/utils/index.js new file mode 100644 index 000000000..93a0d48cc --- /dev/null +++ b/test/utils/index.js @@ -0,0 +1,26 @@ +/** + * Copyright 2019 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + /* + * The utility methods in this directory are not to assist in the testing + * of the core, but to assist with the testing of the SDKs that depend on + * this core library. Specifically, the unit tests generated by the + * IBM OpenAPI SDK Gen project rely on these methods. + */ + + const unitTestUtils = require('./unit-test-helpers'); + + module.exports.unitTestUtils = unitTestUtils; diff --git a/test/utils/unit-test-helpers.js b/test/utils/unit-test-helpers.js new file mode 100644 index 000000000..e9c8225e2 --- /dev/null +++ b/test/utils/unit-test-helpers.js @@ -0,0 +1,106 @@ +/** + * Copyright 2019 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* istanbul ignore file */ + +/** + * 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. + * + * They are included in the `test` directory since they rely on `jest` globals (like `expect`) and this + * requires special linting rules that should not be configured in the rest of the source code. + */ + +/** + * 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} + */ +module.exports.checkUrlAndMethod = function(options, url, method) { + 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} + */ +module.exports.checkMediaHeaders = function(createRequestMock, accept, contentType) { + 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} + */ +module.exports.checkUserHeader = function(createRequestMock, userHeaderName, userHeaderValue) { + 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} + */ +module.exports.checkForSuccessfulExecution = function(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 {void} + */ +module.exports.getOptions = function(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} sdkPromise - the Promise returned by an SDK method + * @returns {void} + */ +module.exports.expectToBePromise = function(sdkPromise) { + expect(typeof sdkPromise.then).toBe('function'); +};