Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Karrys <luke@lukekarrys.com>2022-10-19 20:04:29 +0300
committerLuke Karrys <luke@lukekarrys.com>2022-10-19 22:50:41 +0300
commitd3543e945e721783dcb83385935f282a4bb32cf3 (patch)
tree29737357bb3c06fe0804345ee6e7c0e019d3468e /test/lib/utils/exit-handler.js
parentbe642c6b8e3df40fd43b0110b30d3ecd44086016 (diff)
feat: output json formatted errors on stdout (#5716)
This also adds a new output method `outputBuffer()` which will buffer all output until it is flushed in the exit handler. This allows the exit handler to catch any errors and append them to the output when in json mode. This was necessary to not introduce a regression in the case of npm/cli#2150. BREAKING CHANGE: `npm` now outputs some json errors on stdout. Previously `npm` would output all json formatted errors on stderr, making it difficult to parse as the stderr stream usually has logs already written to it. In the future, `npm` will differentiate between errors and crashes. Errors, such as `E404` and `ERESOLVE`, will be handled and will continue to be output on stdout. In the case of a crash, `npm` will log the error as usual but will not attempt to display it as json, even in `--json` mode. Moving a case from the category of an error to a crash will not be considered a breaking change. For more information see npm/rfcs#482. Closes #2740 Closes https://github.com/npm/statusboard/issues/589
Diffstat (limited to 'test/lib/utils/exit-handler.js')
-rw-r--r--test/lib/utils/exit-handler.js48
1 files changed, 46 insertions, 2 deletions
diff --git a/test/lib/utils/exit-handler.js b/test/lib/utils/exit-handler.js
index 7e3137013..d22ec4dd1 100644
--- a/test/lib/utils/exit-handler.js
+++ b/test/lib/utils/exit-handler.js
@@ -208,7 +208,7 @@ t.test('exit handler called - no npm with error without stack', async (t) => {
})
t.test('console.log output using --json', async (t) => {
- const { exitHandler, outputErrors } = await mockExitHandler(t, {
+ const { exitHandler, outputs } = await mockExitHandler(t, {
config: { json: true },
})
@@ -216,7 +216,7 @@ t.test('console.log output using --json', async (t) => {
t.equal(process.exitCode, 1)
t.same(
- JSON.parse(outputErrors[0]),
+ JSON.parse(outputs[0]),
{
error: {
code: 'EBADTHING', // should default error code to E[A-Z]+
@@ -228,6 +228,50 @@ t.test('console.log output using --json', async (t) => {
)
})
+t.test('merges output buffers errors with --json', async (t) => {
+ const { exitHandler, outputs, npm } = await mockExitHandler(t, {
+ config: { json: true },
+ })
+
+ npm.outputBuffer({ output_data: 1 })
+ npm.outputBuffer(JSON.stringify({ more_data: 2 }))
+ npm.outputBuffer('not json, will be ignored')
+
+ await exitHandler(err('Error: EBADTHING Something happened'))
+
+ t.equal(process.exitCode, 1)
+ t.same(
+ JSON.parse(outputs[0]),
+ {
+ output_data: 1,
+ more_data: 2,
+ error: {
+ code: 'EBADTHING', // should default error code to E[A-Z]+
+ summary: 'Error: EBADTHING Something happened',
+ detail: 'Error: EBADTHING Something happened',
+ },
+ },
+ 'should output expected json output'
+ )
+})
+
+t.test('output buffer without json', async (t) => {
+ const { exitHandler, outputs, npm, logs } = await mockExitHandler(t)
+
+ npm.outputBuffer('output_data')
+ npm.outputBuffer('more_data')
+
+ await exitHandler(err('Error: EBADTHING Something happened'))
+
+ t.equal(process.exitCode, 1)
+ t.same(
+ outputs,
+ [['output_data'], ['more_data']],
+ 'should output expected output'
+ )
+ t.match(logs.error, [['code', 'EBADTHING']])
+})
+
t.test('throw a non-error obj', async (t) => {
const { exitHandler, logs } = await mockExitHandler(t)