From 29b5581876764893f88619886493405d59e6586f Mon Sep 17 00:00:00 2001 From: flifloo Date: Tue, 25 Aug 2020 17:09:50 +0200 Subject: [PATCH] Fix async assertion --- package-lock.json | 9 ++++++++ package.json | 1 + test/database.js | 55 ++++++++++++++--------------------------------- 3 files changed, 26 insertions(+), 39 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7041147..9d1d0c7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -363,6 +363,15 @@ "type-detect": "^4.0.5" } }, + "chai-as-promised": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/chai-as-promised/-/chai-as-promised-7.1.1.tgz", + "integrity": "sha512-azL6xMoi+uxu6z4rhWQ1jbdUhOMhis2PvscD/xjLqNMkv3BPPp2JyyuTHOrf9BOosGpNQ11v6BKv/g57RXbiaA==", + "dev": true, + "requires": { + "check-error": "^1.0.2" + } + }, "chalk": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", diff --git a/package.json b/package.json index 5ea51a2..879954e 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ }, "devDependencies": { "chai": "^4.2.0", + "chai-as-promised": "^7.1.1", "mocha": "^8.1.1", "sqlite3": "^5.0.0", "supertest": "^4.0.2" diff --git a/test/database.js b/test/database.js index 537b5ba..c64094f 100644 --- a/test/database.js +++ b/test/database.js @@ -1,4 +1,6 @@ -let expect = require("chai").expect; +let chai = require("chai"); +chai.use(require("chai-as-promised")); +let expect = chai.expect; let wipeDatabase = require("./utils/wipeDatabase"); @@ -35,12 +37,7 @@ describe("Database Department tests", () => { }); it("Department validation", async () => { await models.Department.create({name: "TestDepartment"}); - try { - await models.Department.create({name: "TestDepartment"}); - } catch (e) { - if (!(e instanceof models.Sequelize.ValidationError)) - throw e; - } + return expect(models.Department.create({name: "TestDepartment"})).to.be.rejectedWith(models.Sequelize.ValidationError); }); }); @@ -63,12 +60,7 @@ describe("Database Sandwich tests", () => { }); it("Sandwich validation", async () => { await models.Sandwich.create({name: "TestSandwich", price: 1.5}); - try { - await models.Sandwich.create({name: "TestSandwich", price: 1.5}); - } catch (e) { - if (!(e instanceof models.Sequelize.ValidationError)) - throw e; - } + return expect(models.Sandwich.create({name: "TestSandwich", price: 1.5})).to.be.rejectedWith(models.Sequelize.ValidationError); }); }); @@ -113,47 +105,32 @@ describe("Database User test", () => { lastName: "Test", passwordHash: "test"}); - //Username - try { - await models.User.create({ + return Promise.all([ + //Username + expect(models.User.create({ username: "test", email: "test2@test.fr", firstName: "Test2", lastName: "Test2", passwordHash: "test" - }) - } catch (e) { - if (!(e instanceof models.Sequelize.ValidationError)) - throw e; - } - - //Email - try { - await models.User.create({ + })).to.be.rejectedWith(models.Sequelize.ValidationError), + //Email + expect(models.User.create({ username: "test2", email: "test@test.fr", firstName: "Test2", lastName: "Test2", passwordHash: "test" - }) - } catch (e) { - if (!(e instanceof models.Sequelize.ValidationError)) - throw e; - } - - //First & last name - try { - await models.User.create({ + })).to.be.rejectedWith(models.Sequelize.ValidationError), + //First & last name + expect(models.User.create({ username: "test2", email: "test2@test.fr", firstName: "Test", lastName: "Test", passwordHash: "test" - }) - } catch (e) { - if (!(e instanceof models.Sequelize.ValidationError)) - throw e; - } + })).to.be.rejectedWith(models.Sequelize.ValidationError), + ]) }); it("User associations", async () => { let testUser = await models.User.create({