Example on how to test with mocks and stubs

Change-type: patch
This commit is contained in:
Otavio Jacobi 2024-09-10 19:11:01 -03:00
parent 629ac9e5e9
commit 357866e345
2 changed files with 79 additions and 3 deletions

6
npm-shrinkwrap.json generated
View File

@ -14810,9 +14810,9 @@
"integrity": "sha512-ev2QzSzWPYmy9GuqfIVildA4OdcGLeFZQrq5ys6RtiuF+RQQiZWr8TZNyAcuVXyQRYfEO+MsoB/1BuQVhOJuoQ=="
},
"node_modules/pump": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz",
"integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==",
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.1.tgz",
"integrity": "sha512-2ynnAmUu45oUSq51AQbeugLkMSKaz8FqVpZ6ykTqzOVkzXe8u/ezkGsYrFJqKZx+D9cVxoDrSbR7CeAwxFa5cQ==",
"dependencies": {
"end-of-stream": "^1.1.0",
"once": "^1.3.1"

View File

@ -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;
});
});