Last active
June 2, 2017 15:47
-
-
Save bsommardahl/da8251512a0a72facca9c12aea4bdb7b to your computer and use it in GitHub Desktop.
ForEach Testing Example
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
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); |
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
for(var i=0;i<10;i++){ | |
console.log(i); | |
} |
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
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