Last active
June 23, 2016 10:20
-
-
Save arieljannai/43c8603f7e594998b6390cba9d8244cf to your computer and use it in GitHub Desktop.
Jasmine matcher 'toHaveEqualContent' - expect two files to have the same content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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