Created
May 15, 2019 00:06
-
-
Save roblevintennis/6711fe4c2a81eafcfa38530f93da922a to your computer and use it in GitHub Desktop.
This file contains 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
// How will we move forward given ES6 module imports cannot be spied on e.g. you'll get error: | |
// Cannot set property of #<Object> which has only a getter | |
// Unless you transpile down to CommonJS (which works for now but probably not forever) | |
// Below are some ideas… | |
// ---- Dependency Injection ---- // | |
// Implementation | |
import * as self from 'foo'; | |
export const init = function (dependencyA) { | |
self.dependency = dependency || dependencyDefault; | |
} | |
export const foo = function () { | |
self.dependency(); | |
} | |
// Spec | |
import * as SUT from 'impl'; | |
... | |
it 'calls dependencyA', -> | |
const spy = jasmine.createSpy(‘dependency'); | |
view.init(spy); | |
view.foo(); | |
expect(spy).toHaveBeenCalled() | |
// ---- Wrapper Facade Method ---- // | |
// Implementation | |
import * as Dep from 'dependency.js'; | |
import * as self from 'foo'; // safe in ES6 see cyclic dependencies | |
export const foo = function () { | |
if(something) | |
self.callDependency(expectedArg) | |
} | |
export const callDependency = function (arg) { | |
Dep.dependency(arg); | |
} | |
// Spec | |
import * as moduleUnderTest from 'impl'; | |
... | |
it 'calls dependencyA', -> | |
spyOn(view, 'callDependency'); | |
expect(view.callDependency).toHaveBeenCalledWith(expectedArg) | |
// ---- Jest Mocks ---- // | |
// dependency.js | |
export const foo = () => { } | |
// depdency.mock.js (sits right besides dependency.js) | |
export const mockFunction = jest.fn(); | |
jest.mock(‘dependency.js’, () => { myFunction: mockFunction })); | |
// Spec | |
import { mockFunction } from ‘dependency.mock'; | |
import SUT from 'impl'; | |
... | |
it 'calls dependencyA', -> | |
mockFunction.returnValue(false); | |
view.functionThatCallsDependency(); | |
expect(mockFunction).toHaveBeenCalled(); | |
// ---- rewiremock (like proxyquire but works w/Webpack?) ---- // | |
const mock = await rewiremock.module(() => import(‘somemodule’), { | |
‘dep1’: { foo: ‘fake’ }, ‘dep2’: { bar: ‘fake’ } | |
})); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment