Skip to content

Commit

Permalink
[Tests] use mock-property
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 28, 2024
1 parent 30004b2 commit cc60bbd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
16 changes: 9 additions & 7 deletions test/parse.js
Expand Up @@ -446,14 +446,14 @@ test('parse()', function (t) {
});

t.test('should not throw when a native prototype has an enumerable property', function (st) {
Object.prototype.crash = '';
Array.prototype.crash = '';
st.intercept(Object.prototype, 'crash', { value: '' });
st.intercept(Array.prototype, 'crash', { value: '' });

st.doesNotThrow(qs.parse.bind(null, 'a=b'));
st.deepEqual(qs.parse('a=b'), { a: 'b' });
st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
delete Object.prototype.crash;
delete Array.prototype.crash;

st.end();
});

Expand Down Expand Up @@ -629,10 +629,12 @@ test('parse()', function (t) {
});

t.test('does not blow up when Buffer global is missing', function (st) {
var tempBuffer = global.Buffer;
delete global.Buffer;
var restore = mockProperty(global, 'Buffer', { 'delete': true });

var result = qs.parse('a=b&c=d');
global.Buffer = tempBuffer;

restore();

st.deepEqual(result, { a: 'b', c: 'd' });
st.end();
});
Expand Down
14 changes: 9 additions & 5 deletions test/stringify.js
Expand Up @@ -6,6 +6,7 @@ var utils = require('../lib/utils');
var iconv = require('iconv-lite');
var SaferBuffer = require('safer-buffer').Buffer;
var hasSymbols = require('has-symbols');
var mockProperty = require('mock-property');
var emptyTestCases = require('./empty-keys-cases').emptyTestCases;
var hasBigInt = typeof BigInt === 'function';

Expand Down Expand Up @@ -677,10 +678,11 @@ test('stringify()', function (t) {
});

t.test('skips properties that are part of the object prototype', function (st) {
Object.prototype.crash = 'test';
st.intercept(Object.prototype, 'crash', { value: 'test' });

st.equal(qs.stringify({ a: 'b' }), 'a=b');
st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
delete Object.prototype.crash;

st.end();
});

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

t.test('does not blow up when Buffer global is missing', function (st) {
var tempBuffer = global.Buffer;
delete global.Buffer;
var restore = mockProperty(global, 'Buffer', { 'delete': true });

var result = qs.stringify({ a: 'b', c: 'd' });
global.Buffer = tempBuffer;

restore();

st.equal(result, 'a=b&c=d');
st.end();
});
Expand Down

0 comments on commit cc60bbd

Please sign in to comment.