-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(amplify-provider-awscloudformation): add tests for utils/archive…
…r.js (#7104)
- Loading branch information
1 parent
8ced06c
commit ec1a5a7
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
packages/amplify-provider-awscloudformation/src/__tests__/utils/archiver.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const archiver = require('../../utils/archiver'); | ||
const archiverLib = require('archiver'); | ||
const path = require('path'); | ||
const fs = require('fs-extra'); | ||
|
||
const mockZip = { | ||
pipe: jest.fn(), | ||
glob: jest.fn(), | ||
file: jest.fn(), | ||
finalize: jest.fn(), | ||
}; | ||
|
||
const mockWriteStream = { | ||
on: jest.fn().mockImplementation(function (this, event, handler) { | ||
if (event === 'close') { | ||
handler(); | ||
} | ||
return this; | ||
}), | ||
}; | ||
|
||
jest.mock('path'); | ||
jest.mock('fs-extra'); | ||
jest.mock('archiver'); | ||
|
||
describe('run archiver', () => { | ||
test('test archiver.run function zips successfully', async () => { | ||
path.dirname.mockImplementation(() => 'TestZipFolderName'); | ||
path.basename.mockImplementation(() => 'TestZipFileName'); | ||
fs.createWriteStream.mockImplementation(() => mockWriteStream); | ||
archiverLib.create.mockImplementation(() => mockZip); | ||
|
||
const res = await archiver.run('testFolder', 'testZipFilePath'); | ||
|
||
expect(res).toEqual({ zipFilePath: 'testZipFilePath', zipFilename: 'TestZipFileName' }); | ||
expect(fs.ensureDir).toBeCalledWith('TestZipFolderName'); | ||
expect(path.basename).toBeCalled(); | ||
expect(mockZip.glob).toHaveBeenCalledWith('api/*/build/**', { | ||
cwd: 'testFolder', | ||
dot: true, | ||
}); | ||
expect(mockZip.finalize).toBeCalled(); | ||
}); | ||
|
||
test('test archiver.run function handles error if zipping fails', async () => { | ||
const mockFailedWriteStream = { | ||
on: jest.fn().mockImplementation(function (this, event, handler) { | ||
if (event === 'error') { | ||
handler(); | ||
} | ||
return this; | ||
}), | ||
}; | ||
|
||
fs.createWriteStream.mockImplementation(() => mockFailedWriteStream); | ||
archiverLib.create.mockImplementation(() => mockZip); | ||
|
||
await expect(archiver.run('testFolder', 'testZipFilePath')).rejects.toThrowErrorMatchingInlineSnapshot('"Failed to zip code."'); | ||
}); | ||
|
||
test('test archiver.run function zips successfully if extraFiles arg provided', async () => { | ||
path.basename.mockImplementation(() => 'TestZipFileName'); | ||
fs.createWriteStream.mockImplementation(() => mockWriteStream); | ||
archiverLib.create.mockImplementation(() => mockZip); | ||
|
||
await archiver.run('testFolder', 'testZipFilePath', undefined, ['testFilePath1', 'testFilePath2']); | ||
expect(mockZip.file.mock.calls).toEqual([ | ||
['testFilePath1', { name: 'TestZipFileName' }], | ||
['testFilePath2', { name: 'TestZipFileName' }], | ||
]); | ||
}); | ||
}); |