Skip to content

Commit

Permalink
fix: don't try to require('diff') for browsers
Browse files Browse the repository at this point in the history
the sourcemaps crash some front-end transpilers :-(
  • Loading branch information
dbushong committed Jul 17, 2017
1 parent c3a5b9b commit bf8e03a
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 18 deletions.
12 changes: 3 additions & 9 deletions lib/assertive.js

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

2 changes: 1 addition & 1 deletion lib/assertive.js.map

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions lib/diff-stub.js

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

1 change: 1 addition & 0 deletions lib/diff-stub.js.map

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
]
}
},
"browser": {
"diff": "./lib/diff-stub.js"
},
"dependencies": {
"diff": "^3.3.0",
"lodash": "^4.6.1"
Expand Down
11 changes: 3 additions & 8 deletions src/assertive.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ let assert;
// eslint-disable-next-line global-require
const _ = global._ || require('lodash');

// we can make this optional for browser use
let jsDiff;
// eslint-disable-next-line global-require
try { jsDiff = require('diff'); } catch (err) { /* */ }
// may be stubbed out for browsers
const jsDiff = require('diff');

const toString = Object.prototype.toString;

Expand Down Expand Up @@ -313,7 +311,7 @@ ${red(wrongLooks)}`, explanation);
if (wrongLooks === rightLooks) {
message = `deepEqual ${green(rightLooks)} failed on something that\n` +
'serializes to the same result (likely some function)';
} else if (jsDiff) {
} else {
const values = jsDiff.diffJson(actual, expected).map((entry) => {
let value = entry.value;
let prefix = ' ';
Expand All @@ -326,9 +324,6 @@ ${red(wrongLooks)}`, explanation);
});
message =
`Actual: ${red('-')} Expected: ${green('+')}\n${values.join('\n')}`;
} else {
message = `Expected: ${green(rightLooks)}\n Actually: ` +
`${red(wrongLooks)}`;
}

throw error(message, explanation);
Expand Down
39 changes: 39 additions & 0 deletions src/diff-stub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2013, Groupon, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of GROUPON nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// can't require('diff') properly with browserify, so just fall back on this
function diffJson(a, b) {
return [
{ removed: true, value: JSON.stringify(a, null, 2) },
{ added: true, value: JSON.stringify(b, null, 2) },
];
}
exports.diffJson = diffJson;

0 comments on commit bf8e03a

Please sign in to comment.