This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| afterAll(() => { | |
| expressConnection.close(); | |
| }) | |
| afterEach(() => { | |
| sinonSandbox.restore(); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Note how the API gets the Express object with the connection as param | |
| //and focus on the logical defintion | |
| const initializeAPI = (expressApp) => { | |
| //A typical Express setup | |
| const router = express.Router(); | |
| expressApp.use(bodyParser.json());//other middlewares come here | |
| //add new order | |
| router.post('/', async (req, res, next) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //open API connection | |
| expressApp = express(); | |
| expressConnection = expressApp.listen(() => { //no port specified | |
| apiUnderTest(expressApp); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Instantiate DB | |
| await dockerCompose.upAll({ | |
| cwd: path.join(__dirname), | |
| log: true | |
| }); | |
| //wait for db readiness | |
| await waitPort({ | |
| host: 'localhost', | |
| port: 5432, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| beforeAll(async () => { | |
| return new Promise(async (resolve, reject) => { | |
| //Instantiate DB | |
| //wait for db readiness | |
| //open API connection | |
| //Open test doubles ('mocking') sandbox | |
| sinonSandbox = sinon.sandbox.create(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const MongodbMemoryServer = require('mongodb-memory-server'); | |
| const globalConfigPath = join(__dirname, 'globalConfig.json'); | |
| const mongod = new MongodbMemoryServer.default(); | |
| module.exports = async () => { | |
| if (!mongod.isRunning) { | |
| await mongod.start(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it("When asking for an admin, ensure only ordered admins in results" , ()={ | |
| //assuming we've added here two admins | |
| const allAdmins = getUsers({adminOnly:true}); | |
| expect(allAdmins).to.include.ordered.members(["admin1" , "admin2"]) | |
| .but.not.include.ordered.members(["user1"]); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it("When asking for an admin, ensure only ordered admins in results" , ()={ | |
| //assuming we've added here two admins "admin1", "admin2" and "user1" | |
| const allAdmins = getUsers({adminOnly:true}); | |
| const admin1Found, adming2Found = false; | |
| allAdmins.forEach(aSingleUser => { | |
| if(aSingleUser === "user1"){ | |
| assert.notEqual(aSingleUser, "user1", "A user was found and not admin"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "scripts": { | |
| "inspect:sanity-testing": "mocha **/**--test.js --grep \"sanity\"", | |
| "inspect:lint": "eslint .", | |
| "inspect:vulnerabilities": "npm audit", | |
| "inspect:license": "license-checker --failOn GPLv2", | |
| "inspect:complexity": "plato .", | |
| "inspect:all": "concurrently -c \"bgBlue.bold,bgMagenta.bold,yellow\" \"npm:inspect:quick-testing\" \"npm:inspect:lint\" \"npm:inspect:vulnerabilities\" \"npm:inspect:license\"" | |
| }, | |
| "husky": { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //the middleware we want to test | |
| const unitUnderTest = require('./middleware') | |
| const httpMocks = require('node-mocks-http'); | |
| //Jest syntax, equivelant to describe() & it() in Mocha | |
| test('A request without authentication header, should return http status 403', () => { | |
| const request = httpMocks.createRequest({ | |
| method: 'GET', | |
| url: '/user/42', | |
| headers: { | |
| authentication: '' |