Skip to content

Instantly share code, notes, and snippets.

@arieljannai
Last active June 23, 2016 10:20
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 arieljannai/43c8603f7e594998b6390cba9d8244cf to your computer and use it in GitHub Desktop.
Save arieljannai/43c8603f7e594998b6390cba9d8244cf to your computer and use it in GitHub Desktop.
Jasmine matcher 'toHaveEqualContent' - expect two files to have the same content
var fs = require('fs');
var fsp = {
stat: function(file) {
return new Promise(function(resolve, reject) {
fs.stat(file, function(err, stats) {
if (err) return reject(err);
return resolve(stats);
});
});
},
readFile: function(file, options) {
options = options || {};
return new Promise(function(resolve, reject) {
fs.readFile(file, options, function(err, content) {
if (err) return reject(err);
return resolve(content);
});
});
}
};
beforeEach(function() {
jasmine.addMatchers({
toHaveEqualContent: function() {
return {
compare: function(actual, expected) {
var result = {};
var actualContent;
var expectedContent;
return fsp.stat(actual)
.then(function() {
return fsp.stat(expected);
})
.then(function() {
return fsp.readFile(actual);
})
.then(function(content) {
actualContent = content;
return fsp.readFile(expected);
})
.then(function(content) {
expectedContent = content;
result.pass = actualContent === expectedContent;
return result;
})
.catch(function(err) {
result.message = 'There was an error while trying to compare the files. ' +
'Maybe some of them does not exist or could not be read.\n' + err.toString();
return result;
});
}
};
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment