From 67a843542198747b1be020ab039909b94f5a5e2b Mon Sep 17 00:00:00 2001 From: LuanRT Date: Tue, 2 Nov 2021 16:41:45 -0300 Subject: [PATCH] tests: throw an error if one or more tests fail --- test/index.js | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/test/index.js b/test/index.js index dfd02941..815fc50d 100644 --- a/test/index.js +++ b/test/index.js @@ -6,27 +6,34 @@ const NToken = require('../lib/NToken'); const SigDecipher = require('../lib/Sig'); const Constants = require('./constants'); +let failed_tests = 0; + async function performTests() { const youtube = await new Innertube().catch((error) => error); assert(youtube instanceof Error ? false : true, `should retrieve Innertube configuration data`); - - const search = await youtube.search('Carl Sagan - Documentary').catch((error) => error); - assert((search instanceof Error ? false : true) && search.videos.length >= 1, `should search videos`); - - const details = await youtube.getDetails(Constants.test_video_id).catch((error) => error); - assert(details instanceof Error ? false : true, `should retrieve details for ${Constants.test_video_id}`); - - const comments = await youtube.getComments(Constants.test_video_id).catch((error) => error); - assert(comments instanceof Error ? false : true, `should retrieve comments for ${Constants.test_video_id}`); - const video = await downloadVideo(Constants.test_video_id_1, youtube).catch((error) => error); - assert(video instanceof Error ? false : true, `should download video (${Constants.test_video_id_1})`); - + if (!(youtube instanceof Error)) { + const search = await youtube.search('Carl Sagan - Documentary').catch((error) => error); + assert((search instanceof Error ? false : true) && search.videos.length >= 1, `should search videos`); + + const details = await youtube.getDetails(Constants.test_video_id).catch((error) => error); + assert(details instanceof Error ? false : true, `should retrieve details for ${Constants.test_video_id}`); + + const comments = await youtube.getComments(Constants.test_video_id).catch((error) => error); + assert(comments instanceof Error ? false : true, `should retrieve comments for ${Constants.test_video_id}`); + + const video = await downloadVideo(Constants.test_video_id_1, youtube).catch((error) => error); + assert(video instanceof Error ? false : true, `should download video (${Constants.test_video_id_1})`); + } + const n_token = new NToken(Constants.n_scramble_sc).transform(Constants.original_ntoken); assert(n_token == Constants.expected_ntoken, `should transform n token into ${Constants.expected_ntoken}`); const transformed_url = new SigDecipher(Constants.test_url, Constants.client_version, { sig_decipher_sc: Constants.sig_decipher_sc, ntoken_sc: Constants.n_scramble_sc }).decipher(); assert(transformed_url == Constants.expected_url, `should correctly decipher signature`); + + if (failed_tests > 0) + throw new Error('Some tests have failed'); } function downloadVideo(id, youtube) { @@ -40,8 +47,9 @@ function downloadVideo(id, youtube) { }); } -function assert(outcome, description) { - const pass_fail = outcome ? 'pass' : 'fail'; +function assert(outcome, description) { + const pass_fail = outcome ? 'pass' : 'fail'; + !outcome && (failed_tests += 1); console.info(pass_fail, ':', description); return outcome; };