I'm testing a web application that uses cloudant. I set up a test db in the beforeAll() and want to delete it after all tests in the afterAll().
The code in before gets executed and I get the db created, but for some reason the code in the after is not being executed. To be more precise, the call to cloudant api is not executed. In both before and after uses the same API that is handled promise.
In my test.js
const cloudant = require('../storage/cloudant');
const dbname = 'testdb';
const doc = { _id: 'testDocument' };
const dbConfig = { dbname, doc };
beforeAll(() => {
cloudant.createDB(dbname)
.then(res => console.log(res))
.then(() => cloudant.createDocument(dbConfig));
});
afterAll(() => {
console.log('It is called');
cloudant.deleteDatabase(dbname).then((res) => console.log(res)).catch(e => console.log(e));
// test templates functions
test('Read documnet from cloudant', async () => {
const response = await cloudant.readDocument(dbname, doc._id);
expect(response._id).toBe(doc._id);
});
I would expect the db to be deleted after the test execution. However the only thing that seams to be called in the afterALL is the console.log; I get no exception or error for the deleteDatabase call. The deleteDatabase method works. I tested it in a separate js script.
cloudant.deleteDatabase(dbname)
.then((res) => console.log(res))
.catch(e => console.log(e));
returns { ok: true }
The delete method is defined as follow:
async deleteDatabase(dbname) {
return this.cloudantDB.db.destroy(dbname);
}
The test results are as follow:
> jest server/test
[2019-08-08T10:58:55.132] [INFO] App-cloudant - creating new db: testdb
[2019-08-08T10:58:55.138] [INFO] App-cloudant - Reading testDocument
console.log server/storage/cloudant.js:43
testdb
console.log server/storage/cloudant.js:44
testDocument
console.log server/test/test.js:17
{ ok: true }
[2019-08-08T10:58:55.487] [INFO] App-cloudant - Creating document [object Object]
PASS server/test/test.js
✓ adds 1 + 2 to equal 3 (2ms)
✓ Read documnet from cloudant (515ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 2.184s
Ran all test suites matching /server\/test/i.
console.log server/test/test.js:22
It is called
So the beforeAll works, the test executes successfully and the console.log from afterAll is called but not the deleteDatabase function.