Skip to content

Instantly share code, notes, and snippets.

@tsmx
tsmx / jest.config.js
Created August 9, 2020 19:05
Jest: set environment variable for all tests via configuration file
// MY_VAR is available in all Jest tests and suites, no need to adapt the before() or beforeAll() functions
process.env['MY_VAR'] = 'My-Var-value';
module.exports = {
testEnvironment: 'node'
};
@tsmx
tsmx / process-exit.test.js
Last active August 9, 2020 19:25
Jest: properly test a function branch ending in process.exit, e.g. in a CLI app
const myFunc = function (condition) {
console.log('before');
if (condition) {
process.exit(-1);
}
console.log('after');
}
describe('jest-process-exit test suite', () => {
@tsmx
tsmx / json-traverse.js
Last active September 22, 2020 19:32
Traversing a JSON object in JavaScript for printing it out or applying functions/filters to every key/value - tracks the full path of every element, supports arrays, nested objects, arrays-in-arrays and objects-in-arrays
// the sample simply logs every item with its corresponding path
// replace 'logKeyValue' with custom functions/filters/... that should be applied
function traverse(obj, path = []) {
Object.entries(obj).forEach(([key, val]) => {
if (val !== null && typeof val == 'object') {
if (Array.isArray(val)) {
for (let i = 0; i < val.length; i++) {
let elem = val[i];
let itemKey = createKeyString(i, true);
@tsmx
tsmx / json-to-html-list.js
Created September 23, 2020 11:02
Create a HTML list from a JSON object. Supports simple nested objects and flat arrays.
function jsonToHtmlList(obj, level = 0) {
if (level == 0) {
console.log('<ul>');
}
else {
console.log((' ').repeat(level) + '<ul class=\"nested\">');
}
Object.entries(obj).forEach(([key, val]) => {
if (val !== null && typeof val == 'object' && !Array.isArray(val)) {
console.log((' ').repeat(level) + ' <li class=\"caret\">Key: ' + key + '</li>')
@tsmx
tsmx / mongoose-encryption-at-rest.js
Last active October 10, 2020 18:55
Encryption-at-rest in MongoDB with Mongoose getters/setters and NodeJS Crypto.
const crypto = require('crypto');
const algorithm = 'aes-256-cbc';
const dbURI = 'mongodb://localhost/encryptiontest';
const dbOptions = {
user: 'encryptiontest',
pass: 'test',
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
@tsmx
tsmx / apache-benchmark-automation.sh
Created October 25, 2020 20:26
A simple automation script for Apache Benchmark with bearer token authentication. Running multiple scenarios of requests/concurrency.
#!/bin/bash
# set URL to test here
url=YOUR_URL_HERE
# set test iterations here: requests|concurrent
declare -a iterations=("1|1" "10|10" "100|20" "100|100" "1000|50" "1000|100")
# first argument is the bearer token
if [ -z "$1" ]
@tsmx
tsmx / streamutils.js
Last active November 9, 2020 19:14
NodeJS: convert stream to buffer and vice versa
const Readable = require('stream').Readable;
// stream-to-buffer promise variant
module.exports.streamToBufferPromise = function (stream) {
return new Promise((resolve, reject) => {
var bufs = [];
var buffer = null;
stream.on('error', (error) => { reject(error); });
stream.on('data', (data) => { bufs.push(data); });
stream.on('end', () => {
@tsmx
tsmx / activate-gitignore.sh
Created November 9, 2020 19:31
Activate a changed gitignore file if it is not recognized
git rm -r --cached .
git add .
@tsmx
tsmx / nodejs-promise-basic-example.js
Last active November 11, 2020 20:10
NodeJS Promise example showing basic usage and control flow with: resolve, reject, then, catch & throw.
function isOdd(x) {
return new Promise((resolve, reject) => {
if (x == 11) throw new Error('11 is not allowed!');
if (x % 2) {
resolve(x.toString() + ' is odd')
}
else {
reject(new Error(x.toString() + ' is even'));
}
});
@tsmx
tsmx / git-build-nodejs-coveralls.yml
Last active November 22, 2020 20:14
Building a NodeJS app with GitHub actions and report test results to Coveralls.
# save as ./github/workflows/git-ci-build.yml
# make sure that 'test-coverage' generates the coverage reports (lcov)
name: git-ci-build
on:
[push]
jobs:
build: