Serverless 1+ convention.
- Integration Tests
Integrating with serverless is a challenge, and it does not expose a documented
programmatic api. Thus, we use the CLI via a module like shelljs.
✓ Ensure serverless.env.yml populates aws credentials
✓ Extract the deployed endpoint by parsing sls info --verbose
export function getServerlessInfo() {
const keyword = 'ServiceEndpoint: ';
const endpoint = sh.exec('sls info --verbose', { silent: true }).stdout
.split('\n')
.find(line => line.startsWith(keyword))
.substr(keyword.length)
.trim();
return {
endpoint,
};
}✓ Use the endpoint to hit it like a real client, with fetch or axios
import 'isomorphic-fetch';
import { getServerlessInfo } from './utils';
describe('/myFancyFunction', () => {
let endpoint
beforeAll(() => {
endpoint = getServerlessInfo().endpoint;
});
});As above, we're merely making sure a the endpoint doesn't error on us.
This is typically all a smoke test needs to do.
it('fetches a record', async () => {
const response = await fetch(`${endpoint}/test/myFancyFunction`, {
method: 'GET',
headers: { 'accept': 'application/vnd.api+json', 'content-type': 'application/vnd.api+json', },
body: JSON.stringify({
data: {
type: 'test',
attributes: { test: true },
},
}),
});
const body = await res.json();
expect(response.status).toBe(200);
expect(body.data).toBeTruthy();
})