Skip to content

Commit cc60bbd

Browse files
committed
[Tests] use mock-property
1 parent 30004b2 commit cc60bbd

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

test/parse.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -446,14 +446,14 @@ test('parse()', function (t) {
446446
});
447447

448448
t.test('should not throw when a native prototype has an enumerable property', function (st) {
449-
Object.prototype.crash = '';
450-
Array.prototype.crash = '';
449+
st.intercept(Object.prototype, 'crash', { value: '' });
450+
st.intercept(Array.prototype, 'crash', { value: '' });
451+
451452
st.doesNotThrow(qs.parse.bind(null, 'a=b'));
452453
st.deepEqual(qs.parse('a=b'), { a: 'b' });
453454
st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
454455
st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
455-
delete Object.prototype.crash;
456-
delete Array.prototype.crash;
456+
457457
st.end();
458458
});
459459

@@ -629,10 +629,12 @@ test('parse()', function (t) {
629629
});
630630

631631
t.test('does not blow up when Buffer global is missing', function (st) {
632-
var tempBuffer = global.Buffer;
633-
delete global.Buffer;
632+
var restore = mockProperty(global, 'Buffer', { 'delete': true });
633+
634634
var result = qs.parse('a=b&c=d');
635-
global.Buffer = tempBuffer;
635+
636+
restore();
637+
636638
st.deepEqual(result, { a: 'b', c: 'd' });
637639
st.end();
638640
});

test/stringify.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var utils = require('../lib/utils');
66
var iconv = require('iconv-lite');
77
var SaferBuffer = require('safer-buffer').Buffer;
88
var hasSymbols = require('has-symbols');
9+
var mockProperty = require('mock-property');
910
var emptyTestCases = require('./empty-keys-cases').emptyTestCases;
1011
var hasBigInt = typeof BigInt === 'function';
1112

@@ -677,10 +678,11 @@ test('stringify()', function (t) {
677678
});
678679

679680
t.test('skips properties that are part of the object prototype', function (st) {
680-
Object.prototype.crash = 'test';
681+
st.intercept(Object.prototype, 'crash', { value: 'test' });
682+
681683
st.equal(qs.stringify({ a: 'b' }), 'a=b');
682684
st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
683-
delete Object.prototype.crash;
685+
684686
st.end();
685687
});
686688

@@ -704,10 +706,12 @@ test('stringify()', function (t) {
704706
});
705707

706708
t.test('does not blow up when Buffer global is missing', function (st) {
707-
var tempBuffer = global.Buffer;
708-
delete global.Buffer;
709+
var restore = mockProperty(global, 'Buffer', { 'delete': true });
710+
709711
var result = qs.stringify({ a: 'b', c: 'd' });
710-
global.Buffer = tempBuffer;
712+
713+
restore();
714+
711715
st.equal(result, 'a=b&c=d');
712716
st.end();
713717
});

0 commit comments

Comments
 (0)