In my mocha tests, I run some functions to setup the db before running the tests. the first function creates all the database tables and looks like this:-
const createDatabase = () => {
return new Promise(async function (resolve, reject) {
await db.serialize(async () => {
db.run('DROP TABLE IF EXISTS Globals');
db.run('DROP TABLE IF EXISTS checks');
db.run('DROP TABLE IF EXISTS SalesItems');
...
...
...
db.run('CREATE TABLE Globals (Id INTEGER NOT NULL PRIMARY KEY, Key TEXT NOT NULL, ValueInt INTEGER)');
...
...
...
})
resolve();
});
}
when the promise is resolved, I then run a function that populates the tables from csv files using fs and db.run. e.g.
fs.createReadStream('./testdata/globals.csv')
.pipe(parse({ from: 2 }))
.on('data', async function (csvrow) {
db.run('INSERT INTO Globals (Key, ValueInt) VALUES($Key, $ValueInt)', {
$Key: csvrow[0],
$ValueInt: csvrow[1]
}, err => {
if (err) {
console.log('Error seeding Globals table');
console.log(err);
return;
}
});
});
for some reason, when these two functions are run, I get an error "Error: SQLITE_ERROR: no such table: Globals" even though (as far as I can tell) the table has been created.
My test suite looks like this:-
describe('test', function () {
beforeEach(async function () {
return new Promise(async function (resolve, reject) {
createDatabase().then(() => {
seedTestDatabase();
resolve();
})
});
});
require('./suites/screensTests.js');
require('./suites/salesItemsTests.js');
require('./suites/checksTests.js');
require('./suites/checkItemsTests.js');
require('./suites/ordersTests.js');
});
So the seedTestDatabase() should not run until the createDatabase() has completed and the tests shouldn't run until the seedTestDatabase() has completed but this isn't happening. They all seem to be running in parallel causing problems.