Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -907,8 +907,8 @@ res.render = function render(view, options, callback) {
opts = {};
}

// merge res.locals
opts._locals = self.locals;
// merge res.locals (into a copy, so the caller's options are not mutated)
opts = { ...opts, _locals: self.locals };

// default callback to respond
done = done || function (err, str) {
Expand Down
19 changes: 19 additions & 0 deletions test/res.render.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

var assert = require('node:assert')
var express = require('..');
var path = require('node:path')
var request = require('supertest');
Expand Down Expand Up @@ -295,6 +296,24 @@ describe('res', function(){
.get('/')
.expect('<p>jane</p>', done);
})

it('should not mutate the options object', function(done){
var app = createApp();

app.set('views', path.join(__dirname, 'fixtures'))
var options = { user: { name: 'tobi' } };
var optionsCopy = { ...options };

app.use(function(req, res){
res.locals.foo = 'bar';
res.render('user.tmpl', options);
});

request(app)
.get('/')
.expect(() => assert.deepStrictEqual(options, optionsCopy))
.end(done)
})
})

describe('.render(name, options, fn)', function(){
Expand Down
Loading