From 8e1d9051ae5dd0c34fca63f3778527985b52a9ed Mon Sep 17 00:00:00 2001 From: StevanFreeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Tue, 14 Feb 2023 22:47:16 -0600 Subject: [PATCH] fix: update integration tests to use a root beforeAll hook that loads the environment variables and assigns values to the api key and base url to be used when constructing the onspring client used in the integration tests --- integrationTests/getAppById.spec.ts | 16 ++---------- integrationTests/getApps.spec.ts | 14 +---------- integrationTests/getAppsByIds.spec.ts | 14 +---------- integrationTests/getFieldById.spec.ts | 16 ++---------- integrationTests/getFieldsByAppId.spec.ts | 14 +---------- integrationTests/mochaRootHooks.ts | 30 +++++++++++++++++++++++ package.json | 7 +++--- 7 files changed, 41 insertions(+), 70 deletions(-) create mode 100644 integrationTests/mochaRootHooks.ts diff --git a/integrationTests/getAppById.spec.ts b/integrationTests/getAppById.spec.ts index 43d9214..4c8eb3a 100644 --- a/integrationTests/getAppById.spec.ts +++ b/integrationTests/getAppById.spec.ts @@ -1,20 +1,8 @@ import { OnspringClient } from '../src/index'; import { expect } from 'chai'; -import * as dotenv from 'dotenv'; -import path from 'path'; -const envPath = path.resolve(__dirname, '.env'); -dotenv.config({ path: envPath }); +import { baseURL, apiKey } from './mochaRootHooks'; describe('getAppById', function () { - this.timeout('30s'); - let baseURL; - let apiKey; - - before(function () { - baseURL = process.env.API_BASE_URL; - apiKey = process.env.SANDBOX_API_KEY; - }); - it('should return an app', async function () { const client = new OnspringClient(baseURL, apiKey); @@ -69,7 +57,7 @@ describe('getAppById', function () { expect(response.statusCode).to.equal(404); expect(response.isSuccessful).to.be.false; - expect(response.message).to.not.be.undefined.and.not.be.null; + expect(response.message).to.be.undefined; expect(response.data).to.be.null; }); }); diff --git a/integrationTests/getApps.spec.ts b/integrationTests/getApps.spec.ts index c430fe0..98a26d6 100644 --- a/integrationTests/getApps.spec.ts +++ b/integrationTests/getApps.spec.ts @@ -1,20 +1,8 @@ import { OnspringClient, PagingRequest } from '../src/index'; import { expect } from 'chai'; -import * as dotenv from 'dotenv'; -import path from 'path'; -const envPath = path.resolve(__dirname, '.env'); -dotenv.config({ path: envPath }); +import { baseURL, apiKey } from './mochaRootHooks'; describe('getApps', function () { - this.timeout('30s'); - let baseURL; - let apiKey; - - before(function () { - baseURL = process.env.API_BASE_URL; - apiKey = process.env.SANDBOX_API_KEY; - }); - it('should return a paged list of apps', async function () { const client = new OnspringClient(baseURL, apiKey); const response = await client.getApps(); diff --git a/integrationTests/getAppsByIds.spec.ts b/integrationTests/getAppsByIds.spec.ts index 8a26738..0cac689 100644 --- a/integrationTests/getAppsByIds.spec.ts +++ b/integrationTests/getAppsByIds.spec.ts @@ -1,20 +1,8 @@ import { OnspringClient } from '../src/index'; import { expect } from 'chai'; -import * as dotenv from 'dotenv'; -import path from 'path'; -const envPath = path.resolve(__dirname, '.env'); -dotenv.config({ path: envPath }); +import { baseURL, apiKey } from './mochaRootHooks'; describe('getAppsByIds', function () { - this.timeout('30s'); - let baseURL; - let apiKey; - - before(function () { - baseURL = process.env.API_BASE_URL; - apiKey = process.env.SANDBOX_API_KEY; - }); - it('should return a collection of apps', async function () { const client = new OnspringClient(baseURL, apiKey); const appIds = process.env.TEST_APP_IDS; diff --git a/integrationTests/getFieldById.spec.ts b/integrationTests/getFieldById.spec.ts index 22400e7..78780a1 100644 --- a/integrationTests/getFieldById.spec.ts +++ b/integrationTests/getFieldById.spec.ts @@ -1,20 +1,8 @@ import { OnspringClient } from '../src/index'; import { expect } from 'chai'; -import * as dotenv from 'dotenv'; -import path from 'path'; -const envPath = path.resolve(__dirname, '.env'); -dotenv.config({ path: envPath }); +import { baseURL, apiKey } from './mochaRootHooks'; describe('getAppById', function () { - this.timeout('30s'); - let baseURL; - let apiKey; - - before(function () { - baseURL = process.env.API_BASE_URL; - apiKey = process.env.SANDBOX_API_KEY; - }); - it('should return a field', async function () { const client = new OnspringClient(baseURL, apiKey); @@ -73,7 +61,7 @@ describe('getAppById', function () { expect(response.statusCode).to.equal(404); expect(response.isSuccessful).to.be.false; - expect(response.message).to.not.be.undefined.and.not.be.null; + expect(response.message).to.be.undefined; expect(response.data).to.be.null; }); }); diff --git a/integrationTests/getFieldsByAppId.spec.ts b/integrationTests/getFieldsByAppId.spec.ts index 1e9c69a..ea03632 100644 --- a/integrationTests/getFieldsByAppId.spec.ts +++ b/integrationTests/getFieldsByAppId.spec.ts @@ -1,20 +1,8 @@ import { OnspringClient, PagingRequest } from '../src/index'; import { expect } from 'chai'; -import * as dotenv from 'dotenv'; -import path from 'path'; -const envPath = path.resolve(__dirname, '.env'); -dotenv.config({ path: envPath }); +import { baseURL, apiKey } from './mochaRootHooks'; describe('getFieldsByAppId', function () { - this.timeout('30s'); - let baseURL; - let apiKey; - - before(function () { - baseURL = process.env.API_BASE_URL; - apiKey = process.env.SANDBOX_API_KEY; - }); - it('should return a paged list of fields', async function () { const client = new OnspringClient(baseURL, apiKey); diff --git a/integrationTests/mochaRootHooks.ts b/integrationTests/mochaRootHooks.ts new file mode 100644 index 0000000..21709c9 --- /dev/null +++ b/integrationTests/mochaRootHooks.ts @@ -0,0 +1,30 @@ +import { type Context } from 'mocha'; +import { expect } from 'chai'; +import * as dotenv from 'dotenv'; +import path from 'path'; +const envPath = path.resolve(__dirname, '.env'); +dotenv.config({ path: envPath }); + +let baseURL: string | undefined; +let apiKey: string | undefined; + +export const mochaHooks = (): Mocha.RootHookObject => { + return { + beforeAll(this: Context) { + this.timeout('30s'); + + baseURL = process.env.API_BASE_URL; + apiKey = process.env.SANDBOX_API_KEY; + + if (baseURL === undefined) { + return expect.fail('API_BASE_URL is not defined'); + } + + if (apiKey === undefined) { + return expect.fail('SANDBOX_API_KEY is not defined'); + } + }, + }; +}; + +export { baseURL, apiKey }; diff --git a/package.json b/package.json index 30079ce..f0c5ed7 100644 --- a/package.json +++ b/package.json @@ -40,10 +40,11 @@ "format-staged": "pretty-quick --staged", "format": "pretty-quick", "build": "npm run lint-fix && npm run tests && npm run format && npm run clean && tsc --project tsconfig.cjs.json & tsc --project tsconfig.esm.json", - "test": "mocha -r ts-node/register", + "test:unit": "mocha -r ts-node/register", + "test:integration": "mocha -r ts-node/register -r integrationTests/mochaRootHooks.ts", "tests:unit": "mocha -R progress -r ts-node/register ./tests/**/*.spec.ts", - "tests:integration": "mocha -R progress -r ts-node/register ./integrationTests/**/*.spec.ts", - "tests": "mocha -R progress -r ts-node/register ./tests/**/*.spec.ts ./integrationTests/**/*.spec.ts", + "tests:integration": "mocha -R progress -r ts-node/register -r integrationTests/mochaRootHooks.ts ./integrationTests/**/*.spec.ts", + "tests": "mocha -R progress -r ts-node/register ./tests/**/*.spec.ts -r ts-node/register -r integrationTests/mochaRootHooks.ts ./integrationTests/**/*.spec.ts", "test-coverage": "nyc npm run tests:unit", "test-coverage:ci": "nyc npm run tests", "prepare": "husky install"