Skip to content

Instantly share code, notes, and snippets.

@willscripted
Last active August 29, 2015 14:25
Show Gist options
  • Save willscripted/a8ad8f65f8c0d061f7a7 to your computer and use it in GitHub Desktop.
Save willscripted/a8ad8f65f8c0d061f7a7 to your computer and use it in GitHub Desktop.
compare json via named pipe
expect = require('chai').expect
diff = require('diff')
require('colors').enabled = true;
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function(){
var chunk = process.stdin.read();
if (chunk !== null) {
try {
json = JSON.parse(chunk);
expect(json[0]).to.deep.eql(json[1]);
process.stdout.write("Success\n");
} catch(err) {
var parts = diff.diffJson(err.actual,err.expected)
parts.forEach(function(part){
var color = part.added ? 'green' :
part.removed ? 'red' : 'grey';
process.stdout.write(part.value[color]);
});
process.stdout.write("\nError: " + err + "\n");
}
}
});
process.stdin.on('end', function(){
process.stdout.write('end');
});
{
"name": "comp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"chai": "^3.1.0",
"colors": "^1.1.2",
"diff": "^1.4.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment