-
Notifications
You must be signed in to change notification settings - Fork 993
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
RFC - Testing API Side Functions and Webhooks with Scenarios #2765
Comments
What about a directory structure of
This way there is no nesting of fixtures in tests -- and fixturs comes pre-setup with the two helpers and ready for custom fixtures. |
I don't believe the initial convention design of Also should consider implication per this Issue if we ever want to support nesting (either just as a convention or also "nested" end-point structure) #849 |
@dac09 and I had thought about nesting and that's our preferred approach -- to have it look much like services and web/components etc ... and we looked at that very same issue #849 as well. But it wasn't as easy as we all thought. Still exploring options. |
Decision from core team meeting ✅
Your functions will be reachable on Outstanding things we need to verify:
|
Netlify already supports This is from their docs
See the second example in the list: |
Thanks @Tobbe - need to improve my reading skillz. I think once we verify how vercel does it, we can decide what to do. Our dev/api server needs to change, but thats not a big deal. Index seems to me to be the most likely to be compatible, but I couldn't find the equivalent section of the docs for that on vercel. |
Something to consider is if the dev server can serve up the function in the same way as an endpoint. |
Not to overdo the research ideas, but it possible we should look into Azure functions as well. Also, I'm assuming Netlify === AWS Lambdas (direct deploy), but maybe not? |
Vercel does not support the nested functions behaviour. Functions in folders are simply ignored. In order to maintain compatibility (and to make it easy to reason about) - the best course of action after discussion with @peterp is to do something like this: Input file tree
Gets transformed to Dist output tree
|
@dac09 Another scenario to add to your test cases for Netlify are the On-Demand Builders: https://docs.netlify.com/configure-builds/on-demand-builders/ const { builder } = require("@netlify/functions")
async function myfunction(event, context) {
// logic to generate the required content
}
exports.handler = builder(myfunction); Since it wraps the handler. |
Some more important info from investigation, although Netlify supports the folders as per their docs, what they do not let you do is group functions into a folder. So in this example:
☑️ myapp.com/api/checking runs checking.js |
I actually think they do. I wrote this in a previous issue (#849)
With that I can access: So you do have to map it all out in vercel.json So, yeah, it is possible. Just putting it out there. Do what you want with the info :) |
Yeah ofcourse, with redirects everything is possible ;) |
Missing from the RedwoodJS testing suite is a way to test api side functions and webhooks with the same scenario-based approach used with api side services. The goal is to follow the same patterns in api/web testing such that testing functions/webhooks is familiar and looks the same.
Some considerations and decisions need to be made to support function and webhook testing.
I have a proof of concept working in https://github.com/redwoodjs/redwood-challenges/tree/main/api/src/functions/__tests__
Tasks
Proof of Concept Directory Structure
Updated API Jest Config
Add filter to ignore
scenarios
intestPathIgnorePatterns
.buildEvent
A function handler needs an event and context, typically lambda (APIGatewayProxyEvent) and some context.
buildEvent takes a payload, and constructs the necessary param to be an expected APIGatewayProxyEvent - include headers, queryParams, httpMethods, etc.
buildContext
At moment, is a stub, but here can sent identity and clientContext info. See: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html
Example Function Test
The
entryHtml.test.ts
uses scenario dataentryHtml.scenarios.ts
to create entries one of which isfind-me-by-id-one
.Here
buildEvent
passes in a query string param corresponding to the scenariofind-me-by-id-one
.The function fetches the entry from a service and returns a body -- which is checked from an expecrted result.
Example Webhook Test
The ``pullRequestWebhook.test.ts
shows how
buildEvent` can be used to mock the event with a signature.This also uses
nock
to intercept a third-party api (a got request) that the webhook also calls.The
scenario
sets up necessary database records for the function to persists the info received from the webhook.The
encodedGitHubFileContent
is fixture data that is needed to verify the mock response from thenock
intercept.The text was updated successfully, but these errors were encountered: