Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roblevintennis/6711fe4c2a81eafcfa38530f93da922a to your computer and use it in GitHub Desktop.
Save roblevintennis/6711fe4c2a81eafcfa38530f93da922a to your computer and use it in GitHub Desktop.
// 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