Super Simple Assert JavaScript Testing


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <!DOCTYPE HTML>
  2. <html lang="en-US">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title> Assert </title>
  6. <style>
  7. .pass:before {
  8. content: 'PASS: ';
  9. color: blue;
  10. font-weight: bold;
  11. }
  12.  
  13. .fail:before {
  14. content: 'FAIL: ';
  15. color: red;
  16. font-weight: bold;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21.  
  22. <ul id="output"></ul>
  23.  
  24. <script>
  25.  
  26. var output = document.getElementById('output');
  27.  
  28. // Via Resig
  29. function assert( outcome, explanation ) {
  30. var li = document.createElement('li');
  31. li.className = outcome ? 'pass' : 'fail';
  32. li.appendChild( document.createTextNode( explanation ) );
  33. output.appendChild(li);
  34. }
  35.  
  36. assert( Array.prototype.each, 'Checking if the each method of the Array object is available, as an example.' );
  37. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.