Skip to content

Commit

Permalink
Add tests for the deploy command
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Oct 3, 2019
1 parent ef65a75 commit 77762eb
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Used for testing
STATICKIT_TEST_DEPLOY_KEY=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
node_modules
.env
162 changes: 139 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"yargs": "^14.0.0"
},
"devDependencies": {
"dotenv": "^8.1.0",
"execa": "^2.0.4",
"jest": "^24.9.0"
}
}
24 changes: 17 additions & 7 deletions src/cmds/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,23 @@ exports.handler = async args => {

spinner.stop();

if (response.status == 200) {
console.log(chalk.green('Deployment succeeded'));
} else if (response.status == 422) {
console.error(chalk.red('Deployment failed with errors:'));
console.error(response.data.errors);
} else {
console.error(chalk.red('Deployment failed'));
switch (response.status) {
case 200:
console.log(chalk.green('Deployment succeeded'));
return;

case 401:
console.error(chalk.red('Deploy key is not valid'));
return;

case 422:
console.error(chalk.red('Deployment failed with errors:'));
console.error(response.data.errors);
return;

default:
console.error(chalk.red('Deployment failed'));
return;
}
} catch (error) {
spinner.stop();
Expand Down
75 changes: 69 additions & 6 deletions test/deploy.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,74 @@
const parser = require('../src/parser');
const execa = require('execa');
const fs = require('fs').promises;
require('dotenv').config();

it('returns help output', async () => {
const output = await new Promise(resolve => {
parser.parse('--help', (err, argv, output) => {
resolve(output);
});
const { stdout } = await execa('bin/statickit', ['--help']);
expect(stdout).toMatch(/Performs a deployment/);
});

it('returns an error if no config is present', async () => {
try {
await execa('bin/statickit', ['deploy']);
} catch (result) {
expect(result.exitCode).toBe(1);
expect(result.stderr).toMatch(/Configuration not provided/);
}
});

it('returns an error if config is unparsable', async () => {
try {
await execa('bin/statickit', ['deploy', '-c', "'{'"]);
} catch (result) {
expect(result.exitCode).toBe(1);
expect(result.stderr).toMatch(/Configuration could not be parsed/);
}
});

it('returns an error if deploy key is not found', async () => {
try {
await execa('bin/statickit', ['deploy', '-c', "'{}'"]);
} catch (result) {
expect(result.exitCode).toBe(1);
expect(result.stderr).toMatch(/Deploy key not found/);
}
});

it('returns an error if deploy key is invalid', async () => {
try {
await execa('bin/statickit', ['deploy', '-c', "'{}'", '-k', 'invalidkey']);
} catch (result) {
console.log(result);
expect(result.exitCode).toBe(1);
expect(result.stderr).toMatch(/Deploy key is not valid/);
}
});

it('succeeds given valid params', async () => {
const { stdout } = await execa('bin/statickit', [
'deploy',
'-c',
"'{}'",
'-k',
process.env.STATICKIT_TEST_DEPLOY_KEY
]);
expect(stdout).toMatch(/Deployment succeeded/);
});

it('accepts a deploy key from env', async () => {
const { stdout } = await execa('bin/statickit', ['deploy', '-c', "'{}'"], {
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY }
});
expect(stdout).toMatch(/Deployment succeeded/);
});

it('accepts a config from the statickit.json file', async () => {
await fs.writeFile('statickit.json', '{}', 'utf8');

const { stdout } = await execa('bin/statickit', ['deploy'], {
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY }
});
expect(stdout).toMatch(/Deployment succeeded/);

expect(output).toMatch(/Performs a deployment/);
await fs.unlink('statickit.json');
});

0 comments on commit 77762eb

Please sign in to comment.