Skip to content

Commit

Permalink
fix: restore err msg for invalid filepathOrObject
Browse files Browse the repository at this point in the history
Although TypeScript users won't need this because the argument is now
typed, it is still useful for JavaScript users
  • Loading branch information
rwalle61 committed Sep 12, 2021
1 parent 08ee985 commit 8141cf9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
20 changes: 9 additions & 11 deletions packages/chai-openapi-response-validator/test/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import chaiResponseValidator from '..';

const { expect } = chai;
const invalidArgErrorMessage =
'The provided argument must be either an absolute filepath or an object representing an OpenAPI specification.';
'The provided argument must be either an absolute filepath or an object representing an OpenAPI specification.\nError details: ';

describe('chaiResponseValidator(stringOrObject)', () => {
describe('chaiResponseValidator(filepathOrObject)', () => {
describe('number', () => {
it('throws an error', () => {
const func = () => chaiResponseValidator(123 as never);
expect(func).to.throw(invalidArgErrorMessage);
expect(func).to.throw(`${invalidArgErrorMessage}Received type 'number'`);
});
});

describe('array', () => {
it('throws an error', () => {
const func = () => chaiResponseValidator([] as never);
expect(func).to.throw(invalidArgErrorMessage);
expect(func).to.throw(`${invalidArgErrorMessage}Received type 'array'`);
});
});

Expand Down Expand Up @@ -51,7 +51,7 @@ describe('chaiResponseValidator(stringOrObject)', () => {
it('throws an error', () => {
const func = () => chaiResponseValidator('./');
expect(func).to.throw(
`${invalidArgErrorMessage}\nError: './' is not an absolute filepath`,
`${invalidArgErrorMessage}'./' is not an absolute filepath`,
);
});
});
Expand All @@ -60,7 +60,7 @@ describe('chaiResponseValidator(stringOrObject)', () => {
it('throws an error', () => {
const func = () => chaiResponseValidator('/non-existent-file.yml');
expect(func).to.throw(
`${invalidArgErrorMessage}\nError: ENOENT: no such file or directory, open '/non-existent-file.yml'`,
`${invalidArgErrorMessage}ENOENT: no such file or directory, open '/non-existent-file.yml'`,
);
});
});
Expand All @@ -71,9 +71,7 @@ describe('chaiResponseValidator(stringOrObject)', () => {
'../../commonTestResources/exampleOpenApiFiles/invalid/fileFormat/neitherYamlNorJson.js',
);
const func = () => chaiResponseValidator(pathToApiSpec);
expect(func).to.throw(
`${invalidArgErrorMessage}\nError: Invalid YAML or JSON:\n`,
);
expect(func).to.throw(`${invalidArgErrorMessage}Invalid YAML or JSON:\n`);
});
});

Expand All @@ -96,7 +94,7 @@ describe('chaiResponseValidator(stringOrObject)', () => {
);
const func = () => chaiResponseValidator(pathToApiSpec);
expect(func).to.throw(
`${invalidArgErrorMessage}\nError: Invalid YAML or JSON:\nduplicated mapping key`,
`${invalidArgErrorMessage}Invalid YAML or JSON:\nduplicated mapping key`,
);
});
});
Expand All @@ -107,7 +105,7 @@ describe('chaiResponseValidator(stringOrObject)', () => {
);
const func = () => chaiResponseValidator(pathToApiSpec);
expect(func).to.throw(
`${invalidArgErrorMessage}\nError: Invalid YAML or JSON:\nduplicated mapping key`,
`${invalidArgErrorMessage}Invalid YAML or JSON:\nduplicated mapping key`,
);
});
});
Expand Down
20 changes: 9 additions & 11 deletions packages/jest-openapi/__test__/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import fs from 'fs-extra';
import jestOpenAPI from '..';

const invalidArgErrorMessage =
'The provided argument must be either an absolute filepath or an object representing an OpenAPI specification.';
'The provided argument must be either an absolute filepath or an object representing an OpenAPI specification.\nError details: ';

describe('jestOpenAPI(stringOrObject)', () => {
describe('jestOpenAPI(filepathOrObject)', () => {
describe('number', () => {
it('throws an error', () => {
const func = () => jestOpenAPI(123 as never);
expect(func).toThrow(new Error(invalidArgErrorMessage));
expect(func).toThrow(`${invalidArgErrorMessage}Received type 'number'`);
});
});

describe('array', () => {
it('throws an error', () => {
const func = () => jestOpenAPI([] as never);
expect(func).toThrow(new Error(invalidArgErrorMessage));
expect(func).toThrow(`${invalidArgErrorMessage}Received type 'array'`);
});
});

Expand Down Expand Up @@ -49,7 +49,7 @@ describe('jestOpenAPI(stringOrObject)', () => {
it('throws an error', () => {
const func = () => jestOpenAPI('./');
expect(func).toThrow(
`${invalidArgErrorMessage}\nError: './' is not an absolute filepath`,
`${invalidArgErrorMessage}'./' is not an absolute filepath`,
);
});
});
Expand All @@ -58,7 +58,7 @@ describe('jestOpenAPI(stringOrObject)', () => {
it('throws an error', () => {
const func = () => jestOpenAPI('/non-existent-file.yml');
expect(func).toThrow(
`${invalidArgErrorMessage}\nError: ENOENT: no such file or directory, open '/non-existent-file.yml'`,
`${invalidArgErrorMessage}ENOENT: no such file or directory, open '/non-existent-file.yml'`,
);
});
});
Expand All @@ -69,9 +69,7 @@ describe('jestOpenAPI(stringOrObject)', () => {
'../../commonTestResources/exampleOpenApiFiles/invalid/fileFormat/neitherYamlNorJson.js',
);
const func = () => jestOpenAPI(pathToApiSpec);
expect(func).toThrow(
`${invalidArgErrorMessage}\nError: Invalid YAML or JSON:\n`,
);
expect(func).toThrow(`${invalidArgErrorMessage}Invalid YAML or JSON:\n`);
});
});

Expand All @@ -94,7 +92,7 @@ describe('jestOpenAPI(stringOrObject)', () => {
);
const func = () => jestOpenAPI(pathToApiSpec);
expect(func).toThrow(
`${invalidArgErrorMessage}\nError: Invalid YAML or JSON:\nduplicated mapping key`,
`${invalidArgErrorMessage}Invalid YAML or JSON:\nduplicated mapping key`,
);
});
});
Expand All @@ -105,7 +103,7 @@ describe('jestOpenAPI(stringOrObject)', () => {
);
const func = () => jestOpenAPI(pathToApiSpec);
expect(func).toThrow(
`${invalidArgErrorMessage}\nError: Invalid YAML or JSON:\nduplicated mapping key`,
`${invalidArgErrorMessage}Invalid YAML or JSON:\nduplicated mapping key`,
);
});
});
Expand Down
7 changes: 3 additions & 4 deletions packages/openapi-validator/lib/openApiSpecFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import yaml from 'js-yaml';
import OpenAPISchemaValidator from 'openapi-schema-validator';
import type { OpenAPI, OpenAPIV2, OpenAPIV3 } from 'openapi-types';
import path from 'path';
import typeOf from 'typeof';
import OpenApi2Spec from './classes/OpenApi2Spec';
import OpenApi3Spec from './classes/OpenApi3Spec';
import { stringify } from './utils/common.utils';
Expand Down Expand Up @@ -32,12 +33,10 @@ function loadSpec(arg: unknown): AnyObject {
if (isObject(arg)) {
return arg;
}
throw new Error();
throw new Error(`Received type '${typeOf(arg)}'`);
} catch (error) {
throw new Error(
`The provided argument must be either an absolute filepath or an object representing an OpenAPI specification.${
error.message ? `\nError: ${error.message}` : ''
}`,
`The provided argument must be either an absolute filepath or an object representing an OpenAPI specification.\nError details: ${error.message}`,
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/openapi-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@
"js-yaml": "^4.0.0",
"openapi-response-validator": "^9.2.0",
"openapi-schema-validator": "^9.2.0",
"path-parser": "^6.1.0"
"path-parser": "^6.1.0",
"typeof": "^1.0.0"
},
"devDependencies": {
"@types/fs-extra": "^9.0.12",
"@types/js-yaml": "^4.0.3",
"@types/request": "^2.48.7",
"@types/superagent": "^4.1.12",
"@types/typeof": "^1.0.0",
"openapi-types": "^9.2.0"
}
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,11 @@
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.1.tgz#8f80dd965ad81f3e1bc26d6f5c727e132721ff40"
integrity sha512-Y0K95ThC3esLEYD6ZuqNek29lNX2EM1qxV8y2FTLUB0ff5wWrk7az+mLrnNFUnaXcgKye22+sFBRXOgpPILZNg==

"@types/typeof@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/typeof/-/typeof-1.0.0.tgz#cb22645285af4ff33c00e85ebe4eebdeff5480c0"
integrity sha512-Qv/U8q1Gnw8czN6Q09ZCm4UdU+/QF7wMy83svkRmRDnW9WX1utFK4QkaXqsKx2vAJTSUrbYErZPSRkP3812Hyw==

"@types/yargs-parser@*":
version "15.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz#cb3f9f741869e20cce330ffbeb9271590483882d"
Expand Down Expand Up @@ -8742,6 +8747,11 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typeof@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/typeof/-/typeof-1.0.0.tgz#9c84403f2323ae5399167275497638ea1d2f2440"
integrity sha1-nIRAPyMjrlOZFnJ1SXY46h0vJEA=

typescript@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.2.3.tgz#39062d8019912d43726298f09493d598048c1ce3"
Expand Down

0 comments on commit 8141cf9

Please sign in to comment.