0

I have simple Jest case where I need to create the table at the beginning, populate it and then run some tests. After all tests are finished, I need to delete the table in DynamoDB.

I tried with beforeAll, creating the table, then testing, and then with afterAll deleting the table. Nevertheless, it runs non-deterministically, so the population tries to run before creating the table, or the table tries to be deleted before ever creating it.

My code

beforeAll(() => {
    await createTable();
    populateTable();
    //setupLDAPConnection();
});

afterAll(() => {
    deleteTable();
});

describe('The program gets list of AD groups', function () {

    it('should get a list of user groups in the Active Directory', async () => {
        //const result = await userSyncFunction.handler(undefined);
        //expect(result).to.be.an('object');
        //console.log('2');
    });
});


populateTable = () => {
    agents.forEach((agent) => {
        docClient.put(agent, (err, data) => {
            if (err) {
                console.log("Error", err);
              } else {
                console.log("Agent inserted", data);
              }
        });
    });
};

createTable = async () => {
    dynamoDb.createTable(agent_table, (err, data) => {
    if (err) {
        console.log("Error", err);
      } else {
        console.log("Table created");
      }
   });
};

deleteTable = () => {
    dynamoDb.deleteTable( { TableName: 'AGENT_LIST' }, function(err, data) {
        if (err)
            console.log(err, err.stack);
        else {
            console.log('Table deleted');
        }
    });
};

Any ideas?

Thank you.

Camilo
  • 439
  • 5
  • 13

1 Answers1

1

Function inside beforeAll should be async, not createTable, since you want to await there. So, it should look like this:

beforeAll(async () => {
    await createTable();
    populateTable();
    //setupLDAPConnection();
});
Metalmi
  • 561
  • 4
  • 8