I wrote a serverless CRUD service with API-Gateway + Lambda + DynamoDB, and I wanted to have local test before I push new changes to AWS as live service.
I have modularized as much as possible for utility/helper functions so that I can do unit tests on them;
But I still have not figured out a way to do functional test for my service logic (I have installed serverless-offline and serverless-dynamodb-local in my repo and configuration), which is an HTTP POST comes in, and then the data is stored into DynamoDB.
Can someone give me a hint how I should start using serverless-offline and serverless-dynamodb-local to achieve my goal?
hey @syang for the time being you could check out our test setup with something like this:
there might be better/easier ways to get to the instance, but I haven't had time to look into it more. I'm open for any improvements regarding that topic.
hey @syang for the time being you could check out our test setup with something like this:
there might be better/easier ways to get to the instance, but I haven't had time to look into it more. I'm open for any improvements regarding that topic.
@dnalborczyk The Setup TearDown helper functions are really neat and I liked it -- thanks a lot.
If I understand it correctly, my test ought to import and call these functions before and after one of my tests -- is there test examples using them?
does these setup / teardown pair help isolate the local environments? i.e, if my test framework run tests in parallel, then there may be chances that multiple setup / teardown run on the fly simutaneously. What would be the consequence for that scenario?
in general this area should be really something to be improved upon. the other issue you probably have to look into is how to deal with serverless-dynamodb-local and other plugins you might use for local development. in that case it might be easier to start a child instance with serverless and the plugins (e.g. with execa or node's own child_process).
- If I understand it correctly, my test ought to import and call these functions before and after one of my tests -- is there test examples using them?
yeah, the integration tests use it, e.g.: https://github.com/dherault/serverless-offline/tree/master/tests/integration/handler
- does these setup / teardown pair help isolate the local environments? i.e, if my test framework run tests in parallel, then there may be chances that multiple setup / teardown run on the fly simutaneously. What would be the consequence for that scenario?
no, you would have to take care of it on your own. the tests for this plugin run with jest in-band, meaning they run serially, as opposed to in-parallel. the reason is that all tests use the standard http port (3000). this hopefully will change in the future and we could use multiple ports for parallel mode. keep in mind that the current situation feels somewhat of a quick hack to me, but I haven't found time regarding the test setup, as there are other more important issues.
to change this for your use case you can apply different ports in the yml file or from the cli, the same way as you would use this plugin for development.
If I find some time in the next days I'll try to add an example to the repo. maybe rather with execa instead of running a script mentioned above. I might change the current test setup as well. stay tuned.
@syang alrighty, I rewrote the test helper to use a child process with execa which I think is a little cleaner than the previous attempt: https://github.com/dherault/serverless-offline/blob/master/tests/integration/_testHelpers/setupTeardown.js
keep in mind that the tests are still using the same port, so they still can't run in parallel [just yet].
@syang alrighty, I rewrote the test helper to use a child process with
execawhich I think is a little cleaner than the previous attempt: https://github.com/dherault/serverless-offline/blob/master/tests/integration/_testHelpers/setupTeardown.jskeep in mind that the tests are still using the same port, so they still can't run in parallel [just yet].
@dnalborczyk I may get back to this integration testing next week, and whatever your cutting-edge suggestion example code is ready, I will be your first tester / verifier
@syang awesome! let me know if you have any questions!
@dnalborczyk I am getting back to this and looks like
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'
...
servicePath: resolve(__dirname),
in https://github.com/dherault/serverless-offline/blob/master/tests/integration/handler/handlerPayload.test.js assumed same package hierarchy.
Can you have an external serverless app testing example that just installed the serverless-offline module?
@syang I'll try to put an example into the examples folder: https://github.com/dherault/serverless-offline/tree/master/examples (probably under /testing)