Skip to content

Instantly share code, notes, and snippets.

View goldbergyoni's full-sized avatar
💭
I may be slow to respond

Yoni Goldberg goldbergyoni

💭
I may be slow to respond
View GitHub Profile
@goldbergyoni
goldbergyoni / test-hooks.js
Last active October 31, 2019 15:59
Cleaning tests
afterAll(() => {
expressConnection.close();
})
afterEach(() => {
sinonSandbox.restore();
});
@goldbergyoni
goldbergyoni / api-ready-for-test.js
Last active October 31, 2019 15:59
Express API definition for component test
//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) => {
@goldbergyoni
goldbergyoni / api-setup-for-component-test.js
Last active October 31, 2019 16:00
API setup for component tests
//open API connection
expressApp = express();
expressConnection = expressApp.listen(() => { //no port specified
apiUnderTest(expressApp);
@goldbergyoni
goldbergyoni / open-db.js
Last active October 31, 2019 16:00
Instantiating DB for component tests
//Instantiate DB
await dockerCompose.upAll({
cwd: path.join(__dirname),
log: true
});
//wait for db readiness
await waitPort({
host: 'localhost',
port: 5432,
@goldbergyoni
goldbergyoni / setup-component-test.js
Last active October 31, 2019 16:00
Initializing for component tests
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();
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();
}
@goldbergyoni
goldbergyoni / simple-assertion.js
Created December 30, 2018 19:52
A simple assertion
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"]);
});
@goldbergyoni
goldbergyoni / complex-assertions.js
Created December 30, 2018 19:51
Skim through complex code to understand the test
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");
}
@goldbergyoni
goldbergyoni / local-ci.json
Created December 26, 2018 14:31
Doing It Right Example: npm scripts that perform code quality inspection, all are run in parallel on demand or when a developer is trying to push new code
"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": {
@goldbergyoni
goldbergyoni / middleware-test.js
Created December 26, 2018 14:29
Doing It Right Example: Testing middleware in isolation without issuing network calls and waking-up the entire Express machine
//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: ''