From 357866e345e1788a348251dd41782552dcf7c004 Mon Sep 17 00:00:00 2001 From: Otavio Jacobi Date: Tue, 10 Sep 2024 19:11:01 -0300 Subject: [PATCH] Example on how to test with mocks and stubs Change-type: patch --- npm-shrinkwrap.json | 6 +-- tests/commands/release/export.spec.ts | 76 +++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 tests/commands/release/export.spec.ts diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 98b42e026e..f2b1c742b6 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -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" diff --git a/tests/commands/release/export.spec.ts b/tests/commands/release/export.spec.ts new file mode 100644 index 0000000000..50307b32ee --- /dev/null +++ b/tests/commands/release/export.spec.ts @@ -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; + }); +});