Skip to content

Commit

Permalink
fix: strip stacktrace off of actual errors in tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
anshchaturvedi committed Aug 7, 2024
1 parent 96a906a commit c11dc0e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
9 changes: 9 additions & 0 deletions packages/openapi-generator/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ export function errorLeft(message: string): E.Either<string, never> {

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]);
}
3 changes: 2 additions & 1 deletion packages/openapi-generator/test/apiSpec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -51,7 +52,7 @@ async function testCase(
}
}

assert.deepEqual(errors, expectedErrors);
assert.deepEqual(stripStacktraceOfErrors(errors), expectedErrors);
assert.deepEqual(actual, expected);
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/openapi-generator/test/externalModule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/openapi-generator/test/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -43,7 +44,7 @@ async function testCase(
}
}

assert.deepEqual(errors, expectedErrors);
assert.deepEqual(stripStacktraceOfErrors(errors), expectedErrors);
assert.deepEqual(actual, expected);
});
}
Expand Down

0 comments on commit c11dc0e

Please sign in to comment.