I am setting up my test DB in a before() hook, using nested Promises. It runs well, but I don't know how to catch the error at the highest level ( in the before() hook) . Looking in the console.log , when an error is catched in a a nested Promise, it is returned to the previous level as a value... not matched..
group.test.js
before(() => {
return utils.clearCollections()
.then(() => {
return utils.createSuperAdmin()
.then(() => {
return utils.createTestGroups(3)
.then((insertedGroups) => {
groups = insertedGroups;
return utils.addRolesInGroups(groups)
.then((insertedRoles) => {
console.log('DONE ! %j', insertedRoles);
})
.catch((e) => {
console.log('error %j', e);
});
});
});
});
});
test.utility.js
export function addRolesInGroups(groups) {
...
return Role.insertMany([roleA1, roleA2, roleA3, roleB2, roleB3, roleC3, role4 ])
.then((insertedRoles) => {
console.log('Successfully created test roles in groups');
return insertedRoles;
})
.catch((e) => {
console.log('Error inserting many: %j', e);
return e;
});
}
console.log
The error is corrrectly catched in the Promise function addRolesInGroups() BUT it is returned to the calling before() hook .. not catched...
Error inserting many: {"code":11000,"index":3,"errmsg":"E11000
duplicate key error collection: cockpit-api-test.roles index: name_1
dup key: { : \"manager\" }","op": "__v":0,
"_groupId":"5954d45e05206235d308f4d6",
"name":"manager","description":"can R group, can RW
user","_id":"5954d45e05206235d308f4db","rolePermissions"
[],"permissions":[{"resourceName":"group","authorizedActions"
["read"]},{"resourceName":"user","authorizedActions"
["read","write"]}],"users":[]}}
DONE ! {"code":11000,"index":3,"errmsg":"E11000 duplicate key error
collection: cockpit-api-test.roles index: name_1
dup key: { : \"manager\" }","op"
{"__v":0,"_groupId":"5954d45e05206235d308f4d6",
"name":"manager","description":"can R group, can RW
user","_id":"5954d45e05206235d308f4db","rolePermissions":[],
"permissions":[{"resourceName":"group","authorizedActions":["read"]}
{"resourceName":"user","authorizedActions":["read","write"]}]
,"users":[]}}