app.render.js
This file
Path
test/app.render.js
374 lines · 10.3 KiB · JavaScript
Last changed by
fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
History of this fileTake it
The walk stopped after 200 commits: lines older than 6aa4a45 point at it.
Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 1why, one step back1'use strict'Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 2why, one step back2fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
line 3why, one step back3var assert = require('node:assert')Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 4why, one step back4var express = require('..');fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
line 5why, one step back5var path = require('node:path')Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 6why, one step back6var tmpl = require('./support/tmpl');Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 7–10why, one step back78describe('app', function(){9 describe('.render(name, fn)', function(){10 it('should support absolute paths', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 11why, one step back11 var app = createApp();Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 12–14why, one step back1213 app.locals.user = { name: 'tobi' };14Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 15why, one step back15 app.render(path.join(__dirname, 'fixtures', 'user.tmpl'), function (err, str) {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 16why, one step back16 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 17why, one step back17 assert.strictEqual(str, '<p>tobi</p>')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 18–22why, one step back18 done();19 })20 })2122 it('should support absolute paths with "view engine"', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 23why, one step back23 var app = createApp();Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 24why, one step back24Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 25why, one step back25 app.set('view engine', 'tmpl');Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 26–27why, one step back26 app.locals.user = { name: 'tobi' };27Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 28why, one step back28 app.render(path.join(__dirname, 'fixtures', 'user'), function (err, str) {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 29why, one step back29 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 30why, one step back30 assert.strictEqual(str, '<p>tobi</p>')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 31–35why, one step back31 done();32 })33 })3435 it('should expose app.locals', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 36why, one step back36 var app = createApp();Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 37why, one step back37Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 38why, one step back38 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 39–40why, one step back39 app.locals.user = { name: 'tobi' };40Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 41why, one step back41 app.render('user.tmpl', function (err, str) {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 42why, one step back42 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 43why, one step back43 assert.strictEqual(str, '<p>tobi</p>')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 44–48why, one step back44 done();45 })46 })4748 it('should support index.<engine>', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 49why, one step back49 var app = createApp();Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 50why, one step back50Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 51why, one step back51 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 52why, one step back52 app.set('view engine', 'tmpl');Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 53why, one step back53Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 54why, one step back54 app.render('blog/post', function (err, str) {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 55why, one step back55 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 56why, one step back56 assert.strictEqual(str, '<h1>blog post</h1>')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 57–75why, one step back57 done();58 })59 })6061 it('should handle render error throws', function(done){62 var app = express();6364 function View(name, options){65 this.name = name;66 this.path = 'fale';67 }6869 View.prototype.render = function(options, fn){70 throw new Error('err!');71 };7273 app.set('view', View);7475 app.render('something', function(err, str){Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 76–77why, one step back76 assert.ok(err)77 assert.strictEqual(err.message, 'err!')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 78–83why, one step back78 done();79 })80 })8182 describe('when the file does not exist', function(){83 it('should provide a helpful error', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 84–85why, one step back84 var app = createApp();85Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 86why, one step back86 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 87why, one step back87 app.render('rawr.tmpl', function (err) {Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 88–89why, one step back88 assert.ok(err)89 assert.equal(err.message, 'Failed to lookup view "rawr.tmpl" in views directory "' + path.join(__dirname, 'fixtures') + '"')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 90–96why, one step back90 done();91 });92 })93 })9495 describe('when an error occurs', function(){96 it('should invoke the callback', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 97why, one step back97 var app = createApp();Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 98why, one step back98Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 99why, one step back99 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 100why, one step back100Merge tag '4.15.5'Douglas Christopher Wilsonbefore adoption19c8d64
lines 101–104why, one step back101 app.render('user.tmpl', function (err) {102 assert.ok(err)103 assert.equal(err.name, 'RenderError')104 done()Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 105–110why, one step back105 })106 })107 })108109 describe('when an extension is given', function(){110 it('should render the template', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 111why, one step back111 var app = createApp();Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 112why, one step back112Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 113why, one step back113 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 114why, one step back114Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 115why, one step back115 app.render('email.tmpl', function (err, str) {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 116why, one step back116 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 117why, one step back117 assert.strictEqual(str, '<p>This is an email</p>')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 118–124why, one step back118 done();119 })120 })121 })122123 describe('when "view engine" is given', function(){124 it('should render the template', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 125why, one step back125 var app = createApp();Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 126why, one step back126Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 127why, one step back127 app.set('view engine', 'tmpl');Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 128why, one step back128 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 129–131why, one step back129130 app.render('email', function(err, str){131 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 132why, one step back132 assert.strictEqual(str, '<p>This is an email</p>')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 133–137why, one step back133 done();134 })135 })136 })137Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 138–139why, one step back138 describe('when "views" is given', function(){139 it('should lookup the file in the path', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 140why, one step back140 var app = createApp();Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
line 141why, one step back141Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 142why, one step back142 app.set('views', path.join(__dirname, 'fixtures', 'default_layout'))Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 143–144why, one step back143 app.locals.user = { name: 'tobi' };144Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 145why, one step back145 app.render('user.tmpl', function (err, str) {Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
line 146why, one step back146 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 147why, one step back147 assert.strictEqual(str, '<p>tobi</p>')Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 148–153why, one step back148 done();149 })150 })151152 describe('when array of paths', function(){153 it('should lookup the file in the path', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 154why, one step back154 var app = createApp();Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 155–158why, one step back155 var views = [156 path.join(__dirname, 'fixtures', 'local_layout'),157 path.join(__dirname, 'fixtures', 'default_layout')158 ]Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 159–162why, one step back159160 app.set('views', views);161 app.locals.user = { name: 'tobi' };162Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 163why, one step back163 app.render('user.tmpl', function (err, str) {Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
line 164why, one step back164 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 165why, one step back165 assert.strictEqual(str, '<span>tobi</span>')Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 166–170why, one step back166 done();167 })168 })169170 it('should lookup in later paths until found', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 171why, one step back171 var app = createApp();Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 172–175why, one step back172 var views = [173 path.join(__dirname, 'fixtures', 'local_layout'),174 path.join(__dirname, 'fixtures', 'default_layout')175 ]Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 176–179why, one step back176177 app.set('views', views);178 app.locals.name = 'tobi';179Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 180why, one step back180 app.render('name.tmpl', function (err, str) {Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
line 181why, one step back181 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 182why, one step back182 assert.strictEqual(str, '<p>tobi</p>')Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 183–187why, one step back183 done();184 })185 })186187 it('should error if file does not exist', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 188why, one step back188 var app = createApp();Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 189–192why, one step back189 var views = [190 path.join(__dirname, 'fixtures', 'local_layout'),191 path.join(__dirname, 'fixtures', 'default_layout')192 ]Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 193–196why, one step back193194 app.set('views', views);195 app.locals.name = 'tobi';196Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 197why, one step back197 app.render('pet.tmpl', function (err, str) {Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 198–199why, one step back198 assert.ok(err)199 assert.equal(err.message, 'Failed to lookup view "pet.tmpl" in views directories "' + views[0] + '" or "' + views[1] + '"')Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 200–205why, one step back200 done();201 })202 })203 })204 })205Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 206–222why, one step back206 describe('when a "view" constructor is given', function(){207 it('should create an instance of it', function(done){208 var app = express();209210 function View(name, options){211 this.name = name;212 this.path = 'path is required by application.js as a signal of success even though it is not used there.';213 }214215 View.prototype.render = function(options, fn){216 fn(null, 'abstract engine');217 };218219 app.set('view', View);220221 app.render('something', function(err, str){222 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 223why, one step back223 assert.strictEqual(str, 'abstract engine')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 224–248why, one step back224 done();225 })226 })227 })228229 describe('caching', function(){230 it('should always lookup view without cache', function(done){231 var app = express();232 var count = 0;233234 function View(name, options){235 this.name = name;236 this.path = 'fake';237 count++;238 }239240 View.prototype.render = function(options, fn){241 fn(null, 'abstract engine');242 };243244 app.set('view cache', false);245 app.set('view', View);246247 app.render('something', function(err, str){248 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 249–250why, one step back249 assert.strictEqual(count, 1)250 assert.strictEqual(str, 'abstract engine')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 251–252why, one step back251 app.render('something', function(err, str){252 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 253–254why, one step back253 assert.strictEqual(count, 2)254 assert.strictEqual(str, 'abstract engine')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 255–278why, one step back255 done();256 })257 })258 })259260 it('should cache with "view cache" setting', function(done){261 var app = express();262 var count = 0;263264 function View(name, options){265 this.name = name;266 this.path = 'fake';267 count++;268 }269270 View.prototype.render = function(options, fn){271 fn(null, 'abstract engine');272 };273274 app.set('view cache', true);275 app.set('view', View);276277 app.render('something', function(err, str){278 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 279–280why, one step back279 assert.strictEqual(count, 1)280 assert.strictEqual(str, 'abstract engine')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 281–282why, one step back281 app.render('something', function(err, str){282 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 283–284why, one step back283 assert.strictEqual(count, 1)284 assert.strictEqual(str, 'abstract engine')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 285–293why, one step back285 done();286 })287 })288 })289 })290 })291292 describe('.render(name, options, fn)', function(){293 it('should render the template', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 294why, one step back294 var app = createApp();Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 295why, one step back295Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 296why, one step back296 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 297–299why, one step back297298 var user = { name: 'tobi' };299Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 300why, one step back300 app.render('user.tmpl', { user: user }, function (err, str) {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 301why, one step back301 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 302why, one step back302 assert.strictEqual(str, '<p>tobi</p>')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 303–307why, one step back303 done();304 })305 })306307 it('should expose app.locals', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 308why, one step back308 var app = createApp();Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 309why, one step back309Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 310why, one step back310 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 311–312why, one step back311 app.locals.user = { name: 'tobi' };312Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 313why, one step back313 app.render('user.tmpl', {}, function (err, str) {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 314why, one step back314 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 315why, one step back315 assert.strictEqual(str, '<p>tobi</p>')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 316–320why, one step back316 done();317 })318 })319320 it('should give precedence to app.render() locals', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 321why, one step back321 var app = createApp();Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 322why, one step back322Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 323why, one step back323 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 324–326why, one step back324 app.locals.user = { name: 'tobi' };325 var jane = { name: 'jane' };326Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 327why, one step back327 app.render('user.tmpl', { user: jane }, function (err, str) {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 328why, one step back328 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 329why, one step back329 assert.strictEqual(str, '<p>jane</p>')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 330–353why, one step back330 done();331 })332 })333334 describe('caching', function(){335 it('should cache with cache option', function(done){336 var app = express();337 var count = 0;338339 function View(name, options){340 this.name = name;341 this.path = 'fake';342 count++;343 }344345 View.prototype.render = function(options, fn){346 fn(null, 'abstract engine');347 };348349 app.set('view cache', false);350 app.set('view', View);351352 app.render('something', {cache: true}, function(err, str){353 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 354–355why, one step back354 assert.strictEqual(count, 1)355 assert.strictEqual(str, 'abstract engine')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 356–357why, one step back356 app.render('something', {cache: true}, function(err, str){357 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 358–359why, one step back358 assert.strictEqual(count, 1)359 assert.strictEqual(str, 'abstract engine')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 360–366why, one step back360 done();361 })362 })363 })364 })365 })366})Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 367–374why, one step back367368function createApp() {369 var app = express();370371 app.engine('.tmpl', tmpl);372373 return app;374}