-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example on how to test with mocks and stubs
Change-type: patch
- Loading branch information
1 parent
629ac9e
commit 357866e
Showing
2 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,76 @@ | ||
import * as stream from 'node:stream'; | ||
import { cleanOutput, runCommand } from '../../helpers'; | ||
import { BalenaAPIMock } from '../../nock/balena-api-mock'; | ||
import { expect } from 'chai'; | ||
import * as mock from 'mock-require'; | ||
import * as sinon from 'sinon'; | ||
|
||
// "itSS" means "it() Skip Standalone" | ||
const itSS = process.env.BALENA_CLI_TEST_TYPE === 'standalone' ? it.skip : it; | ||
|
||
describe('export fleet content to a file', function () { | ||
let api: BalenaAPIMock; | ||
const releaseBundleCreateStub = sinon.stub(); | ||
|
||
this.beforeEach(() => { | ||
api = new BalenaAPIMock(); | ||
mock('@balena/release-bundle', { | ||
create: releaseBundleCreateStub, | ||
}); | ||
}); | ||
|
||
this.afterEach(() => { | ||
// Check all expected api calls have been made and clean up. | ||
api.done(); | ||
mock.stop('@balena/release-bundle'); | ||
}); | ||
|
||
itSS('should export a release to a file', async () => { | ||
api.expectGetWhoAmI(); | ||
api.expectGetRelease(); | ||
releaseBundleCreateStub.resolves(stream.Readable.from('something')); | ||
|
||
const { out, err } = await runCommand( | ||
'release export badc0ffe -o /tmp/release.tar.gz', | ||
); | ||
|
||
const lines = cleanOutput(out); | ||
expect(lines[0]).to.contain( | ||
'Release badc0ffe has been exported to /tmp/release.tar.gz.', | ||
); | ||
expect(err).to.be.empty; | ||
}); | ||
|
||
itSS('should fail if the create throws an error', async () => { | ||
api.expectGetWhoAmI(); | ||
api.expectGetRelease(); | ||
releaseBundleCreateStub.rejects( | ||
new Error('Something went wrong creating the bundle'), | ||
); | ||
|
||
const { err } = await runCommand( | ||
'release export badc0ffe -o /tmp/release.tar.gz', | ||
); | ||
|
||
expect(cleanOutput(err, true)).to.include( | ||
'Release badc0ffe could not be exported: Something went wrong creating the bundle', | ||
); | ||
}); | ||
|
||
itSS('should parse with application slug and version', async () => { | ||
api.expectGetWhoAmI(); | ||
api.expectGetRelease(); | ||
api.expectGetApplication(); | ||
releaseBundleCreateStub.resolves(stream.Readable.from('something')); | ||
|
||
const { out, err } = await runCommand( | ||
'release export org/superApp -o /tmp/release.tar.gz --version 1.2.3+rev1', | ||
); | ||
|
||
const lines = cleanOutput(out); | ||
expect(lines[0]).to.contain( | ||
'Release org/superApp version 1.2.3+rev1 has been exported to /tmp/release.tar.gz.', | ||
); | ||
expect(err).to.be.empty; | ||
}); | ||
}); |