New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Supports async response transformers #467
Conversation
|
||
const worker = setupWorker( | ||
rest.get('/image', async (req, res, ctx) => { | ||
return res(ctx.status(201), await jpeg(base64Image)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it extremely readable to await the transformer upon invocation, as opposed to drilling asynchronicity all the way down to the response
composition tree.
import { ResponseTransformer, setupWorker, rest, context, compose } from 'msw' | ||
import base64Image from 'url-loader!../../fixtures/image.jpg' | ||
|
||
async function jpeg(base64: string): Promise<ResponseTransformer> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an example of a custom jpeg
response transformer that appends a given Base64 image to the mocked response. It utilizes newly exported compose
function, as well as the ability to make the transformer async.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we have to await
somewhere (I think in compose
)?
My thoughts:
ResponseTransformer
can now be a promise and is a parameter (defaultTransformers
) for createResponseComposition
. This parameter is used to build the resolvedTransformers
, which is passed to the compose
method. So, when we're in compose
, we still have the promise, and we're not awaiting
its result.
(I could be wrong tho, I didn't test it)
Thanks for the feedback, @timdeschryver.
Not with the current implementation. We allow response transformer to be async, but it must be awaited within the response resolver (which is already supported as async). That way the list of transformers that we supply to async (req, res, ctx) => {
// `res` will get the resolved value of `customTransformer`, not the Promise
return res(await customTransformer())
}
That's a great point. I'll add a test that ensures that async transformer passed to the custom response composition function still executes properly 👍 |
19ece18
to
10a1727
Compare
What do you think about this extension to the msw/src/utils/internal/compose.ts Lines 5 to 17 in d526e8e
I didn't want to make it always return a Promise, which would ruin a synchronous execution. At the same time if any of the functions returns a Promise the
I've also added an integration test for an async response transformer as the value of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, nice @kettanaito!
d526e8e
to
9d73c9d
Compare
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 5ec66d2:
|
4a39def
to
5ec66d2
Compare
Experiencing some TS issues during the compilation of an Angular project as a part of smoke tests. |
@kettanaito this could be due to miss matching typescript versions. |
This is amazing, @timdeschryver! Thank you for figuring that out. I'll review your Angular pull request. |
No problem! It was an error that I've had seen before 😅 |
Changes
compose
function publicly to compose multiple standard response transformers as a part of a custom response transformer.response
calls as potentially async in unit tests.describe
blocks).GitHub
Roadmap
compose
properly (infer function type, set return type)