zjsunit: Restore missing fields correctly in with_field.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-02-22 19:54:34 -08:00
committed by Anders Kaseorg
parent 58b18fd499
commit 3ccb4391d8

View File

@@ -67,12 +67,17 @@ exports.restore = function () {
};
exports.with_field = function (obj, field, val, f) {
const had_val = Object.prototype.hasOwnProperty.call(obj, field);
const old_val = obj[field];
try {
obj[field] = val;
return f();
} finally {
if (had_val) {
obj[field] = old_val;
} else {
delete obj[field];
}
}
};