Skip to content

Instantly share code, notes, and snippets.

View philipisapain's full-sized avatar

Philip Spain philipisapain

View GitHub Profile
@philipisapain
philipisapain / testing-an-exported-node-process-with-a-dependency.js
Last active March 7, 2018 15:23
Testing an exported Node process with a dependency
const SomeDependency = require('some-dependency')
const { TopLevelProcess } = require('./topLevelProcess')
describe('Stubbing a dependency passed to a constructor in an exported Node process', () => {
it('works as you would expect', () => {
const stubbedDependency = stub(new SomeDependency())
new TopLevelProcess(stubbedDependency).start()
expect(/* something to have happened when `start()` is called */)
@philipisapain
philipisapain / testing-a-node-process-with-dependency.js
Last active March 7, 2018 15:23
Testing a Node Process with a dependency
const proxyquire = require('proxyquire')
const SomeDependency = require('some-dependency')
describe('Stubbing a dependency passed to a constructor in a Node process', () => {
it('is not particularly elegant', () => {
const stubbedDependency = stub(new SomeDependency())
const StubbedDependencyConstructor = () => stubbedDependency
proxyquire('./topLevelProcess', { 'some-dependency': StubbedDependencyConstructor })
@philipisapain
philipisapain / node-entrypoint-example.js
Created March 7, 2018 14:07
Example of a Node entry point
const SomeDependency = require('some-dependency')
class TopLevelProcess {
constructor (someDependency) { ... }
start () { ... }
}
const process = new TopLevelProcess(new SomeDependency())
process.start()
@philipisapain
philipisapain / optionally-export-module.js
Last active March 7, 2018 14:03
Example of optionally exporting a module
const SomeDependency = require('some-dependency')
class TopLevelProcess {
constructor (someDependency) { ... }
start () { ... }
}
const isNodeProcess = require.main === module
if (isNodeProcess) {
new TopLevelProcess(new SomeDependency()).start()
@philipisapain
philipisapain / add-app-metadata-to-jwt.js
Created March 6, 2018 16:58
Auth0 Rule for adding app_metadata to JWT
function (user, context, callback) {
var namespace = 'https://tojs.io/';
context.accessToken[namespace + 'user_authorization'] = {
groups: user.app_metadata.groups,
roles: user.app_metadata.roles,
permissions: user.app_metadata.permissions
};
return callback(null, user, context);
}