3. Best practices for a JS test workflow
1. Create testable code
2. Find a good testing framework
3. Test against real HTML
4. Test against 'real' data
5. Use continuous integration
6. Enable in-browser debugging
7. Enable automated cross-browser testing
8. Enforce red, green, refactor!
4. Best practices for a JS test workflow
1. Create testable code
2. Find a good testing framework
3. Test against real HTML
4. Test against 'real' data
5. Use continuous integration
6. Enable in-browser debugging
7. Enable automated cross-browser testing
8. Enforce red, green, refactor!
6. Test against real HTML
$(function() {
$('#hide-action').on('click', function(e) {
JavaScript e.preventDefault();
$('#my-image').hide();
});
});
HTML <a id="hide-action" href="#hide”>Hide image</a>
<img id="my-image" src="img.png"/>
it('should hide the image when click on button', function() {
var image = $('#my-image');
Test expect(image).toBeVisible();
$('#hide-action').trigger('click');
expect(image).not.toBeVisible();
});
Test result: Success
7. Test against real HTML
$(function() {
$('#hide-action').on('click', function(e) {
JavaScript e.preventDefault();
$('#my-image').hide();
});
});
HTML <a id="hide-action" href="#hide”>Hide image</a>
<img id=”main-image" src="img.png"/>
it('should hide the image when click on button', function() {
var image = $('#my-image');
Test expect(image).toBeVisible();
$('#hide-action').trigger('click');
expect(image).not.toBeVisible();
});
Test result: Failed