Skip to content

Instantly share code, notes, and snippets.

@bsommardahl
Last active June 2, 2017 15:47
Show Gist options
  • Save bsommardahl/da8251512a0a72facca9c12aea4bdb7b to your computer and use it in GitHub Desktop.
Save bsommardahl/da8251512a0a72facca9c12aea4bdb7b to your computer and use it in GitHub Desktop.
ForEach Testing Example
console.log(0);
console.log(1);
console.log(2);
console.log(3);
console.log(4);
console.log(5);
console.log(6);
console.log(7);
console.log(8);
console.log(9);
for(var i=0;i<10;i++){
console.log(i);
}
const path = require('path');
console = {
log: jest.fn()
};
const theFile = '../for-loop.js';
describe('for loop and console log output', () => {
require(theFile);
it('should call the console log 10 times', () => {
expect(console.log.mock.calls.length).toBe(10);
});
it('should call log all 10 numbers', () => {
for(var i=0;i<10;++i){
expect(console.log.mock.calls[i][0]).toBe(i);
}
});
it('should not cheat', () => {
var fs = require('fs');
function countInstances(string, word) {
var substrings = string.split(word);
return substrings.length - 1;
}
const pathToFile = path.join(__dirname, theFile);
const text = fs.readFileSync(pathToFile, 'utf8');
const instances = countInstances(text, 'console.log');
expect(instances).toBe(1);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment