From fb05fb134fb67f8879711054b6e0da0febbbdc7b Mon Sep 17 00:00:00 2001 From: johnjbarton Date: Wed, 28 Nov 2018 06:56:30 -0800 Subject: [PATCH] fix(server): use flatted for json.stringify (#3220) Remove json3 dep and use, probably was needed for older node code. Fixes #3215 --- lib/server.js | 4 +++- lib/utils/json-utils.js | 10 ++++++++++ package.json | 2 +- test/client/karma.spec.js | 1 - test/unit/utils/json-utils.spec.js | 18 ++++++++++++++++++ 5 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 lib/utils/json-utils.js create mode 100644 test/unit/utils/json-utils.spec.js diff --git a/lib/server.js b/lib/server.js index b4c62196e..9624d4c6b 100644 --- a/lib/server.js +++ b/lib/server.js @@ -8,8 +8,10 @@ const spawn = require('child_process').spawn const tmp = require('tmp') const fs = require('fs') const path = require('path') + const BundleUtils = require('./utils/bundle-utils') const NetUtils = require('./utils/net-utils') +const JsonUtils = require('./utils/json-utils') const root = global || window || this const cfg = require('./config') @@ -63,7 +65,7 @@ class Server extends KarmaEventEmitter { const config = cfg.parseConfig(cliOptions.configFile, cliOptions) - this.log.debug('Final config', JSON.stringify(config, null, 2)) + this.log.debug('Final config', JsonUtils.stringify(config, null, 2)) let modules = [{ helper: ['value', helper], diff --git a/lib/utils/json-utils.js b/lib/utils/json-utils.js new file mode 100644 index 000000000..a6bb481a9 --- /dev/null +++ b/lib/utils/json-utils.js @@ -0,0 +1,10 @@ + +const {stringify} = require('flatted/cjs') + +const JsonUtils = { + stringify (obj) { + return stringify(obj) + } +} + +module.exports = JsonUtils diff --git a/package.json b/package.json index 6a8005a4c..ad743dc73 100644 --- a/package.json +++ b/package.json @@ -412,6 +412,7 @@ "eslint-plugin-promise": "^3.4.2", "eslint-plugin-react": "^7.0.1", "eslint-plugin-standard": "^3.0.1", + "flatted": "^2.0.0", "grunt": "^1.0.0", "grunt-auto-release": "^0.0.7", "grunt-browserify": "^5.0.0", @@ -428,7 +429,6 @@ "http2": "^3.3.6", "husky": "^0.14.3", "jasmine-core": "^2.3.4", - "json3": "^3.3.2", "karma-browserify": "^5.0.1", "karma-browserstack-launcher": "^1.0.0", "karma-chai": "^0.1.0", diff --git a/test/client/karma.spec.js b/test/client/karma.spec.js index bb933c0f6..78745962f 100644 --- a/test/client/karma.spec.js +++ b/test/client/karma.spec.js @@ -1,6 +1,5 @@ // shim all the things require('core-js/es5') -global.JSON = require('json3') var sinon = require('sinon') var assert = require('assert') diff --git a/test/unit/utils/json-utils.spec.js b/test/unit/utils/json-utils.spec.js new file mode 100644 index 000000000..aa6757e81 --- /dev/null +++ b/test/unit/utils/json-utils.spec.js @@ -0,0 +1,18 @@ +'use strict' + +const JsonUtils = require('../../../lib/utils/json-utils') + +describe('json-utils', () => { + it('stringify-s', () => { + const obj = {a: 'a', i: 1} + const json = JsonUtils.stringify(obj) + expect(json).to.be.equal('[{"a":"1","i":1},"a"]') + }) + it('stringify-s circular data', () => { + const a = [{}] + a[0].a = a + a.push(a) + + expect(JsonUtils.stringify(a)).to.be.equal('[["1","0"],{"a":"0"}]') + }) +})