PageRenderTime 28ms CodeModel.GetById 15ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/test/error.reporting.js

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