diff --git a/lib/search/format-package-stream.js b/lib/search/format-package-stream.js index c908601144c23..c88df5eb4be04 100644 --- a/lib/search/format-package-stream.js +++ b/lib/search/format-package-stream.js @@ -43,6 +43,7 @@ class JSONOutputStream extends Minipass { end () { super.write(this._didFirst ? ']\n' : '\n]\n') + super.end() } } diff --git a/test/lib/search.js b/test/lib/search.js index ea7ec4ca7c19e..b1ba7775c73c8 100644 --- a/test/lib/search.js +++ b/test/lib/search.js @@ -82,6 +82,48 @@ t.test('search ', t => { src.end() }) +t.test('search --json', (t) => { + const src = new Minipass() + src.objectMode = true + + flatOptions.json = true + const libnpmsearch = { + stream () { + return src + }, + } + + const Search = requireInject('../../lib/search.js', { + ...mocks, + libnpmsearch, + }) + const search = new Search(npm) + + search.exec(['libnpm'], (err) => { + if (err) + throw err + + const parsedResult = JSON.parse(result) + parsedResult.forEach((entry) => { + entry.date = new Date(entry.date) + }) + + t.same( + parsedResult, + libnpmsearchResultFixture, + 'should have expected search results as json' + ) + + flatOptions.json = false + t.end() + }) + + for (const i of libnpmsearchResultFixture) + src.write(i) + + src.end() +}) + t.test('search --searchexclude --searchopts', t => { npm.flatOptions.search = { ...flatOptions.search,