Use let or const to avoid scope issues and hoisting
var assert = require('node:assert')
1'use strict'23var assert = require('node:assert')4var ejs = require('ejs');5var express = require('..');6var path = require('node:path')7var tmpl = require('./support/tmpl');89describe('app', function(){10 describe('.render(name, fn)', function(){11 it('should support absolute paths', function(done){12 var app = createApp();1314 app.locals.user = { name: 'tobi' };1516 app.render(path.join(__dirname, 'fixtures', 'user.tmpl'), function (err, str) {17 if (err) return done(err);18 assert.strictEqual(str, '<p>tobi</p>')19 done();20 })21 })2223 it('should support absolute paths with "view engine"', function(done){24 var app = createApp();2526 app.set('view engine', 'tmpl');27 app.locals.user = { name: 'tobi' };2829 app.render(path.join(__dirname, 'fixtures', 'user'), function (err, str) {30 if (err) return done(err);31 assert.strictEqual(str, '<p>tobi</p>')32 done();33 })34 })3536 it('should throw without a view engine or file extension', function(){37 var app = express();3839 assert.throws(function () {40 app.render('user', function () {41 assert.fail('render callback should not be called');42 });43 }, /No default engine was specified and no extension was provided\./);44 })4546 it('should throw when the view engine module does not exist', function(){47 var app = express();4849 app.set('view engine', 'express-test-missing-engine');5051 assert.throws(function () {52 app.render('user', function () {53 assert.fail('render callback should not be called');54 });55 }, {56 code: 'MODULE_NOT_FOUND',57 message: /^Cannot find module 'express-test-missing-engine'(?:\n|$)/58 });59 })6061 it('should expose app.locals', function(done){62 var app = createApp();6364 app.set('views', path.join(__dirname, 'fixtures'))65 app.locals.user = { name: 'tobi' };6667 app.render('user.tmpl', function (err, str) {68 if (err) return done(err);69 assert.strictEqual(str, '<p>tobi</p>')70 done();71 })72 })7374 it('should support index.<engine>', function(done){75 var app = createApp();7677 app.set('views', path.join(__dirname, 'fixtures'))78 app.set('view engine', 'tmpl');7980 app.render('blog/post', function (err, str) {81 if (err) return done(err);82 assert.strictEqual(str, '<h1>blog post</h1>')83 done();84 })85 })8687 it('should handle render error throws', function(done){88 var app = express();8990 function View(name, options){91 this.name = name;92 this.path = 'fale';93 }9495 View.prototype.render = function(options, fn){96 throw new Error('err!');97 };9899 app.set('view', View);100101 app.render('something', function(err, str){102 assert.ok(err)103 assert.strictEqual(err.message, 'err!')104 done();105 })106 })107108 describe('when the file does not exist', function(){109 it('should provide a helpful error', function(done){110 var app = createApp();111112 app.set('views', path.join(__dirname, 'fixtures'))113 app.render('rawr.tmpl', function (err) {114 assert.ok(err)115 assert.equal(err.message, 'Failed to lookup view "rawr.tmpl" in views directory "' + path.join(__dirname, 'fixtures') + '"')116 done();117 });118 })119 })120121 describe('when an error occurs', function(){122 it('should invoke the callback', function(done){123 var app = createApp();124125 app.set('views', path.join(__dirname, 'fixtures'))126127 app.render('user.tmpl', function (err) {128 assert.ok(err)129 assert.equal(err.name, 'RenderError')130 done()131 })132 })133134 it('should pass a sync engine throw to the callback on first render', function(done){135 var app = express();136 var error = new Error('render failed');137138 app.engine('tmpl', function () {139 throw error;140 });141 app.set('views', path.join(__dirname, 'fixtures'))142143 app.render('user.tmpl', function (err) {144 assert.strictEqual(err, error)145 done();146 })147 })148 })149150 describe('when an extension is given', function(){151 it('should render the template', function(done){152 var app = createApp();153154 app.set('views', path.join(__dirname, 'fixtures'))155156 app.render('email.tmpl', function (err, str) {157 if (err) return done(err);158 assert.strictEqual(str, '<p>This is an email</p>')159 done();160 })161 })162 })163164 describe('when "view engine" is given', function(){165 it('should render the template', function(done){166 var app = createApp();167168 app.set('view engine', 'tmpl');169 app.set('views', path.join(__dirname, 'fixtures'))170171 app.render('email', function(err, str){172 if (err) return done(err);173 assert.strictEqual(str, '<p>This is an email</p>')174 done();175 })176 })177 })178179 describe('when "views" is given', function(){180 it('should lookup the file in the path', function(done){181 var app = createApp();182183 app.set('views', path.join(__dirname, 'fixtures', 'default_layout'))184 app.locals.user = { name: 'tobi' };185186 app.render('user.tmpl', function (err, str) {187 if (err) return done(err);188 assert.strictEqual(str, '<p>tobi</p>')189 done();190 })191 })192193 describe('when array of paths', function(){194 it('should lookup the file in the path', function(done){195 var app = createApp();196 var views = [197 path.join(__dirname, 'fixtures', 'local_layout'),198 path.join(__dirname, 'fixtures', 'default_layout')199 ]200201 app.set('views', views);202 app.locals.user = { name: 'tobi' };203204 app.render('user.tmpl', function (err, str) {205 if (err) return done(err);206 assert.strictEqual(str, '<span>tobi</span>')207 done();208 })209 })210211 it('should lookup in later paths until found', function(done){212 var app = createApp();213 var views = [214 path.join(__dirname, 'fixtures', 'local_layout'),215 path.join(__dirname, 'fixtures', 'default_layout')216 ]217218 app.set('views', views);219 app.locals.name = 'tobi';220221 app.render('name.tmpl', function (err, str) {222 if (err) return done(err);223 assert.strictEqual(str, '<p>tobi</p>')224 done();225 })226 })227228 it('should continue past an empty-string root', function(done){229 var app = createApp();230231 app.set('views', ['', path.join(__dirname, 'fixtures')]);232 app.locals.user = { name: 'tobi' };233234 app.render('user.tmpl', function (err, str) {235 if (err) return done(err);236 assert.strictEqual(str, '<p>tobi</p>')237 done();238 })239 })240241 it('should continue looking up after an ENOTDIR stat error', function(done){242 var app = createApp();243 var views = [244 path.join(__dirname, 'fixtures', 'user.tmpl'),245 path.join(__dirname, 'fixtures')246 ];247248 app.set('views', views);249 app.locals.user = { name: 'tobi' };250251 app.render('user.tmpl', function (err, str) {252 if (err) return done(err);253 assert.strictEqual(str, '<p>tobi</p>')254 done();255 })256 })257258 it('should error if file does not exist', function(done){259 var app = createApp();260 var views = [261 path.join(__dirname, 'fixtures', 'local_layout'),262 path.join(__dirname, 'fixtures', 'default_layout')263 ]264265 app.set('views', views);266 app.locals.name = 'tobi';267268 app.render('pet.tmpl', function (err, str) {269 assert.ok(err)270 assert.equal(err.message, 'Failed to lookup view "pet.tmpl" in views directories "' + views[0] + '" or "' + views[1] + '"')271 done();272 })273 })274 })275 })276277 describe('when a "view" constructor is given', function(){278 it('should create an instance of it', function(done){279 var app = express();280281 function View(name, options){282 this.name = name;283 this.path = 'path is required by application.js as a signal of success even though it is not used there.';284 }285286 View.prototype.render = function(options, fn){287 fn(null, 'abstract engine');288 };289290 app.set('view', View);291292 app.render('something', function(err, str){293 if (err) return done(err);294 assert.strictEqual(str, 'abstract engine')295 done();296 })297 })298299 it('should error when a custom view leaves path unset', function(done){300 var app = express();301302 function View(name, options){303 this.name = name;304 this.root = options.root;305 }306307 View.prototype.render = function(){308 throw new Error('render should not be called');309 };310311 app.set('view', View);312313 app.render('something', function(err){314 assert.ok(err)315 assert.match(err.message, /^Failed to lookup view "something"/)316 done();317 })318 })319320 it('should honor a View subclass that clears path to deny a view', function(done){321 var app = express();322 var BaseView = app.get('view');323324 app.engine('tmpl', function (file, options, fn) {325 fn(null, 'served');326 });327 app.set('views', path.join(__dirname, 'fixtures'))328329 function DenyView(name, options){330 BaseView.call(this, name, options);331 this.path = null;332 }333334 DenyView.prototype = Object.create(BaseView.prototype);335 DenyView.prototype.constructor = DenyView;336337 app.set('view', DenyView);338339 app.render('user.tmpl', function (err){340 assert.ok(err, 'denied view must not be served')341 assert.match(err.message, /^Failed to lookup view "user\.tmpl"/)342 done();343 })344 })345 })346347 describe('engine caching', function(){348 it('should cache an automatically loaded engine', function(done){349 var app = express();350351 app.disable('view cache');352 app.set('views', path.join(__dirname, 'fixtures'))353 app.locals.user = { name: 'tobi' };354355 assert.strictEqual(app.engines['.ejs'], undefined)356357 app.render('user.ejs', function (err, str) {358 if (err) return done(err);359 assert.strictEqual(str, '<p>tobi</p>')360 assert.strictEqual(app.engines['.ejs'], ejs.__express)361 done();362 })363 })364365 it('should use a replacement engine on subsequent renders', function(done){366 var app = express();367 var calls = 0;368369 // Construct a new View on each render so its cached engine is exercised.370 app.disable('view cache');371 app.set('views', path.join(__dirname, 'fixtures'))372 app.locals.user = { name: 'tobi' };373374 app.render('user.ejs', function (err, str) {375 if (err) return done(err);376 assert.strictEqual(str, '<p>tobi</p>')377378 var engine = app.engines['.ejs'];379 assert.strictEqual(engine, ejs.__express)380381 app.engine('ejs', function (file, options, callback) {382 calls++;383 engine(file, options, callback);384 });385 app.locals.user = { name: 'loki' };386387 app.render('user.ejs', function (err2, str2) {388 if (err2) return done(err2);389 assert.strictEqual(str2, '<p>loki</p>')390 assert.strictEqual(calls, 1)391 done();392 })393 })394 })395 })396397 describe('caching', function(){398 it('should not cache views whose lookup failed', function(done){399 var app = createApp();400401 app.enable('view cache');402 app.set('views', path.join(__dirname, 'fixtures', 'local_layout'))403404 app.render('email.tmpl', function (err) {405 assert.ok(err)406 assert.match(err.message, /^Failed to lookup view "email\.tmpl"/)407408 app.set('views', path.join(__dirname, 'fixtures'))409410 app.render('email.tmpl', function (err2, str) {411 if (err2) return done(err2);412 assert.strictEqual(str, '<p>This is an email</p>')413 done();414 })415 })416 })417418 it('should always lookup view without cache', function(done){419 var app = express();420 var count = 0;421422 function View(name, options){423 this.name = name;424 this.path = 'fake';425 count++;426 }427428 View.prototype.render = function(options, fn){429 fn(null, 'abstract engine');430 };431432 app.set('view cache', false);433 app.set('view', View);434435 app.render('something', function(err, str){436 if (err) return done(err);437 assert.strictEqual(count, 1)438 assert.strictEqual(str, 'abstract engine')439 app.render('something', function(err, str){440 if (err) return done(err);441 assert.strictEqual(count, 2)442 assert.strictEqual(str, 'abstract engine')443 done();444 })445 })446 })447448 it('should cache with "view cache" setting', function(done){449 var app = express();450 var count = 0;451452 function View(name, options){453 this.name = name;454 this.path = 'fake';455 count++;456 }457458 View.prototype.render = function(options, fn){459 fn(null, 'abstract engine');460 };461462 app.set('view cache', true);463 app.set('view', View);464465 app.render('something', function(err, str){466 if (err) return done(err);467 assert.strictEqual(count, 1)468 assert.strictEqual(str, 'abstract engine')469 app.render('something', function(err, str){470 if (err) return done(err);471 assert.strictEqual(count, 1)472 assert.strictEqual(str, 'abstract engine')473 done();474 })475 })476 })477 })478 })479480 describe('.render(name, options, fn)', function(){481 it('should render the template', function(done){482 var app = createApp();483484 app.set('views', path.join(__dirname, 'fixtures'))485486 var user = { name: 'tobi' };487488 app.render('user.tmpl', { user: user }, function (err, str) {489 if (err) return done(err);490 assert.strictEqual(str, '<p>tobi</p>')491 done();492 })493 })494495 it('should expose app.locals', function(done){496 var app = createApp();497498 app.set('views', path.join(__dirname, 'fixtures'))499 app.locals.user = { name: 'tobi' };500501 app.render('user.tmpl', {}, function (err, str) {502 if (err) return done(err);503 assert.strictEqual(str, '<p>tobi</p>')504 done();505 })506 })507508 it('should give precedence to app.render() locals', function(done){509 var app = createApp();510511 app.set('views', path.join(__dirname, 'fixtures'))512 app.locals.user = { name: 'tobi' };513 var jane = { name: 'jane' };514515 app.render('user.tmpl', { user: jane }, function (err, str) {516 if (err) return done(err);517 assert.strictEqual(str, '<p>jane</p>')518 done();519 })520 })521522 it('should accept null or undefined options', function (done) {523 var app = createApp()524525 app.set('views', path.join(__dirname, 'fixtures'))526 app.locals.user = { name: 'tobi' }527528 app.render('user.tmpl', null, function (err, str) {529 if (err) return done(err);530 assert.strictEqual(str, '<p>tobi</p>')531532 app.render('user.tmpl', undefined, function (err2, str2) {533 if (err2) return done(err2);534 assert.strictEqual(str2, '<p>tobi</p>')535 done()536 })537 })538 })539540 describe('caching', function(){541 it('should cache with cache option', function(done){542 var app = express();543 var count = 0;544545 function View(name, options){546 this.name = name;547 this.path = 'fake';548 count++;549 }550551 View.prototype.render = function(options, fn){552 fn(null, 'abstract engine');553 };554555 app.set('view cache', false);556 app.set('view', View);557558 app.render('something', {cache: true}, function(err, str){559 if (err) return done(err);560 assert.strictEqual(count, 1)561 assert.strictEqual(str, 'abstract engine')562 app.render('something', {cache: true}, function(err, str){563 if (err) return done(err);564 assert.strictEqual(count, 1)565 assert.strictEqual(str, 'abstract engine')566 done();567 })568 })569 })570 })571 })572})573574function createApp() {575 var app = express();576577 app.engine('.tmpl', tmpl);578579 return app;580}
Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.
Generated by searchcode.com · powered by code_get_file + code_get_findings