Skip to content

Commit

Permalink
test: Add failure testing (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
peaceiris authored Jan 14, 2020
1 parent 85b1691 commit 6693991
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
60 changes: 59 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import * as main from '../src/main';
const nock = require('nock');
import {FetchError} from 'node-fetch';
import jsonTestBrew from './data/brew.json';
// import jsonTestGithub from './data/github.json';

jest.setTimeout(30000);
const repo: string = 'mdbook';

beforeEach(() => {
jest.resetModules();
});

afterEach(() => {
delete process.env['INPUT_MDBOOK-VERSION'];
nock.cleanAll();
});

describe('Integration testing run()', () => {
Expand All @@ -21,8 +27,60 @@ describe('Integration testing run()', () => {
test('succeed in installing the latest version', async () => {
const testVersion: string = 'latest';
process.env['INPUT_MDBOOK-VERSION'] = testVersion;
nock('https://formulae.brew.sh')
.get(`/api/formula/${repo}.json`)
.reply(200, jsonTestBrew);
const result: main.actionResult = await main.run();
expect(result.output).toMatch(/mdbook v/);
expect(result.output).toMatch('mdbook v0.3.5');
});

test('fail to install a custom version due to 404 of tarball', async () => {
const testVersion: string = '0.3.4';
process.env['INPUT_MDBOOK-VERSION'] = testVersion;
nock('https://github.com')
.get(
`/rust-lang/mdBook/releases/download/v${testVersion}/mdbook-v${testVersion}-x86_64-unknown-linux-gnu.tar.gz`
)
.reply(404);
try {
const result: main.actionResult = await main.run();
console.debug(result.output);
} catch (e) {
expect(e).toThrow(FetchError);
}
});

test('fail to install the latest version due to 404 of brew.sh', async () => {
const testVersion: string = 'latest';
process.env['INPUT_MDBOOK-VERSION'] = testVersion;
nock('https://formulae.brew.sh')
.get(`/api/formula/${repo}.json`)
.reply(404);
try {
const result: main.actionResult = await main.run();
console.debug(result.output);
} catch (e) {
expect(e).toThrow(FetchError);
}
});

test('fail to install the latest version due to 404 of tarball', async () => {
const testVersion: string = 'latest';
process.env['INPUT_MDBOOK-VERSION'] = testVersion;
nock('https://formulae.brew.sh')
.get(`/api/formula/${repo}.json`)
.reply(200, jsonTestBrew);
nock('https://github.com')
.get(
`/rust-lang/mdBook/releases/download/v0.3.5/mdbook-v0.3.5-x86_64-unknown-linux-gnu.tar.gz`
)
.reply(404);
try {
const result: main.actionResult = await main.run();
console.debug(result.output);
} catch (e) {
expect(e).toThrow(FetchError);
}
});
});

Expand Down
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export async function run(): Promise<any> {
result = await showVersion('mdbook', ['--version']);

return result;
} catch (error) {
core.setFailed(error.message);
} catch (e) {
core.setFailed(`Action failed with error ${e}`);
return e;
}
}

0 comments on commit 6693991

Please sign in to comment.