Skip to content

Commit c11dc0e

Browse files
author
Ansh Chaturvedi
committed
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
1 parent 96a906a commit c11dc0e

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

packages/openapi-generator/src/error.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ export function errorLeft(message: string): E.Either<string, never> {
1111

1212
return E.left(messageWithStacktrace);
1313
}
14+
15+
/**
16+
* Testing utility to strip the stacktrace from errors.
17+
* @param errors the list of errors to strip
18+
* @returns the errors without the stacktrace
19+
*/
20+
export function stripStacktraceOfErrors(errors: string[]) {
21+
return errors.map((e) => e!.split('\n')[0]);
22+
}

packages/openapi-generator/test/apiSpec.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { NestedDirectoryJSON } from 'memfs';
66
import { TestProject } from './testProject';
77
import { parseApiSpec, parseApiSpecComment, type Route } from '../src';
88
import { MOCK_NODE_MODULES_DIR } from './externalModules';
9+
import { stripStacktraceOfErrors } from '../src/error';
910

1011
async function testCase(
1112
description: string,
@@ -51,7 +52,7 @@ async function testCase(
5152
}
5253
}
5354

54-
assert.deepEqual(errors, expectedErrors);
55+
assert.deepEqual(stripStacktraceOfErrors(errors), expectedErrors);
5556
assert.deepEqual(actual, expected);
5657
});
5758
}

packages/openapi-generator/test/externalModule.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as p from 'path';
55

66
import { parsePlainInitializer, Project, type Schema } from '../src';
77
import { KNOWN_IMPORTS } from '../src/knownImports';
8+
import { stripStacktraceOfErrors } from '../src/error';
89

910
/** External library parsing test case
1011
*
@@ -49,7 +50,7 @@ async function testCase(
4950
}
5051

5152
assert.deepEqual(actual, expected[path]);
52-
assert.deepEqual(errors, expectedErrors[path] ?? []);
53+
assert.deepEqual(stripStacktraceOfErrors(errors), expectedErrors[path] ?? []);
5354
}
5455

5556
// If we are expecting errors in a file that wasn't parsed, raise that here

packages/openapi-generator/test/resolve.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import test from 'node:test';
66
import { TestProject } from './testProject';
77
import { parseCodecInitializer, Project, type Schema } from '../src';
88
import { MOCK_NODE_MODULES_DIR } from './externalModules';
9+
import { stripStacktraceOfErrors } from '../src/error';
910

1011
async function testCase(
1112
description: string,
@@ -43,7 +44,7 @@ async function testCase(
4344
}
4445
}
4546

46-
assert.deepEqual(errors, expectedErrors);
47+
assert.deepEqual(stripStacktraceOfErrors(errors), expectedErrors);
4748
assert.deepEqual(actual, expected);
4849
});
4950
}

0 commit comments

Comments
 (0)