From 1b6bf5d0dde4d1438ed573627e3cfb845b550cb2 Mon Sep 17 00:00:00 2001 From: Tobias Sauerwein Date: Fri, 5 Jul 2024 15:59:01 +0200 Subject: [PATCH] fix: remove obsolete `try...catch` (#176) --- src/util.ts | 10 ++++------ tests/lib/index.spec.ts | 22 ++++++++++++++++++++++ tests/wrong-external-ref.yaml | 10 ++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 tests/wrong-external-ref.yaml diff --git a/src/util.ts b/src/util.ts index 79b441d..2f136f3 100644 --- a/src/util.ts +++ b/src/util.ts @@ -54,12 +54,10 @@ export const resolve = async ( ) => { const docs = []; - try { - for (const asyncapiDocument of asyncapiDocuments) { - await parse(asyncapiDocument, specVersion, options); - docs.push(asyncapiDocument); - } - } catch (e) {} // eslint-disable-line + for (const asyncapiDocument of asyncapiDocuments) { + await parse(asyncapiDocument, specVersion, options); + docs.push(asyncapiDocument); + } return docs; }; diff --git a/tests/lib/index.spec.ts b/tests/lib/index.spec.ts index 7cf7eec..94257ef 100644 --- a/tests/lib/index.spec.ts +++ b/tests/lib/index.spec.ts @@ -2,8 +2,15 @@ import { describe, expect, test } from '@jest/globals'; import bundle from '../../src'; import { isExternalReference } from '../../src/util'; import path from 'path'; +import { JSONParserError } from '@apidevtools/json-schema-ref-parser'; describe('[integration testing] bundler should ', () => { + // as the working directory might get changed, make sure to reset it + let workingDirectory: string = process.cwd(); + afterEach(() => { + process.chdir(workingDirectory); + }); + test('should return bundled doc', async () => { const files = ['./tests/camera.yml', './tests/audio.yml']; const response = await bundle(files, { @@ -41,6 +48,21 @@ describe('[integration testing] bundler should ', () => { ).resolves; }); + test('should throw if external `$ref` cannot be resolved', async () => { + const files = ['wrong-external-ref.yaml']; + + await expect( + async () => { + await bundle(files, { + xOrigin: true, + base: 'base.yml', + baseDir: path.resolve(process.cwd(), './tests'), + noValidation: true, + }) + } + ).rejects.toThrow(JSONParserError); + }); + test('should be able to bundle base file', async () => { const files = [ './tests/base-option/lights.yaml', diff --git a/tests/wrong-external-ref.yaml b/tests/wrong-external-ref.yaml new file mode 100644 index 0000000..0dac1ee --- /dev/null +++ b/tests/wrong-external-ref.yaml @@ -0,0 +1,10 @@ +asyncapi: '2.2.0' +info: + title: Account Service + version: 1.0.0 + description: This service is in charge of processing user signups +channels: + user/signedup: + subscribe: + message: + $ref: "./audio.yml#/components/messages/WrongReference" \ No newline at end of file