I'd like to test my koa
API routes using supertest
and check what's in DynamoDB before and after to make sure that the end point did what was intended.
// app related
const pool = require('../../src/common/pool');
const app = require('../../server');
// for testing
const uuid = require('uuid');
const supertest = require('supertest');
// listen on port 40002
const request = supertest.agent(app.listen(4002));
describe('test', () => {
it.only('should', async (done) => {
debugger;
const id = uuid.v4().replace(/-/g, '');
await pool.add(id, 'data', 30);
return request
.get('/api/1')
.expect(204)
// .then(async (res) => {
// .then((res) => {
.end((res) => {
// still returns 'data' instead of 'dataNew' after the route is hit
const record = await pool.get(id);
debugger;
done();
});
});
});
In the code above, I'm creating a record in the db, then I hit the end point, and I tried a then()
and an end()
chained function to check the db once again. The end point will just data
to dataNew
and in the then()
function, it still returns the original data
.
Any ideas on how I can verify the new record in the db ?
References:
- Supertest: Verify database after request - In TLDR at the bottom, the solution was to use
co
. I tried this and had issues probably cause I'm usingawait
instead of generators.