diff --git a/index.js b/index.js index 728ba96..c9b4185 100755 --- a/index.js +++ b/index.js @@ -145,13 +145,15 @@ function parse (text, reviver, options) { function safeParse (text, reviver) { const { stackTraceLimit } = Error Error.stackTraceLimit = 0 + let result try { - return _parse(text, reviver, { safe: true }) + result = _parse(text, reviver, { safe: true }) } catch { - return undefined + result = undefined } finally { Error.stackTraceLimit = stackTraceLimit } + return result } module.exports = parse diff --git a/package.json b/package.json index 109c180..0d6a79f 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "benchmark": "cd benchmarks && npm install && npm run all", "lint": "eslint", "lint:fix": "eslint --fix", - "test": "nyc npm run test:unit && npm run test:typescript", + "test": "c8 --all --include index.js --check-coverage --100 npm run test:unit && npm run test:typescript", "test:unit": "tape \"test/*.test.js\"", "test:typescript": "tstyche", "test:browser": "airtap test/*.test.js" @@ -64,9 +64,9 @@ "devDependencies": { "airtap": "^5.0.0", "airtap-playwright": "^1.0.1", + "c8": "^12.0.0", "eslint": "^9.17.0", "neostandard": "^0.13.0", - "nyc": "^18.0.0", "playwright": "^1.43.1", "tape": "^5.7.5", "tstyche": "^7.1.0" diff --git a/test/index.test.js b/test/index.test.js index 835d582..8c0c1ad 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -453,11 +453,13 @@ test('safeParse', t => { t.end() }) - t.test('returns undefined on invalid object string', t => { + t.test('returns undefined on invalid object string and resets stackTraceLimit', t => { + Error.stackTraceLimit = 42 t.strictEqual( j.safeParse('{"a": 5, "b": 6'), undefined ) + t.strictEqual(Error.stackTraceLimit, 42) t.end() })