From c11dc0e5830be56caa616429f434b4525c57f478 Mon Sep 17 00:00:00 2001 From: Ansh Chaturvedi Date: Wed, 7 Aug 2024 16:49:05 -0400 Subject: [PATCH] fix: strip stacktrace off of actual errors in tests Adds a utility function to strip stacktraces off of errors since they are machine/platform dependent and cannot hard-code them into tests. Ticket: DX-660 --- packages/openapi-generator/src/error.ts | 9 +++++++++ packages/openapi-generator/test/apiSpec.test.ts | 3 ++- packages/openapi-generator/test/externalModule.test.ts | 3 ++- packages/openapi-generator/test/resolve.test.ts | 3 ++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/openapi-generator/src/error.ts b/packages/openapi-generator/src/error.ts index 781d108d..e1e1258d 100644 --- a/packages/openapi-generator/src/error.ts +++ b/packages/openapi-generator/src/error.ts @@ -11,3 +11,12 @@ export function errorLeft(message: string): E.Either { return E.left(messageWithStacktrace); } + +/** + * Testing utility to strip the stacktrace from errors. + * @param errors the list of errors to strip + * @returns the errors without the stacktrace + */ +export function stripStacktraceOfErrors(errors: string[]) { + return errors.map((e) => e!.split('\n')[0]); +} diff --git a/packages/openapi-generator/test/apiSpec.test.ts b/packages/openapi-generator/test/apiSpec.test.ts index 08c96ccd..52a7030b 100644 --- a/packages/openapi-generator/test/apiSpec.test.ts +++ b/packages/openapi-generator/test/apiSpec.test.ts @@ -6,6 +6,7 @@ import type { NestedDirectoryJSON } from 'memfs'; import { TestProject } from './testProject'; import { parseApiSpec, parseApiSpecComment, type Route } from '../src'; import { MOCK_NODE_MODULES_DIR } from './externalModules'; +import { stripStacktraceOfErrors } from '../src/error'; async function testCase( description: string, @@ -51,7 +52,7 @@ async function testCase( } } - assert.deepEqual(errors, expectedErrors); + assert.deepEqual(stripStacktraceOfErrors(errors), expectedErrors); assert.deepEqual(actual, expected); }); } diff --git a/packages/openapi-generator/test/externalModule.test.ts b/packages/openapi-generator/test/externalModule.test.ts index 4a09985a..07891023 100644 --- a/packages/openapi-generator/test/externalModule.test.ts +++ b/packages/openapi-generator/test/externalModule.test.ts @@ -5,6 +5,7 @@ import * as p from 'path'; import { parsePlainInitializer, Project, type Schema } from '../src'; import { KNOWN_IMPORTS } from '../src/knownImports'; +import { stripStacktraceOfErrors } from '../src/error'; /** External library parsing test case * @@ -49,7 +50,7 @@ async function testCase( } assert.deepEqual(actual, expected[path]); - assert.deepEqual(errors, expectedErrors[path] ?? []); + assert.deepEqual(stripStacktraceOfErrors(errors), expectedErrors[path] ?? []); } // If we are expecting errors in a file that wasn't parsed, raise that here diff --git a/packages/openapi-generator/test/resolve.test.ts b/packages/openapi-generator/test/resolve.test.ts index b6a11d91..afe0fdd7 100644 --- a/packages/openapi-generator/test/resolve.test.ts +++ b/packages/openapi-generator/test/resolve.test.ts @@ -6,6 +6,7 @@ import test from 'node:test'; import { TestProject } from './testProject'; import { parseCodecInitializer, Project, type Schema } from '../src'; import { MOCK_NODE_MODULES_DIR } from './externalModules'; +import { stripStacktraceOfErrors } from '../src/error'; async function testCase( description: string, @@ -43,7 +44,7 @@ async function testCase( } } - assert.deepEqual(errors, expectedErrors); + assert.deepEqual(stripStacktraceOfErrors(errors), expectedErrors); assert.deepEqual(actual, expected); }); }