From eeec410b485a014b58ced9c15af6756fc3ba55a8 Mon Sep 17 00:00:00 2001 From: Shazron Abdullah <36107+shazron@users.noreply.github.com> Date: Thu, 11 May 2023 15:19:01 +0800 Subject: [PATCH] updated aio app install tests --- src/commands/app/install.js | 6 ++-- test/commands/app/install.test.js | 50 +++++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/commands/app/install.js b/src/commands/app/install.js index 71e694ea..84b8d923 100644 --- a/src/commands/app/install.js +++ b/src/commands/app/install.js @@ -48,11 +48,12 @@ class InstallCommand extends BaseCommand { await this.unzipFile(args.path, outputPath) await this.validateConfig(outputPath, USER_CONFIG_FILE) await this.validateConfig(outputPath, DEPLOY_CONFIG_FILE) - await this.runTests(flags.verbose) + await this.npmInstall(flags.verbose) + await this.runTests() this.spinner.succeed('Install done.') } catch (e) { this.spinner.fail(e.message) - this.error(e.message) + this.error(flags.verbose ? e : e.message) } } @@ -128,7 +129,6 @@ class InstallCommand extends BaseCommand { } async runTests (isVerbose) { - await this.npmInstall(isVerbose) this.spinner.start('Running app tests...') return this.config.runCommand('app:test').then((result) => { if (result === 0) { // success diff --git a/test/commands/app/install.test.js b/test/commands/app/install.test.js index c7b3880e..65dee268 100644 --- a/test/commands/app/install.test.js +++ b/test/commands/app/install.test.js @@ -287,15 +287,46 @@ describe('run', () => { command.unzipFile = jest.fn() command.validateConfig = jest.fn() command.runTests = jest.fn() + command.npmInstall = jest.fn() + command.error = jest.fn() await command.run() expect(command.validateZipDirectoryStructure).toHaveBeenCalledTimes(1) expect(command.unzipFile).toHaveBeenCalledTimes(1) expect(command.validateConfig).toHaveBeenCalledTimes(2) expect(command.runTests).toHaveBeenCalledTimes(1) + expect(command.npmInstall).toHaveBeenCalledTimes(1) + expect(command.error).toHaveBeenCalledTimes(0) }) - test('subcommand throws error', async () => { + test('subcommand throws error (--verbose)', async () => { + const command = new TheCommand() + command.argv = ['my-app.zip', '--verbose'] + + const errorObject = new Error('this is a subcommand error message') + + // since we already unit test the methods above, we mock it here + // we only reject one call, to simulate a subcommand failure + command.validateZipDirectoryStructure = jest.fn() + command.unzipFile = jest.fn() + command.validateConfig = jest.fn() + command.npmInstall = jest.fn() + command.error = jest.fn() + command.runTests = jest.fn(() => { throw errorObject }) + + await command.run() + + expect(command.validateZipDirectoryStructure).toHaveBeenCalledTimes(1) + expect(command.unzipFile).toHaveBeenCalledTimes(1) + expect(command.validateConfig).toHaveBeenCalledTimes(2) + expect(command.runTests).toHaveBeenCalledTimes(1) + expect(command.npmInstall).toHaveBeenCalledTimes(1) + expect(command.error).toHaveBeenCalledTimes(1) + + expect(command.error).toHaveBeenCalledWith(errorObject) + }) + + test('subcommand throws error (not verbose)', async () => { const command = new TheCommand() command.argv = ['my-app.zip'] @@ -306,16 +337,20 @@ describe('run', () => { command.validateZipDirectoryStructure = jest.fn() command.unzipFile = jest.fn() command.validateConfig = jest.fn() - command.runTests = jest.fn(() => { - throw new Error(errorMessage) - }) + command.npmInstall = jest.fn() + command.error = jest.fn() + command.runTests = jest.fn(() => { throw new Error(errorMessage) }) - await expect(command.run()).rejects.toThrow(errorMessage) + await command.run() expect(command.validateZipDirectoryStructure).toHaveBeenCalledTimes(1) expect(command.unzipFile).toHaveBeenCalledTimes(1) expect(command.validateConfig).toHaveBeenCalledTimes(2) expect(command.runTests).toHaveBeenCalledTimes(1) + expect(command.npmInstall).toHaveBeenCalledTimes(1) + expect(command.error).toHaveBeenCalledTimes(1) + + expect(command.error).toHaveBeenCalledWith(errorMessage) }) test('flag --output', async () => { @@ -327,12 +362,17 @@ describe('run', () => { command.unzipFile = jest.fn() command.validateConfig = jest.fn() command.runTests = jest.fn() + command.npmInstall = jest.fn() + command.error = jest.fn() + await command.run() expect(command.validateZipDirectoryStructure).toHaveBeenCalledTimes(1) expect(command.unzipFile).toHaveBeenCalledTimes(1) expect(command.validateConfig).toHaveBeenCalledTimes(2) expect(command.runTests).toHaveBeenCalledTimes(1) + expect(command.npmInstall).toHaveBeenCalledTimes(1) + expect(command.error).toHaveBeenCalledTimes(0) expect(fakeCwd).toEqual(path.resolve('my-dest-folder')) }) })