Skip to content

Commit

Permalink
chore: add ".toEqualBytes()" assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed May 27, 2024
1 parent 59d438e commit 96b09c8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 26 deletions.
7 changes: 2 additions & 5 deletions src/fromTraffic/utils/harUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { encodeBase64String } from './encodeBase64String'
import { isEqualBytes } from './isEqualBytes'
import { toHeaders, toResponse, toResponseBody } from './harUtils'

describe(toHeaders, () => {
Expand Down Expand Up @@ -71,9 +69,8 @@ describe(toResponseBody, () => {
encoding: 'base64',
})

expect(responseBody).toBeDefined()
expect(isEqualBytes(bodyBytes, responseBody as Uint8Array)).toBe(true)
})
expect(responseBody).toEqualBytes(bodyBytes)
})

it.todo('handles a compressed response body')
})
Expand Down
13 changes: 0 additions & 13 deletions src/fromTraffic/utils/isEqualBytes.ts

This file was deleted.

11 changes: 4 additions & 7 deletions test/traffic/fromTraffic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
*/
import Har from 'har-format'
import { fromTraffic } from '../../src/fromTraffic/fromTraffic'
import { decodeBase64String } from '../../src/fromTraffic/utils/decodeBase64String'
import { readArchive } from './utils'
import { toResponse } from '../../src/fromTraffic/utils/harUtils'
import { inspectHandlers } from '../support/inspectHandler'
import { encodeBase64String } from '../../src/fromTraffic/utils/encodeBase64String'
import { isEqualBytes } from '../../src/fromTraffic/utils/isEqualBytes'

describe('fromTraffic', () => {
it('throws an exception given no HAR object', () => {
Expand Down Expand Up @@ -120,12 +117,12 @@ describe('toResponseBody', () => {

it('returns a decoded text body given a base64-encoded response body', async () => {
const exepctedBody = 'hello world'
const response = toResponse(createResponse(btoa(exepctedBody), { encoding: 'base64' }))
const response = toResponse(
createResponse(btoa(exepctedBody), { encoding: 'base64' }),
)
const responseText = await response.text()

expect(
responseText
).toEqual(exepctedBody)
expect(responseText).toEqual(exepctedBody)
})

it('returns a plain text response body as-is', async () => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"types": ["vitest/globals"],
"lib": ["dom", "DOM.Iterable"]
},
"include": ["test", "src/**/*.test.ts"]
"include": ["./vitest.d.ts", "./vitest.setup.ts", "test", "src/**/*.test.ts"]
}
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
globals: true,
setupFiles: ['./vitest.setup.ts'],
environment: 'jsdom',
environmentOptions: {
jsdom: {
Expand Down
39 changes: 39 additions & 0 deletions vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { invariant } from 'outvariant'

expect.extend({
toEqualBytes(expected: unknown, actual: unknown) {
invariant(isUint8Array(expected), '')
invariant(isUint8Array(actual), '')

if (expected.length !== actual.length) {
return {
pass: false,
message: () =>
`Expected Uint8Array of length (${expected.length}) but got (${actual.length})`,
actual: actual.length,
expected: expected.length,
}
}

for (let i = 0; i < expected.length; i++) {
if (actual[i] !== expected[i]) {
return {
pass: false,
message: () =>
`Expected Uint8Array to be equal but found a difference at index ${i}`,
actual: actual[i],
expected: expected[i],
}
}
}

return {
pass: true,
message: () => '...',
}
},
})

function isUint8Array(value: unknown): value is Uint8Array {
return value?.constructor.name === 'Uint8Array'
}

0 comments on commit 96b09c8

Please sign in to comment.