From 0c7f4501238bbca753996cf9b1db3b786518822b Mon Sep 17 00:00:00 2001 From: Daniel Izdebski Date: Tue, 3 Oct 2023 18:00:58 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(core):=20Reorder?= =?UTF-8?q?=20`downloadFile`=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/downloadFile.ts | 2 +- packages/core/test/downloadFile.test.ts | 44 ++++++++++++------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/packages/core/src/downloadFile.ts b/packages/core/src/downloadFile.ts index 4e3275fab..921ad9fdc 100644 --- a/packages/core/src/downloadFile.ts +++ b/packages/core/src/downloadFile.ts @@ -48,7 +48,7 @@ export async function downloadFile(options: DownloaderOptions) { reject(new Error(`[Writer] ${error.message}`)) }) }) - .catch((error) => { + .catch((error: Error) => { // TODO: Handle errors in a more sophisticated way reject(new Error(`[Axios] ${error.message}`)) }) diff --git a/packages/core/test/downloadFile.test.ts b/packages/core/test/downloadFile.test.ts index 02753de6c..679099e4c 100644 --- a/packages/core/test/downloadFile.test.ts +++ b/packages/core/test/downloadFile.test.ts @@ -67,6 +67,27 @@ describe('downloadFile', () => { }) }) + it('throws an error if the file cannot be downloaded', async () => { + server.use( + rest.get(MOCK_URL, (_, res, ctx) => { + return res( + ctx.status(500), + ctx.json({ message: 'Internal server error' }) + ) + }) + ) + + const downloadFileFuncCall = downloadFile({ + url: MOCK_URL, + outputDir: MOCK_OUTPUT_DIR, + fileName: MOCK_FILE_NAME + }) + + await expect(downloadFileFuncCall).rejects.toThrowError( + '[DownloadFile] Error downloading the file - [Axios] Request failed with status code 500' + ) + }) + describe('when the file does not exist', () => { it('downloads a file', async () => { const result = await downloadFile({ @@ -82,27 +103,6 @@ describe('downloadFile', () => { expect(fs.existsSync(result.filePath)).toBe(true) expect(fs.readFileSync(result.filePath, 'utf8')).toBe(MOCK_FILE_CONTENT) }) - - it('throws an error if the file cannot be downloaded', async () => { - server.use( - rest.get(MOCK_URL, (_, res, ctx) => { - return res( - ctx.status(500), - ctx.json({ message: 'Internal server error' }) - ) - }) - ) - - const downloadFileFuncCall = downloadFile({ - url: MOCK_URL, - outputDir: MOCK_OUTPUT_DIR, - fileName: MOCK_FILE_NAME - }) - - await expect(() => downloadFileFuncCall).rejects.toThrowError( - '[DownloadFile] Error downloading the file - [Axios] Request failed with status code 500' - ) - }) }) describe('when the file already exists', () => { @@ -113,7 +113,7 @@ describe('downloadFile', () => { expect(fs.existsSync(MOCK_FILE_PATH)).toBe(true) }) - it('skips download if the file exists', async () => { + it('skips download', async () => { const result = await downloadFile({ url: MOCK_URL, outputDir: MOCK_OUTPUT_DIR,