diff --git a/test/integration/live-api.test.ts b/test/integration/live-api.test.ts index 40ff690c..bea43a5f 100644 --- a/test/integration/live-api.test.ts +++ b/test/integration/live-api.test.ts @@ -12,6 +12,7 @@ import debug = require('debug') import { CreateAssemblyOptions, Transloadit, UploadProgress } from '../../src/Transloadit' import { createTestServer, TestServer } from '../testserver' +import { createProxy } from '../util' const log = debug('transloadit:live-api') @@ -54,7 +55,7 @@ function createClient(opts = {}) { ], } - return new Transloadit({ authKey, authSecret, gotRetry, ...opts }) + return createProxy(new Transloadit({ authKey, authSecret, gotRetry, ...opts })) } function createAssembly(client: Transloadit, params: CreateAssemblyOptions) { diff --git a/test/unit/mock-http.test.ts b/test/unit/mock-http.test.ts index b59d6e65..628ded66 100644 --- a/test/unit/mock-http.test.ts +++ b/test/unit/mock-http.test.ts @@ -3,14 +3,16 @@ import { inspect } from 'node:util' import { ApiError, - HTTPError, InconsistentResponseError, TimeoutError, Transloadit, } from '../../src/Transloadit' +import { createProxy } from '../util' const getLocalClient = (opts?: Omit) => - new Transloadit({ authKey: '', authSecret: '', endpoint: 'http://localhost', ...opts }) + createProxy( + new Transloadit({ authKey: '', authSecret: '', endpoint: 'http://localhost', ...opts }) + ) const createAssemblyRegex = /\/assemblies\/[0-9a-f]{32}/ @@ -21,11 +23,7 @@ describe('Mocked API tests', () => { }) it('should time out createAssembly with a custom timeout', async () => { - const client = new Transloadit({ - authKey: '', - authSecret: '', - endpoint: 'http://localhost', - }) + const client = getLocalClient() nock('http://localhost').post(createAssemblyRegex).delay(100).reply(200) @@ -327,4 +325,21 @@ describe('Mocked API tests', () => { await expect(promise).rejects.toThrow(ApiError) scope.done() }) + + it.skip('should not log monster error stack traces in vitest', async () => { + const client = getLocalClient() + + const scope = nock('http://localhost') + .get('/assemblies/invalid') + .query(() => true) + .reply(404, { error: 'SERVER_404', message: 'not found' }) + + try { + await client.getAssembly('invalid') + // NOTE: manually check output from vitest + // Check that it doesn't print a huge blob of JSON + } finally { + scope.done() + } + }) }) diff --git a/test/util.ts b/test/util.ts new file mode 100644 index 00000000..dc17525f --- /dev/null +++ b/test/util.ts @@ -0,0 +1,46 @@ +import { HTTPError, Transloadit } from '../src/Transloadit' + +export const createProxy = (transloaditInstance: Transloadit) => { + return new Proxy(transloaditInstance, { + get(target, propKey) { + // @ts-expect-error I dunno how to type + const origMethod = target[propKey] + if (typeof origMethod === 'function') { + return function (...args: any) { + const result = origMethod.apply(target, args) + + if (!(result && 'then' in result)) { + return result + } + + // @ts-expect-error any + return result.catch((err) => { + if (err instanceof Error && 'cause' in err && err.cause instanceof HTTPError) { + if (err.cause.request) { + Object.defineProperty(err.cause.request, 'toJSON', { + value: () => undefined, + enumerable: false, + }) + Object.defineProperty(err.cause.response, 'toJSON', { + value: () => undefined, + enumerable: false, + }) + Object.defineProperty(err.cause.options, 'toJSON', { + value: () => undefined, + enumerable: false, + }) + Object.defineProperty(err.cause.timings, 'toJSON', { + value: () => undefined, + enumerable: false, + }) + } + } + throw err + }) + } + } + + return origMethod + }, + }) +}