diff --git a/src/commands/app/build.js b/src/commands/app/build.js index 3fbd237b..52af9ef1 100644 --- a/src/commands/app/build.js +++ b/src/commands/app/build.js @@ -57,8 +57,8 @@ class Build extends BaseCommand { } spinner.succeed(chalk.green('Building actions')) } catch (err) { - this.log(err) spinner.fail(chalk.green('Building actions')) + throw err } } else { spinner.info('no backend or a build already exists, skipping action build') @@ -78,8 +78,8 @@ class Build extends BaseCommand { } spinner.succeed(chalk.green('Building web assets')) } catch (err) { - this.log(err) spinner.fail(chalk.green('Building web assets')) + throw err } } else { spinner.info('no frontend or a build already exists, skipping frontend build') @@ -93,7 +93,11 @@ class Build extends BaseCommand { // final message if (flags['skip-static']) { - this.log(chalk.green(chalk.bold('Build success, your actions are ready to be deployed 👌'))) + if (flags['skip-actions']) { + this.log(chalk.green(chalk.bold('Nothing to build 🚫'))) + } else { + this.log(chalk.green(chalk.bold('Build success, your actions are ready to be deployed 👌'))) + } } else { this.log(chalk.green(chalk.bold('Build success, your app is ready to be deployed 👌'))) } diff --git a/src/commands/app/deploy.js b/src/commands/app/deploy.js index ed2d24b0..761876a4 100644 --- a/src/commands/app/deploy.js +++ b/src/commands/app/deploy.js @@ -71,8 +71,8 @@ class Deploy extends BuildCommand { } spinner.succeed(chalk.green('Deploying actions')) } catch (err) { - this.log(err) spinner.fail(chalk.green('Deploying actions')) + throw err } } else { this.log('no backend, skipping action deploy') @@ -89,8 +89,8 @@ class Deploy extends BuildCommand { } spinner.succeed(chalk.green('Deploying web assets')) } catch (err) { - this.log(err) spinner.fail(chalk.green('Deploying web assets')) + throw err } } else { this.log('no frontend, skipping frontend deploy') @@ -125,7 +125,11 @@ class Deploy extends BuildCommand { // final message if (!flags['skip-deploy']) { if (flags['skip-static']) { - this.log(chalk.green(chalk.bold('Well done, your actions are now online 🏄'))) + if (flags['skip-actions']) { + this.log(chalk.green(chalk.bold('Nothing to deploy 🚫'))) + } else { + this.log(chalk.green(chalk.bold('Well done, your actions are now online 🏄'))) + } } else { this.log(chalk.green(chalk.bold('Well done, your app is now online 🏄'))) } diff --git a/test/commands/app/build.test.js b/test/commands/app/build.test.js index 64d3e8bc..8bc69b4a 100644 --- a/test/commands/app/build.test.js +++ b/test/commands/app/build.test.js @@ -175,10 +175,106 @@ describe('run', () => { expect(mockWebLib.buildWeb).toHaveBeenCalledTimes(1) }) - test('error in runPackageScript', async () => { - helpers.runPackageScript.mockRejectedValue('error') + test('build (--skip-actions and --skip-static)', async () => { + const noScriptFound = undefined + helpers.runPackageScript + .mockResolvedValueOnce(noScriptFound) // pre-app-build + .mockResolvedValueOnce(noScriptFound) // post-app-build + + command.argv = ['--skip-actions', '--skip-static'] + await command.run() + expect(command.error).toHaveBeenCalledTimes(0) + + expect(command.log).toHaveBeenCalledTimes(1) + expect(command.log).toHaveBeenCalledWith(expect.stringMatching(/Nothing to build/)) + }) + + test('build (--skip-actions)', async () => { + const noScriptFound = undefined + helpers.runPackageScript + .mockResolvedValueOnce(noScriptFound) // pre-app-build + .mockResolvedValueOnce(noScriptFound) // post-app-build + + command.argv = ['--skip-actions'] + await command.run() + expect(command.error).toHaveBeenCalledTimes(0) + + expect(command.log).toHaveBeenCalledTimes(1) + expect(command.log).toHaveBeenCalledWith(expect.stringMatching(/Build success, your app is ready to be deployed/)) + }) + + test('build (--skip-static)', async () => { + const noScriptFound = undefined + helpers.runPackageScript + .mockResolvedValueOnce(noScriptFound) // pre-app-build + .mockResolvedValueOnce(noScriptFound) // post-app-build + + command.argv = ['--skip-static'] await command.run() - expect(command.log).toHaveBeenNthCalledWith(1, 'error') - expect(command.log).toHaveBeenNthCalledWith(2, 'error') + expect(command.error).toHaveBeenCalledTimes(0) + + expect(command.log).toHaveBeenCalledTimes(1) + expect(command.log).toHaveBeenCalledWith(expect.stringMatching(/Build success, your actions are ready to be deployed/)) + }) + + test('build (has build-actions and build-static hooks)', async () => { + const noScriptFound = undefined + const childProcess = {} + helpers.runPackageScript + .mockResolvedValueOnce(noScriptFound) // pre-app-build + .mockResolvedValueOnce(childProcess) // build-actions (uses hook) + .mockResolvedValueOnce(childProcess) // build-static (uses hook) + .mockResolvedValueOnce(noScriptFound) // post-app-build + + await command.run() + expect(command.error).toHaveBeenCalledTimes(0) + + expect(command.log).toHaveBeenCalledTimes(1) + expect(command.log).toHaveBeenCalledWith(expect.stringMatching(/Build success, your app is ready to be deployed/)) + }) + + test('build (pre and post hooks have errors, --skip-actions and --skip-static)', async () => { + helpers.runPackageScript + .mockRejectedValueOnce('error-pre-app-build') // pre-app-build (logs error) + .mockRejectedValueOnce('error-post-app-build') // post-app-build (logs error) + + command.argv = ['--skip-actions', '--skip-static'] + await command.run() + expect(command.error).toHaveBeenCalledTimes(0) + + expect(command.log).toHaveBeenCalledTimes(3) + expect(command.log).toHaveBeenCalledWith('error-pre-app-build') + expect(command.log).toHaveBeenCalledWith('error-post-app-build') + expect(command.log).toHaveBeenCalledWith(expect.stringMatching(/Nothing to build/)) + }) + + test('build (build-actions hook has an error)', async () => { + const noScriptFound = undefined + helpers.runPackageScript + .mockResolvedValueOnce(noScriptFound) // pre-app-build (no error) + .mockRejectedValueOnce('error-build-actions') // build-actions (rethrows error) + .mockResolvedValueOnce(noScriptFound) // build-static (will not reach here) + .mockResolvedValueOnce(noScriptFound) // post-app-build (will not reach here) + + await command.run() + expect(command.error).toHaveBeenCalledTimes(1) + expect(command.error).toHaveBeenCalledWith('error-build-actions') + + expect(command.log).toHaveBeenCalledTimes(0) + }) + + test('build (build-static hook has an error)', async () => { + const noScriptFound = undefined + helpers.runPackageScript + .mockResolvedValueOnce(noScriptFound) // pre-app-build (no error) + .mockResolvedValueOnce(noScriptFound) // build-actions (uses inbuilt, no error) + .mockRejectedValueOnce('error-build-static') // build-static (rethrows error) + .mockResolvedValueOnce(noScriptFound) // post-app-build (will not reach here) + + await command.run() + expect(command.error).toHaveBeenCalledTimes(1) + expect(command.error).toHaveBeenCalledWith('error-build-static') + + expect(command.log).toHaveBeenCalledTimes(0) }) }) diff --git a/test/commands/app/deploy.test.js b/test/commands/app/deploy.test.js index f5b4d4ba..dd47c6a1 100644 --- a/test/commands/app/deploy.test.js +++ b/test/commands/app/deploy.test.js @@ -365,14 +365,106 @@ describe('run', () => { expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(1) }) - test('build & deploy (app hooks missing)', async () => { + test('deploy (--skip-actions and --skip-static)', async () => { + const noScriptFound = undefined helpers.runPackageScript - .mockRejectedValueOnce('error-1') - .mockRejectedValueOnce('error-2') + .mockResolvedValueOnce(noScriptFound) // pre-app-deploy + .mockResolvedValueOnce(noScriptFound) // post-app-deploy + command.argv = ['--skip-actions', '--skip-static'] await command.run() expect(command.error).toHaveBeenCalledTimes(0) - expect(command.log).toHaveBeenCalledWith('error-1') - expect(command.log).toHaveBeenCalledWith('error-2') + + expect(command.log).toHaveBeenCalledTimes(1) + expect(command.log).toHaveBeenCalledWith(expect.stringMatching(/Nothing to deploy/)) + }) + + test('deploy (--skip-actions)', async () => { + const noScriptFound = undefined + helpers.runPackageScript + .mockResolvedValueOnce(noScriptFound) // pre-app-deploy + .mockResolvedValueOnce(noScriptFound) // post-app-deploy + + command.argv = ['--skip-actions'] + await command.run() + expect(command.error).toHaveBeenCalledTimes(0) + + expect(command.log).toHaveBeenCalledTimes(1) + expect(command.log).toHaveBeenCalledWith(expect.stringMatching(/Well done, your app is now online/)) + }) + + test('deploy (--skip-static)', async () => { + const noScriptFound = undefined + helpers.runPackageScript + .mockResolvedValueOnce(noScriptFound) // pre-app-deploy + .mockResolvedValueOnce(noScriptFound) // post-app-deploy + + command.argv = ['--skip-static'] + await command.run() + expect(command.error).toHaveBeenCalledTimes(0) + + expect(command.log).toHaveBeenCalledTimes(1) + expect(command.log).toHaveBeenCalledWith(expect.stringMatching(/Well done, your actions are now online/)) + }) + + test('deploy (has deploy-actions and deploy-static hooks)', async () => { + const noScriptFound = undefined + const childProcess = {} + helpers.runPackageScript + .mockResolvedValueOnce(noScriptFound) // pre-app-deploy + .mockResolvedValueOnce(childProcess) // deploy-actions (uses hook) + .mockResolvedValueOnce(childProcess) // deploy-static (uses hook) + .mockResolvedValueOnce(noScriptFound) // post-app-deploy + + await command.run() + expect(command.error).toHaveBeenCalledTimes(0) + + expect(command.log).toHaveBeenCalledTimes(1) + expect(command.log).toHaveBeenCalledWith(expect.stringMatching(/Well done, your app is now online/)) + }) + + test('deploy (pre and post hooks have errors, --skip-actions and --skip-static)', async () => { + helpers.runPackageScript + .mockRejectedValueOnce('error-pre-app-deploy') // pre-app-deploy (logs error) + .mockRejectedValueOnce('error-post-app-deploy') // post-app-deploy (logs error) + + command.argv = ['--skip-actions', '--skip-static'] + await command.run() + expect(command.error).toHaveBeenCalledTimes(0) + + expect(command.log).toHaveBeenCalledTimes(3) + expect(command.log).toHaveBeenCalledWith('error-pre-app-deploy') + expect(command.log).toHaveBeenCalledWith('error-post-app-deploy') + expect(command.log).toHaveBeenCalledWith(expect.stringMatching(/Nothing to deploy/)) + }) + + test('deploy (deploy-actions hook has an error)', async () => { + const noScriptFound = undefined + helpers.runPackageScript + .mockResolvedValueOnce(noScriptFound) // pre-app-deploy (no error) + .mockRejectedValueOnce('error-deploy-actions') // deploy-actions (rethrows error) + .mockResolvedValueOnce(noScriptFound) // deploy-static (will not reach here) + .mockResolvedValueOnce(noScriptFound) // post-app-deploy (will not reach here) + + await command.run() + expect(command.error).toHaveBeenCalledTimes(1) + expect(command.error).toHaveBeenCalledWith('error-deploy-actions') + + expect(command.log).toHaveBeenCalledTimes(0) + }) + + test('deploy (deploy-static hook has an error)', async () => { + const noScriptFound = undefined + helpers.runPackageScript + .mockResolvedValueOnce(noScriptFound) // pre-app-deploy (no error) + .mockResolvedValueOnce(noScriptFound) // deploy-actions (uses inbuilt, no error) + .mockRejectedValueOnce('error-deploy-static') // deploy-static (rethrows error) + .mockResolvedValueOnce(noScriptFound) // post-app-deploy (will not reach here) + + await command.run() + expect(command.error).toHaveBeenCalledTimes(1) + expect(command.error).toHaveBeenCalledWith('error-deploy-static') + + expect(command.log).toHaveBeenCalledTimes(0) }) }) diff --git a/test/jest.setup.js b/test/jest.setup.js index 493c5421..4b29daf5 100644 --- a/test/jest.setup.js +++ b/test/jest.setup.js @@ -12,6 +12,8 @@ governing permissions and limitations under the License. const { stdout, stderr } = require('stdout-stderr') +jest.setTimeout(30000) + const fs = require.requireActual('fs') const eol = require('eol') const path = require('path')