template.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const html = require('html-template-tag')
  2. /**
  3. * Generates HTML templates from list of test sheets
  4. */
  5. module.exports = (stylesheets, helpers, spec, less) => {
  6. if (!Array.isArray(helpers)) {
  7. helpers = [helpers]
  8. }
  9. return html`
  10. <!DOCTYPE html>
  11. <html lang="en">
  12. <head>
  13. <meta charset="utf-8">
  14. <title>Less.js Spec Runner</title>
  15. <!-- for each test, generate CSS/LESS link tags -->
  16. $${stylesheets.map(function(fullLessName) {
  17. var pathParts = fullLessName.split('/');
  18. var fullCssName = fullLessName.replace(/\/less\//g, '/css/').replace(/less$/, 'css')
  19. var lessName = pathParts[pathParts.length - 1];
  20. var name = lessName.split('.')[0];
  21. return `
  22. <!-- the tags to be generated -->
  23. <link id="original-less:test-less-${name}" title="test-less-${name}" rel="stylesheet/less" type="text/css" href="../../${fullLessName}">
  24. <link id="expected-less:test-less-${name}" rel="stylesheet" type="text/css" href="../../${fullCssName}">
  25. ` }).join('')}
  26. $${helpers.map(helper => `
  27. <script src="../../${helper}"></script>
  28. `).join('')}
  29. <link rel="stylesheet" href="../../node_modules/mocha/mocha.css">
  30. </head>
  31. <body>
  32. <!-- content -->
  33. <div id="mocha"></div>
  34. <script src="../../node_modules/mocha/mocha.js"></script>
  35. <script src="../../node_modules/mocha-teamcity-reporter/lib/teamcityBrowser.js"></script>
  36. <script src="../../node_modules/chai/chai.js"></script>
  37. <script>
  38. expect = chai.expect
  39. mocha.setup({
  40. ui: 'bdd',
  41. timeout: 2500
  42. });
  43. </script>
  44. <script src="common.js"></script>
  45. <script src="../../${spec}"></script>
  46. <script src="${less || 'less.min.js'}"></script>
  47. <script>
  48. /** Saucelabs config */
  49. onload = function() {
  50. var runner = mocha.run();
  51. var failedTests = [];
  52. runner.on('end', function() {
  53. window.mochaResults = runner.stats;
  54. window.mochaResults.reports = failedTests;
  55. });
  56. runner.on('fail', logFailure);
  57. function logFailure(test, err){
  58. var flattenTitles = function(test){
  59. var titles = [];
  60. while (test.parent.title) {
  61. titles.push(test.parent.title);
  62. test = test.parent;
  63. }
  64. return titles.reverse();
  65. };
  66. failedTests.push({name: test.title, result: false, message: err.message, stack: err.stack, titles: flattenTitles(test) });
  67. };
  68. };
  69. </script>
  70. </body>
  71. </html>
  72. `
  73. }