Skip to content

Commit 0305974

Browse files
authored
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
1 parent e395024 commit 0305974

File tree

5 files changed

+137
-1
lines changed

5 files changed

+137
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ lib/*.js
1212
auth/**/*.js
1313
iam-token-manager/*.js
1414
index.js
15+
!test/**/index.js
1516
.nyc_output
1617
**/*.d.ts
1718
!*/blob.d.ts

.npmignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
test
1+
test/resources
2+
test/unit
3+
test/.eslintrc.js
24
node_modules
35
coverage
46
.jshintignore

index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ export * from './lib/helper';
2424
export { default as qs } from './lib/querystring';
2525
export { default as contentType } from './lib/content-type';
2626
export * from './lib/stream-to-promise';
27+
export * from './test/utils';

test/utils/index.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* The utility methods in this directory are not to assist in the testing
19+
* of the core, but to assist with the testing of the SDKs that depend on
20+
* this core library. Specifically, the unit tests generated by the
21+
* IBM OpenAPI SDK Gen project rely on these methods.
22+
*/
23+
24+
const unitTestUtils = require('./unit-test-helpers');
25+
26+
module.exports.unitTestUtils = unitTestUtils;

test/utils/unit-test-helpers.js

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* Copyright 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/* istanbul ignore file */
18+
19+
/**
20+
* This module provides a set of helper methods used to reduce code duplication in the generated unit tests
21+
* for the SDKs that depend on this core package. Note that these methods are not used by the tests for this
22+
* package - they are meant to be exported and made available to dependent libraries.
23+
*
24+
* They are included in the `test` directory since they rely on `jest` globals (like `expect`) and this
25+
* requires special linting rules that should not be configured in the rest of the source code.
26+
*/
27+
28+
/**
29+
* Takes the request options constructed by the SDK and checks that the `url` and `method` properties
30+
* were set to their correct values.
31+
*
32+
* @param {Object} options - the options object put together by the SDK, retrieved from the createRequest mock
33+
* @param {String} url - The URL path of the service endpoint, from the paths section of the API definition
34+
* @param {String} string - The HTTP method for the request, from the API definition
35+
* @returns {void}
36+
*/
37+
module.exports.checkUrlAndMethod = function(options, url, method) {
38+
expect(options.url).toEqual(url);
39+
expect(options.method).toEqual(method);
40+
};
41+
42+
/**
43+
* Takes the mock object for the `createRequest` method, extracts the headers that were sent with the call,
44+
* and checks for the expected values for `Accept` and `Content-Type`. This to verify that the SDK sets
45+
* the correct values in the code.
46+
*
47+
* @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class
48+
* @param {String} accept - the expected value for the `Accept` header
49+
* @param {String} contentType - the expected value for the `Content-Type` header
50+
* @returns {void}
51+
*/
52+
module.exports.checkMediaHeaders = function(createRequestMock, accept, contentType) {
53+
const headers = createRequestMock.mock.calls[0][0].defaultOptions.headers;
54+
expect(headers.Accept).toEqual(accept);
55+
expect(headers['Content-Type']).toEqual(contentType);
56+
};
57+
58+
/**
59+
* Takes the mock object for the `createRequest` method, extracts the headers that were sent with the call,
60+
* and checks for the expected value for a user-defined header. This is verify that the SDK accepts header
61+
* parameters and sends them as headers in the request.
62+
*
63+
* @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class
64+
* @param {String} userHeaderName - the name of the header passed by the user, e.g. `Contained-Content-Type`
65+
* @param {String} userHeaderValue - the expected value for the header passed by the user
66+
* @returns {void}
67+
*/
68+
module.exports.checkUserHeader = function(createRequestMock, userHeaderName, userHeaderValue) {
69+
const headers = createRequestMock.mock.calls[0][0].defaultOptions.headers;
70+
expect(headers[userHeaderName]).toEqual(userHeaderValue);
71+
};
72+
73+
/**
74+
* This method simply ensures that the method executed without any issues by extracting
75+
* the argument from the mock object for the `createRequest` method and verifying that it is an object.
76+
*
77+
* @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class
78+
* @returns {void}
79+
*/
80+
module.exports.checkForSuccessfulExecution = function(createRequestMock) {
81+
const sdkParams = createRequestMock.mock.calls[0][0];
82+
expect(typeof sdkParams).toEqual('object');
83+
};
84+
85+
/**
86+
* This method extracts the `options` property from the object passed into `createRequest`. This property is
87+
* an object containing all of the SDK method-specific information (like `path` and `body`) used to build a request.
88+
* This method is just a convenience method for the unit tests to be able to make assertions on the items in the request.
89+
*
90+
* @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class
91+
* @returns {void}
92+
*/
93+
module.exports.getOptions = function(createRequestMock) {
94+
return createRequestMock.mock.calls[0][0].options;
95+
};
96+
97+
/**
98+
* This method simply ensures that the SDK methods return Promises by checking for
99+
* the `then` function - common way to assess whether or not an object is a Promise.
100+
*
101+
* @param {Promise<any>} sdkPromise - the Promise returned by an SDK method
102+
* @returns {void}
103+
*/
104+
module.exports.expectToBePromise = function(sdkPromise) {
105+
expect(typeof sdkPromise.then).toBe('function');
106+
};

0 commit comments

Comments
 (0)