Skip to content

Commit

Permalink
Added request to MorningstarError and improved test runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
bre1470 committed Aug 2, 2024
1 parent e026c1b commit 578d40a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- name: Install dependencies
run: npm install

- name: Build webpack
- name: Build webpack bundles
run: npm run webpack

- name: Run unit-tests
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"reset": "rm -rf bin/ build/ code/ node_modules/ ; npm i",
"test": "npm run test:unit-tests",
"test:morningstar": "npx -y newman run --bail tmp/Collection.json -e tmp/Environment.json",
"test:unit-tests": "npm run webpack && node -r ts-node/register --env-file .env test/unit-tests/runtime",
"test:unit-tests": "npm run webpack && ts-node test/unit-tests/runtime",
"watch": "tsc -p src/ -w",
"webpack": "npm run scripts && webpack && npm run webpack:dts",
"webpack:dts": "npm run webpack:dts:mc && npm run webpack:dts:mrn && npm run webpack:dts:mts",
Expand Down
5 changes: 5 additions & 0 deletions src/Shared/MorningstarError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class MorningstarError extends Error {


public constructor (
request: RequestInit,
response: Response
) {
super(
Expand All @@ -41,6 +42,7 @@ export class MorningstarError extends Error {
'Unknown Error'
);

this.request = request;
this.response = response;
}

Expand All @@ -52,6 +54,9 @@ export class MorningstarError extends Error {
* */


public readonly request: RequestInit;


public readonly response: Response;


Expand Down
14 changes: 14 additions & 0 deletions test/unit-tests/Shared/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as Assert from 'node:assert/strict';
import * as MC from '../../../code/morningstar-connectors.src';

export async function api_access(
apiOptions: MC.Shared.MorningstarAPIOptions
) {
const api = new MC.Shared.MorningstarAPI(apiOptions);

Assert.ok(
await api.access.authenticate(),
'Morningstar API credentials should be valid.'
);

}
107 changes: 77 additions & 30 deletions test/unit-tests/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,66 @@ import * as JSDOM from 'jsdom';

/* *
*
* Constants
* Functions
*
* */


const api: Shared.MorningstarAPIOptions = {
access: {
password: process.env.MORNINGSTAR_PASSWORD,
username: process.env.MORNINGSTAR_USERNAME
},
url: 'https://www.emea-api.morningstar.com'
};
async function getAPIOptions(): Promise<Shared.MorningstarAPIOptions> {
const apiOptions: Shared.MorningstarAPIOptions = {
url: 'https://www.emea-api.morningstar.com'
};

if ((await FS.lstat('.env')).isFile()) {
process.loadEnvFile('.env');
}

/* *
*
* Functions
*
* */
if (
process.env.MORNINGSTAR_PASSWORD &&
process.env.MORNINGSTAR_USERNAME
) {
apiOptions.access = {
password: process.env.MORNINGSTAR_PASSWORD,
username: process.env.MORNINGSTAR_USERNAME
}
}

return apiOptions;
}


function logError(
async function logError(
error: unknown
) {
): Promise<void> {

if (error instanceof Error) {
console.error(error);
const request = (error as Shared.MorningstarError).request;
const response = (error as Shared.MorningstarError).response;

// Duck typing to avoid real import before tests
if (response) {
const headers = (
request.headers instanceof Headers ?
request.headers :
new Headers(request.headers)
);

console.error(error.message);
console.error('REQUEST:', new Map(headers.entries()));
console.error('RESPONSE:', await response.text());

} else {

console.error(error);

}

} else {

console.error('' + error);

}

}


Expand Down Expand Up @@ -88,18 +119,14 @@ function prepareGlobals() {


async function runUnitTests() {

prepareGlobals();

const failures: Array<string> = [];
const successes: Array<string> = [];

let testCounter = 0;
const stdout = process.stdout;

let test: unknown;
let unitTests: Record<string, unknown>;

for (let path of await FS.readdir(__dirname, { recursive: true })) {
for (let path of (await FS.readdir(__dirname, { recursive: true })).sort()) {

if (!path.endsWith('test.ts')) {
continue;
Expand All @@ -108,31 +135,45 @@ async function runUnitTests() {
path = './' + path.substring(0, path.length - 3);
unitTests = await import(path) as Record<string, unknown>;

for (const testName of Object.keys(unitTests)) {
for (let testName of Object.keys(unitTests).sort()) {

test = unitTests[testName];
testName = testName.replace('_', ' ');

if (typeof test === 'function') {

try {
++testCounter;
await test(api);

stdout.write(`[${new Date().toISOString().substring(0, 19).replace('T', ' ')}]`);
stdout.write(` Test ${testName} ... `);

await test(await getAPIOptions());

stdout.write('success.\n');

successes.push(testName);

} catch (error) {
logError(error);

stdout.write('failure.\n');

await logError(error);

failures.push(testName);

}
}
}

}

const total = successes.length + failures.length;

console.info(
successes.length,
'of',
testCounter,
(testCounter === 1 ? 'test' : 'tests'),
total,
(total === 1 ? 'test' : 'tests'),
'succeeded.'
);

Expand All @@ -152,7 +193,13 @@ async function runUnitTests() {
* */


runUnitTests().catch(error => {
logError(error);
prepareGlobals();


runUnitTests().catch(async error => {

await logError(error);

process.exit(1);

});

0 comments on commit 578d40a

Please sign in to comment.