Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ refactor(core): Reorder downloadFile tests #899

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/downloadFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`))
})
Expand Down
44 changes: 22 additions & 22 deletions packages/core/test/downloadFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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', () => {
Expand All @@ -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,
Expand Down