Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion test/integration/live-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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) {
Expand Down
29 changes: 22 additions & 7 deletions test/unit/mock-http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Transloadit.Options, 'authKey' | 'authSecret' | 'endpoint'>) =>
new Transloadit({ authKey: '', authSecret: '', endpoint: 'http://localhost', ...opts })
createProxy(
new Transloadit({ authKey: '', authSecret: '', endpoint: 'http://localhost', ...opts })
)

const createAssemblyRegex = /\/assemblies\/[0-9a-f]{32}/

Expand All @@ -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)

Expand Down Expand Up @@ -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()
}
})
})
46 changes: 46 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -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
},
})
}