PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/test/error.reporting.js

https://github.com/ashengwang/jade
JavaScript | 185 lines | 175 code | 6 blank | 4 comment | 2 complexity | 73728aa252c0775cd2b51cef37b402f1 MD5 | raw file
Possible License(s): MIT
  1. /**
  2. * Module dependencies.
  3. */
  4. var jade = require('../')
  5. , assert = require('assert')
  6. , fs = require('fs');
  7. // Shortcut
  8. function getError(str, options){
  9. try {
  10. jade.render(str, options);
  11. } catch (ex) {
  12. return ex;
  13. }
  14. throw new Error('Input was supposed to result in an error.');
  15. }
  16. function getFileError(name, options){
  17. try {
  18. jade.renderFile(name, options);
  19. } catch (ex) {
  20. return ex;
  21. }
  22. throw new Error('Input was supposed to result in an error.');
  23. }
  24. describe('error reporting', function () {
  25. describe('compile time errors', function () {
  26. describe('with no filename', function () {
  27. it('includes detail of where the error was thrown', function () {
  28. var err = getError('foo(')
  29. assert(/Jade:1/.test(err.message))
  30. assert(/foo\(/.test(err.message))
  31. });
  32. });
  33. describe('with a filename', function () {
  34. it('includes detail of where the error was thrown including the filename', function () {
  35. var err = getError('foo(', {filename: 'test.jade'})
  36. assert(/test\.jade:1/.test(err.message))
  37. assert(/foo\(/.test(err.message))
  38. });
  39. });
  40. describe('with a layout without block declaration (syntax)', function () {
  41. it('includes detail of where the error was thrown including the filename', function () {
  42. var err = getFileError(__dirname + '/fixtures/compile.with.layout.syntax.error.jade', {})
  43. assert(/[\\\/]layout.syntax.error.jade:2/.test(err.message))
  44. assert(/foo\(/.test(err.message))
  45. });
  46. });
  47. describe('with a layout without block declaration (locals)', function () {
  48. it('includes detail of where the error was thrown including the filename', function () {
  49. var err = getFileError(__dirname + '/fixtures/compile.with.layout.locals.error.jade', {})
  50. assert(/[\\\/]layout.locals.error.jade:2/.test(err.message))
  51. assert(/undefined is not a function/.test(err.message))
  52. });
  53. });
  54. describe('with a include (syntax)', function () {
  55. it('includes detail of where the error was thrown including the filename', function () {
  56. var err = getFileError(__dirname + '/fixtures/compile.with.include.syntax.error.jade', {})
  57. assert(/[\\\/]include.syntax.error.jade:2/.test(err.message))
  58. assert(/foo\(/.test(err.message))
  59. });
  60. });
  61. describe('with a include (locals)', function () {
  62. it('includes detail of where the error was thrown including the filename', function () {
  63. var err = getFileError(__dirname + '/fixtures/compile.with.include.locals.error.jade', {})
  64. assert(/[\\\/]include.locals.error.jade:2/.test(err.message))
  65. assert(/foo\(/.test(err.message))
  66. });
  67. });
  68. describe('with a layout (without block) with an include (syntax)', function () {
  69. it('includes detail of where the error was thrown including the filename', function () {
  70. var err = getFileError(__dirname + '/fixtures/compile.with.layout.with.include.syntax.error.jade', {})
  71. assert(/[\\\/]include.syntax.error.jade:2/.test(err.message))
  72. assert(/foo\(/.test(err.message))
  73. });
  74. });
  75. describe('with a layout (without block) with an include (locals)', function () {
  76. it('includes detail of where the error was thrown including the filename', function () {
  77. var err = getFileError(__dirname + '/fixtures/compile.with.layout.with.include.locals.error.jade', {})
  78. assert(/[\\\/]include.locals.error.jade:2/.test(err.message))
  79. assert(/foo\(/.test(err.message))
  80. });
  81. });
  82. });
  83. describe('runtime errors', function () {
  84. describe('with no filename and `compileDebug` left undefined', function () {
  85. it('just reports the line number', function () {
  86. var sentinel = new Error('sentinel');
  87. var err = getError('-foo()', {foo: function () { throw sentinel; }})
  88. assert(/on line 1/.test(err.message))
  89. });
  90. });
  91. describe('with no filename and `compileDebug` set to `true`', function () {
  92. it('includes detail of where the error was thrown', function () {
  93. var sentinel = new Error('sentinel');
  94. var err = getError('-foo()', {foo: function () { throw sentinel; }, compileDebug: true})
  95. assert(/Jade:1/.test(err.message))
  96. assert(/-foo\(\)/.test(err.message))
  97. });
  98. });
  99. describe('with a filename that does not correspond to a real file and `compileDebug` left undefined', function () {
  100. it('just reports the line number', function () {
  101. var sentinel = new Error('sentinel');
  102. var err = getError('-foo()', {foo: function () { throw sentinel; }, filename: 'fake.jade'})
  103. assert(/on line 1/.test(err.message))
  104. });
  105. });
  106. describe('with a filename that corresponds to a real file and `compileDebug` left undefined', function () {
  107. it('includes detail of where the error was thrown including the filename', function () {
  108. var sentinel = new Error('sentinel');
  109. var path = __dirname + '/fixtures/runtime.error.jade'
  110. var err = getError(fs.readFileSync(path, 'utf8'), {foo: function () { throw sentinel; }, filename: path})
  111. assert(/fixtures[\\\/]runtime\.error\.jade:1/.test(err.message))
  112. assert(/-foo\(\)/.test(err.message))
  113. });
  114. });
  115. describe('in a mixin', function () {
  116. it('includes detail of where the error was thrown including the filename', function () {
  117. var err = getFileError(__dirname + '/fixtures/runtime.with.mixin.error.jade', {})
  118. assert(/mixin.error.jade:2/.test(err.message))
  119. assert(/Cannot read property 'length' of null/.test(err.message))
  120. });
  121. });
  122. describe('in a layout', function () {
  123. it('includes detail of where the error was thrown including the filename', function () {
  124. var err = getFileError(__dirname + '/fixtures/runtime.layout.error.jade', {})
  125. assert(/layout.with.runtime.error.jade:3/.test(err.message))
  126. assert(/Cannot read property 'length' of undefined/.test(err.message))
  127. });
  128. });
  129. });
  130. describe('deprecated features', function () {
  131. it('deprecates `!!!` in favour of `doctype`', function () {
  132. var err = getError('!!! 5', {filename: 'test.jade'})
  133. assert(/test\.jade:1/.test(err.message))
  134. assert(/`!!!` is deprecated, you must now use `doctype`/.test(err.message))
  135. });
  136. it('deprecates `doctype 5` in favour of `doctype html`', function () {
  137. var err = getError('doctype 5', {filename: 'test.jade'})
  138. assert(/test\.jade:1/.test(err.message))
  139. assert(/`doctype 5` is deprecated, you must now use `doctype html`/.test(err.message))
  140. });
  141. it('warns about element-with-multiple-attributes', function () {
  142. var consoleWarn = console.warn;
  143. var log = '';
  144. console.warn = function (str) {
  145. log += str;
  146. };
  147. var res = jade.renderFile(__dirname + '/fixtures/element-with-multiple-attributes.jade');
  148. console.warn = consoleWarn;
  149. assert(/element-with-multiple-attributes.jade, line 1:/.test(log));
  150. assert(/You should not have jade tags with multiple attributes/.test(log));
  151. assert(res === '<div attr="val" foo="bar"></div>');
  152. });
  153. it('warns about missing space at the start of a line', function () {
  154. var consoleWarn = console.warn;
  155. var log = '';
  156. console.warn = function (str) {
  157. log += str;
  158. };
  159. var res = jade.render('%This line is plain text, but it should not be', {filename: 'foo.jade'});
  160. console.warn = consoleWarn;
  161. assert(log === 'Warning: missing space before text for line 1 of jade file "foo.jade"');
  162. assert(res === '%This line is plain text, but it should not be');
  163. });
  164. });
  165. describe('if you throw something that isn\'t an error', function () {
  166. it('just rethrows without modification', function () {
  167. var err = getError('- throw "foo"');
  168. assert(err === 'foo');
  169. });
  170. });
  171. describe('import without a filename for a basedir', function () {
  172. it('throws an error', function () {
  173. var err = getError('include foo.jade');
  174. assert(/the "filename" option is required to use/.test(err.message));
  175. var err = getError('include /foo.jade');
  176. assert(/the "basedir" option is required to use/.test(err.message));
  177. })
  178. });
  179. });