diff --git a/lib/search/format-package-stream.js b/lib/search/format-package-stream.js index c88df5eb4be04..fb7d81856d63f 100644 --- a/lib/search/format-package-stream.js +++ b/lib/search/format-package-stream.js @@ -42,7 +42,7 @@ class JSONOutputStream extends Minipass { } end () { - super.write(this._didFirst ? ']\n' : '\n]\n') + super.write(this._didFirst ? ']\n' : '\n[]\n') super.end() } } diff --git a/test/lib/search.js b/test/lib/search.js index 510a470f48088..55b584b8aa7dc 100644 --- a/test/lib/search.js +++ b/test/lib/search.js @@ -130,6 +130,37 @@ t.test('search --json', (t) => { src.end() }) +t.test('search --json', (t) => { + const src = new Minipass() + src.objectMode = true + + npm.flatOptions.json = true + config.json = true + const libnpmsearch = { + stream () { + return src + }, + } + + const Search = t.mock('../../lib/search.js', { + ...mocks, + libnpmsearch, + }) + const search = new Search(npm) + + search.exec(['foo'], (err) => { + if (err) + throw err + + t.equal(result, '\n[]\n', 'should have expected empty square brackets') + + config.json = false + t.end() + }) + + src.end() +}) + t.test('search --searchexclude --searchopts', t => { npm.flatOptions.search = { ...flatOptions.search,