mirror of
https://github.com/tenrok/axios.git
synced 2026-06-11 18:02:32 +03:00
Added toJSONObject util; (#5247)
Fixed AxiosError.toJSON method to avoid circular references; Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -23,4 +23,30 @@ describe('utils', function (){
|
||||
assert.equal(utils.isFormData(new FormData()), true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('toJSON', function (){
|
||||
it('should convert to a plain object without circular references', function () {
|
||||
const obj= {a: [0]}
|
||||
const source = {x:1, y:2, obj};
|
||||
source.circular1 = source;
|
||||
obj.a[1] = obj;
|
||||
|
||||
assert.deepStrictEqual(utils.toJSONObject(source), {
|
||||
x: 1, y:2, obj: {a: [0]}
|
||||
});
|
||||
});
|
||||
|
||||
it('should use objects with defined toJSON method without rebuilding', function () {
|
||||
const objProp = {};
|
||||
const obj= {objProp, toJSON(){
|
||||
return {ok: 1}
|
||||
}};
|
||||
const source = {x:1, y:2, obj};
|
||||
|
||||
const jsonObject = utils.toJSONObject(source);
|
||||
|
||||
assert.strictEqual(jsonObject.obj.objProp, objProp);
|
||||
assert.strictEqual(JSON.stringify(jsonObject), JSON.stringify({x: 1, y:2, obj: {ok: 1}}))
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user