Skip to content

Commit

Permalink
test(axios-upload): wrap request promise in expect
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Oct 4, 2024
1 parent 4471412 commit 3fdf314
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions test/node/third-party/axios-upload.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ it('responds with a mocked response to an upload request', async () => {
const file = new Blob(['Hello', 'world'], { type: 'text/plain' })
formData.set('file', file, 'doc.txt')

const response = await request.post('/upload', formData)

expect(response.data).toEqual({
message: 'Successfully uploaded "doc.txt"!',
content: 'Helloworld',
await expect(request.post('/upload', formData)).resolves.toMatchObject({

Check failure on line 48 in test/node/third-party/axios-upload.node.test.ts

View workflow job for this annotation

GitHub Actions / build

test/node/third-party/axios-upload.node.test.ts > responds with a mocked response to an upload request

AssertionError: promise rejected "AxiosError{ …(7) }" instead of resolving ❯ test/node/third-party/axios-upload.node.test.ts:48:49 Caused by: AxiosError: Request failed with status code 500 ❯ settle node_modules/.pnpm/[email protected]/node_modules/axios/lib/core/settle.js:19:12 ❯ IncomingMessage.handleStreamEnd node_modules/.pnpm/[email protected]/node_modules/axios/lib/adapters/http.js:589:11 ❯ IncomingMessage.emit node:events:531:35 ❯ Axios.request node_modules/.pnpm/[email protected]/node_modules/axios/lib/core/Axios.js:45:41 ❯ test/node/third-party/axios-upload.node.test.ts:48:3 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_BAD_RESPONSE', config: { transitional: { silentJSONParsing: true, forcedJSONParsing: true, clarifyTimeoutError: false }, adapter: [ 'xhr', 'http' ], transformRequest: [ 'Function<transformRequest>' ], transformResponse: [ 'Function<transformResponse>' ], timeout: +0, xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, maxBodyLength: -1, env: { FormData: 'Function<FormData>', Blob: 'Function<Blob>' }, validateStatus: 'Function<validateStatus>', headers: { Accept: 'application/json, text/plain, */*', 'Content-Type': 'multipart/form-data; boundary=axios-1.6.8-boundary-h3P-FZydipcXqh8gR-qoL8RGs', 'User-Agent': 'axios/1.6.8', 'Content-Length': '209', 'Accept-Encoding': 'gzip, compress, deflate, br', constructor: 'Function<AxiosHeaders>', set: 'Function<set>', get: 'Function<get>', has: 'Function<has>', delete: 'Function<delete>', clear: 'Function<clear>', normalize: 'Function<normalize>', concat: 'Function<concat>', toJSON: 'Function<toJSON>', toString: 'Function<toString>', getContentType: 'Function<value>', setContentType: 'Function<value>', hasContentType: 'Function<value>', getContentLength: 'Function<value>', setContentLength: 'Function<value>', hasContentLength: 'Function<value>', getAccept: 'Function<value>', setAccept: 'Function<value>', hasAccept: 'Function<value>', getAcceptEncoding: 'Function<value>', setAcceptEncoding: 'Function<value>', hasAcceptEncoding: 'Function<value>', getUserAgent: 'Function<value>', setUserAgent: 'Function<value>', hasUserAgent: 'Function<value>', getAuthorization: 'Function<value>', setAuthorization: 'Function<value>', hasAuthorization: 'Function<value>' }, baseURL: 'https://example.com', onUploadProgress: 'Function<spy>', method: 'post', url: '/upload', data: { constructor: 'Function<FormData>', append: 'Function<append>', delete: 'Function<delete>', get: 'Function<get>', getAll: 'Function<getAll>', has: 'Function<has>', set: 'Function<set>', keys: 'Function<keys>', values: 'Function<values>', entries: 'Function<entries>', forEach: 'Function<forEach>' } }, request: { _events: { abort: 'Function<anonymous>', aborted: 'Function<anonymous>', connect: 'Function<anonymous>', error: 'Function<anonymous>', socket: 'Function<anonymous>', timeout: 'Function<anonymous>', finish: 'Function<requestOnFinish>' }, _eventsCount: 7, _maxListeners: undefined, outputData: [], outputSize: +0, writable: true, destroyed: true, _last: true, chunkedEncoding: false, shouldKeepAlive: false, maxRequestsOnConnectionReached: false, _defaultKeepAlive: true, useChunkedEncodingByDefault: true, sendDate: false, _removedConnection: false, _removedContLen: false, _removedTE: false, strictContentLength: false, _contentLength: '209', _hasBody: true, _trailer: '', finished: true, _headerSent: true, _closed: true, socket: { connecting: false, _hadError: false, _parent: null, _host: null, _closeAfterHandlingError: false, _events: { close: [ 'Function<onClose>', 'Function<socketCloseListener>' ], error: [ 'Function<socketErrorListener>', 'Function<bound onceWrapper>' ], prefinish: undefined, finish: undefined, drain: 'Function<ondrain>', data: 'Function<socketOnData>', end: [ 'Function<onReadableStreamEnd>', 'Function<socketOnEnd>' ], readable: undefined, free: 'Function<onFree>', timeout: 'Function<onTimeout>', agentRemove: 'Function<onRemove>', connect: 'Function<bound onceWrapper>' }, _readableState: { highWaterMark: 16384, buffer: [], bufferIndex: +0, length: +0
data: {
message: 'Successfully uploaded "doc.txt"!',
content: 'Helloworld',
},
})

expect(onUploadProgress.mock.calls.length).toBeGreaterThan(0)
expect(onUploadProgress).toHaveBeenNthCalledWith(
1,
Expand Down

0 comments on commit 3fdf314

Please sign in to comment.