Skip to content

Commit

Permalink
Convert use of proxyquire for mocking over to mockable-imports Babel …
Browse files Browse the repository at this point in the history
…plugin

This is mostly done for consistency with the client. It will also make
some future changes to the code (eg. adoption of ES imports or changes
to the bundling) easier if/when we decide to do that.

See hypothesis/client#1061 for background.
  • Loading branch information
robertknight committed May 2, 2019
1 parent c822489 commit 4001283
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 47 deletions.
8 changes: 5 additions & 3 deletions h/static/scripts/karma.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ module.exports = function(config) {

browserify: {
debug: true,
configure: function (bundle) {
bundle.plugin('proxyquire-universal');
},
transform: [
['babelify', {
plugins: ['mockable-imports'],
}],
],
},

mochaReporter: {
Expand Down
12 changes: 7 additions & 5 deletions h/static/scripts/tests/base/raven-test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';

const proxyquire = require('proxyquire');
const noCallThru = require('../util').noCallThru;
const raven = require('../../base/raven');

describe('raven', () => {
let fakeRavenJS;
let raven;

beforeEach(() => {
fakeRavenJS = {
Expand All @@ -17,9 +15,13 @@ describe('raven', () => {
setUserContext: sinon.stub(),
};

raven = proxyquire('../../base/raven', noCallThru({
raven.$imports.$mock({
'raven-js': fakeRavenJS,
}));
});
});

afterEach(() => {
raven.$imports.$restore();
});

describe('.init()', () => {
Expand Down
9 changes: 4 additions & 5 deletions h/static/scripts/tests/controllers/form-controller-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const proxyquire = require('proxyquire');

const { hyphenate } = require('../../util/string');
const { noCallThru } = require('../util');
const upgradeElements = require('../../base/upgrade-elements');
const FormController = require('../../controllers/form-controller');

/**
* Converts a map of data attribute names to string values into a string
Expand Down Expand Up @@ -72,8 +70,8 @@ describe('FormController', () => {

function initForm(template) {
fakeSubmitForm = sinon.stub();
const FormController = proxyquire('../../controllers/form-controller', {
'../util/submit-form': noCallThru(fakeSubmitForm),
FormController.$imports.$mock({
'../util/submit-form': fakeSubmitForm,
});

const container = document.createElement('div');
Expand Down Expand Up @@ -107,6 +105,7 @@ describe('FormController', () => {
afterEach(() => {
ctrl.beforeRemove();
ctrl.element.remove();
FormController.$imports.$restore();
});

function isEditing() {
Expand Down
23 changes: 0 additions & 23 deletions h/static/scripts/tests/util.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
'use strict';

/**
* Utility function for use with 'proxyquire' that prevents calls to
* stubs 'calling through' to the _original_ dependency if a particular
* function or property is not set on a stub, which is proxyquire's default
* but usually undesired behavior.
*
* See https://github.com/thlorenz/proxyquireify#nocallthru
*
* Usage:
* var moduleUnderTest = proxyquire('./module-under-test', noCallThru({
* './dependency-foo': fakeFoo,
* }));
*
* @param {Object} stubs - A map of dependency paths to stubs, or a single
* stub.
*/
function noCallThru(stubs) {
// This function is trivial but serves as documentation for why
// '@noCallThru' is used.
return Object.assign(stubs, {'@noCallThru':true});
}

/**
* Helper for writing parameterized tests.
*
Expand Down Expand Up @@ -77,6 +55,5 @@ function unroll(description, testFn, fixtures) {
}

module.exports = {
noCallThru: noCallThru,
unroll: unroll,
};
47 changes: 36 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"whatwg-fetch": "^0.10.1"
},
"devDependencies": {
"babel-plugin-mockable-imports": "^1.3.0",
"chai": "^3.5.0",
"check-dependencies": "^0.12.0",
"diff": "^2.2.2",
Expand Down

0 comments on commit 4001283

Please sign in to comment.