-
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
chore(api-server): improve tests #9325
Conversation
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 left a couple of really very minor comments that shouldn't hold this up. I hope they don't appear overly nit-picky as that was certainly not my intension.
This work to add these tests to understand better the effects of incoming changes is really great!
Edit: The only other thing I was going to mention was the location of the dist.test.ts
file. At first it struck me as a little odd that it was at the root level rather than in src/
somewhere but I don't really have a strong opinion on where it should live so wherever you put it is good with me.
Co-authored-by: Josh GM Walker <56300765+Josh-Walker-GM@users.noreply.github.com>
Forgot to address your comment about the |
In #9325, I added a few fixtures to the `@redwoodjs/api-server` package that include dist files. `yarn build:clean` deletes all dist directories in all packages (the glob is `packages/**/dist`), so `yarn rwfw project:sync` ended up deleting twenty or so files. This PR makes `yarn build:clean` a little smarter. I couldn't see a way to do it via the `rimraf` CLI, so I just made a script. This PR also increases the timeout in the server tests which were a little flakey.
I added a new job to CI in #9325 that tested all the different serve entry points. It was a bit flakey so I increased its timeouts in #9336. Unfortunately it's still a bit flakey. Right now when these tests fail they're creating confusion, and most of our changes lately don't affect these tests. They're still valuable to me locally, and at least right now I'm the only one making changes to api-server. So I'll just leave the tasks directory as is and remove them from CI till I do a bit more testing on their flakiness.
This PR improves the tests for the `@redwoodjs/api-server` package. In doing this, I deliberately didn't edit any sources files (save one edit to export an object for testing). My goal was basically to test everything I could. I'm going to be deduplicating code across the framework (a lot of the code in the api-server package was duplicated to avoid breaking anything while iterating on experimental features), and since users can invoke api-server code in a few different ways (`npx @redwoodjs/api-server`, `yarn rw serve`, and importing handlers from `@redwoodjs/api-server`), I don't want to accidentally break anything anywhere. Improving test coverage also revealed some things that need to be fixed. A few high-level notes. Overall, I reduced our reliance on mocking (in many tests, we weren't using fastify at all). Most tests covered `configureFastify` and while a few checked to see that routes were registered correctly, none of them actually started the api-server, or used any of fastify's testing facilities like `inject` to check that the right file was sent back, etc. (Now they do!) Lastly, while most of the tests I added assume fastify, the `dist.test.ts` isn't and is more like a black-box test. Re the "revealed some things that need to be fixed", in no particular order: - `apiUrl` and `apiHost` can be a bit confusing. Even the previous `withApiProxy.test.ts` got them wrong While this test checked if the options were passed correctly, it mixed up the `apiUrl` for the `apiHost`. `apiHost` is the upstream (and should be a URL). https://github.com/redwoodjs/redwood/blob/daaa1998837bdb6eaa42d9160292e781fadb3dc8/packages/api-server/src/__tests__/withApiProxy.test.ts#L51-L54 @Josh-Walker-GM alerted me to this some time ago, but I didn't understand the nuance. Now that I do, these are just poorly named and we should try to fix them. - Everything in web dist is served https://github.com/redwoodjs/redwood/blob/daaa1998837bdb6eaa42d9160292e781fadb3dc8/packages/api-server/src/plugins/withWebServer.ts#L50-L53 We may want to revisit this. I'm sure most of it needs to be served, but it seems like a poor default when it comes to security. Also, it just results in routes being served that don't work. E.g., all prerendered routes are available at their `.html` suffix, but they error out as soon as they're rendered. - The lambda adapter seems a little too simple https://github.com/redwoodjs/redwood/blob/daaa1998837bdb6eaa42d9160292e781fadb3dc8/packages/api-server/src/requestHandlers/awsLambdaFastify.ts#L11-L27 There's a [core fastify plugin for lambdas](https://github.com/fastify/aws-lambda-fastify), and it's much more complicated than our implementation. Should we just use that? What's the difference between our implementation and theirs? - `setLambdaFunctions` should ignore functions that don't export a handler. It probably doesn't do any harm but it registers them as `undefined`: https://github.com/redwoodjs/redwood/blob/daaa1998837bdb6eaa42d9160292e781fadb3dc8/packages/api-server/src/plugins/lambdaLoader.ts#L32-L40 - The lambda adapter supports callback syntax https://github.com/redwoodjs/redwood/blob/daaa1998837bdb6eaa42d9160292e781fadb3dc8/packages/api-server/src/requestHandlers/awsLambdaFastify.ts#L71-L89 I don't think we need to support this anymore. - The CLI handlers don't have help and don't error out on unknown args It's not uncommon to use this CLI standalone so we should polish it. https://github.com/redwoodjs/redwood/blob/daaa1998837bdb6eaa42d9160292e781fadb3dc8/packages/api-server/src/index.ts#L26-L28 ``` $ yarn rw-server --help Options: --help Show help [boolean] --version Show version number [boolean] ``` ``` $ yarn rw-server --foo --bar --baz Starting API and Web Servers... ``` - You can't import `@redwoodjs/api-server` outside a Redwood project This one isn't relevant to users, but it made testing the built package more complicated than it needed to be. The issue is that `getConfig` is called as soon as the file is imported. And if you're not in a redwood project at that point, you're out of luck: https://github.com/redwoodjs/redwood/blob/daaa1998837bdb6eaa42d9160292e781fadb3dc8/packages/api-server/src/cliHandlers.ts#L24 --------- Co-authored-by: Josh GM Walker <56300765+Josh-Walker-GM@users.noreply.github.com>
In #9325, I added a few fixtures to the `@redwoodjs/api-server` package that include dist files. `yarn build:clean` deletes all dist directories in all packages (the glob is `packages/**/dist`), so `yarn rwfw project:sync` ended up deleting twenty or so files. This PR makes `yarn build:clean` a little smarter. I couldn't see a way to do it via the `rimraf` CLI, so I just made a script. This PR also increases the timeout in the server tests which were a little flakey.
I added a new job to CI in #9325 that tested all the different serve entry points. It was a bit flakey so I increased its timeouts in #9336. Unfortunately it's still a bit flakey. Right now when these tests fail they're creating confusion, and most of our changes lately don't affect these tests. They're still valuable to me locally, and at least right now I'm the only one making changes to api-server. So I'll just leave the tasks directory as is and remove them from CI till I do a bit more testing on their flakiness.
This PR improves the tests for the
@redwoodjs/api-server
package. In doing this, I deliberately didn't edit any sources files (save one edit to export an object for testing).My goal was basically to test everything I could. I'm going to be deduplicating code across the framework (a lot of the code in the api-server package was duplicated to avoid breaking anything while iterating on experimental features), and since users can invoke api-server code in a few different ways (
npx @redwoodjs/api-server
,yarn rw serve
, and importing handlers from@redwoodjs/api-server
), I don't want to accidentally break anything anywhere. Improving test coverage also revealed some things that need to be fixed.A few high-level notes. Overall, I reduced our reliance on mocking (in many tests, we weren't using fastify at all). Most tests covered
configureFastify
and while a few checked to see that routes were registered correctly, none of them actually started the api-server, or used any of fastify's testing facilities likeinject
to check that the right file was sent back, etc. (Now they do!) Lastly, while most of the tests I added assume fastify, thedist.test.ts
isn't and is more like a black-box test.Re the "revealed some things that need to be fixed", in no particular order:
apiUrl
andapiHost
can be a bit confusing. Even the previouswithApiProxy.test.ts
got them wrongWhile this test checked if the options were passed correctly, it mixed up the
apiUrl
for theapiHost
.apiHost
is the upstream (and should be a URL).redwood/packages/api-server/src/__tests__/withApiProxy.test.ts
Lines 51 to 54 in daaa199
@Josh-Walker-GM alerted me to this some time ago, but I didn't understand the nuance. Now that I do, these are just poorly named and we should try to fix them.
Everything in web dist is served
redwood/packages/api-server/src/plugins/withWebServer.ts
Lines 50 to 53 in daaa199
We may want to revisit this. I'm sure most of it needs to be served, but it seems like a poor default when it comes to security. Also, it just results in routes being served that don't work. E.g., all prerendered routes are available at their
.html
suffix, but they error out as soon as they're rendered.The lambda adapter seems a little too simple
redwood/packages/api-server/src/requestHandlers/awsLambdaFastify.ts
Lines 11 to 27 in daaa199
There's a core fastify plugin for lambdas, and it's much more complicated than our implementation. Should we just use that? What's the difference between our implementation and theirs?
setLambdaFunctions
should ignore functions that don't export a handler.It probably doesn't do any harm but it registers them as
undefined
:redwood/packages/api-server/src/plugins/lambdaLoader.ts
Lines 32 to 40 in daaa199
The lambda adapter supports callback syntax
redwood/packages/api-server/src/requestHandlers/awsLambdaFastify.ts
Lines 71 to 89 in daaa199
I don't think we need to support this anymore.
The CLI handlers don't have help and don't error out on unknown args
It's not uncommon to use this CLI standalone so we should polish it.
redwood/packages/api-server/src/index.ts
Lines 26 to 28 in daaa199
You can't import
@redwoodjs/api-server
outside a Redwood projectThis one isn't relevant to users, but it made testing the built package more complicated than it needed to be. The issue is that
getConfig
is called as soon as the file is imported. And if you're not in a redwood project at that point, you're out of luck:redwood/packages/api-server/src/cliHandlers.ts
Line 24 in daaa199