-
Notifications
You must be signed in to change notification settings - Fork 1k
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
mockRedwoodDirective fails when ValidatorDirective is async #5713
Comments
Thanks @xmonkee, I was able to confirm this one. @dthyresson is the mind behind many of the GraphQL integrations in the framework so I'll tag him here to see what he thinks about your fix. |
@dac09 Thoughts on letting validator directives be async? |
I’m a little spread thin DT - I don’t want to get in the way of you investigating/solving this! |
@dac09 I still planned to review, I just wanted to know if you had any reservations about a validator making some sort of async call (like a db query inside the graphql execution). |
Hi @xmonkee let's add export const mockAsyncRedwoodDirective: DirectiveMocker = async (
directive,
executionMock
) => {
const { directiveArgs = {}, context, ...others } = executionMock
if (context) {
setContext(context || {})
}
return async () => {
if (directive.type === DirectiveType.TRANSFORMER) {
const { mockedResolvedValue } = others as TransformerMock
return await directive.onResolverCalled({
resolvedValue: mockedResolvedValue,
context: globalContext,
...others,
} as DirectiveParams)
} else {
await directive.onResolverCalled({
context: globalContext,
directiveArgs,
...others,
} as DirectiveParams)
}
}
} Would you be interested in contributing a PR for that and the test? Thanks! |
Issue
mockRedwoodDirective
does not await the inner validator function, thus making it hard to test that an async validator functions throws an errorRepro
yarn rw generate directive requireSomething
It will give two errors:
Received function did not throw
and then the actual throw as an errorThis is the same behavior jest shows when trying to
expect(...).toThrow()
on an async function without doingasync expect(..).rejects.toThrow()
Possible fix
Add a
mockAsyncRedwoodDirective
. This is justmockRedwoodDirective
which now returns an async function that awaits the validator and transformer functionsAnd modify the test
The text was updated successfully, but these errors were encountered: