Replies: 1 comment
-
const ctx = createMockContext({
body: 'foo'
}) Except no, that isn't quite right either, because import { describe, it } from 'jsr:@std/testing/bdd'
import { expect } from 'jsr:@std/expect'
import stringToReadableStream from './string-to-readable-stream.ts'
describe('stringToReadableStream', () => {
const greeting = 'Hello, world!'
const readableStreamToString = async (stream: ReadableStream<any>): Promise<string> => {
const reader = stream.getReader()
const decoder = new TextDecoder()
let result = ''
while (true) {
const { done, value } = await reader.read()
if (done) break
result += decoder.decode(value, { stream: true })
}
result += decoder.decode()
return result
}
it('returns a readable stream', async () => {
const stream = stringToReadableStream(greeting)
const back = await readableStreamToString(stream)
expect(typeof stream).not.toBe('string')
expect(back).toEqual(greeting)
})
}) Then... const stringToReadableStream = (orig: string): ReadableStream<any> => {
return new ReadableStream({
start (controller) {
controller.enqueue(new TextEncoder().encode(orig))
controller.close()
}
})
}
export default stringToReadableStream At which point you can bring it all together: import stringToReadableStream from './utils/string-to-readable-stream.ts'
const ctx = createMockContext({
body: stringToReadableStream('foo')
}) And that works! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to write tests for a route I've created. However I cannot for the life of me figure out how to set the
request.body
oncreateMockContext
. Therequest.body
is readonly so clearly it's not intended to be mutated like so:I've tried so search the codebase but can't see any examples or interfaces for creating a mocked JSON request context.
Beta Was this translation helpful? Give feedback.
All reactions