-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from shlomimatichin/unittest
Added good flow unit tests
- Loading branch information
Showing
11 changed files
with
321 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: CI | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v2-beta | ||
with: | ||
node-version: '12' | ||
- name: Install dependencies | ||
run: | | ||
npm i | ||
- name: Style check | ||
run: | | ||
npx prettier --check "*.ts" "lib/*.ts" | ||
- name: Unit test | ||
run: | | ||
npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"semi": true, | ||
"singleQuote": true, | ||
"printWidth": 120, | ||
"trailingComma": "none", | ||
"parser": "typescript", | ||
"proseWrap": "preserve", | ||
"quoteProps": "as-needed", | ||
"jsxBracketSameLine": true, | ||
"arrowParens": "avoid" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"recommendations": [ | ||
"christian-kohler.npm-intellisense", | ||
"dbaeumer.vscode-eslint", | ||
"eg2.vscode-npm-script", | ||
"esbenp.prettier-vscode", | ||
"lannonbr.vscode-js-annotations", | ||
"ms-vscode.vscode-typescript-tslint-plugin", | ||
"orta.vscode-jest" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const self = { | ||
error: jest.fn(), | ||
info: jest.fn(), | ||
debug: jest.fn(), | ||
warn: jest.fn(), | ||
getInput: jest.fn(), | ||
addPath: jest.fn(), | ||
} | ||
export default self; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const self = { | ||
downloadTool: jest.fn(), | ||
extractTar: jest.fn(), | ||
} | ||
export default self; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testPathIgnorePatterns: [ | ||
"<rootDir>/dist/", | ||
"<rootDir>/node_modules/" | ||
], | ||
resetMocks: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import axios from 'axios'; | ||
import childProcess from 'child_process'; | ||
import core from '@actions/core'; | ||
import tc from '@actions/tool-cache'; | ||
jest.mock('axios'); | ||
jest.mock('child_process'); | ||
const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
const mockedChildProcess = childProcess as jest.Mocked<typeof childProcess>; | ||
const mockedCore = core as jest.Mocked<typeof core>; | ||
const mockedTC = tc as jest.Mocked<typeof tc>; | ||
|
||
import run from './terratag-action'; | ||
|
||
describe('terratag action', () => { | ||
beforeEach(() => { | ||
mockedTC.downloadTool.mockResolvedValue('FAKE PATH FOR DOWNLOADED TOOL TAR'); | ||
mockedTC.extractTar.mockResolvedValue('FAKE PATH FOR EXTRACTED'); | ||
const spawn = { | ||
stdout: { on: jest.fn() }, | ||
stderr: { on: jest.fn() }, | ||
on: jest.fn() | ||
}; | ||
spawn.on.mockImplementation((eventName: string, callback: (code: number) => void) => { | ||
expect(eventName).toBe('close'); | ||
callback(0); | ||
}); | ||
mockedChildProcess.spawn.mockReturnValue(spawn as any); | ||
}); | ||
|
||
describe('simple end to end, latest terratag', () => { | ||
beforeEach(async () => { | ||
mockedCore.getInput.mockImplementation((name: string) => { | ||
const inputs: { [key: string]: string } = { terratagVersion: 'latest', tags: JSON.stringify({ a: 'b' }) }; | ||
return inputs[name]; | ||
}); | ||
mockedAxios.get.mockResolvedValue({ | ||
status: 200, | ||
data: `href="/env0/terratag/releases/tag/v1.2.3"` | ||
}); | ||
await run(); | ||
}); | ||
it('shoud query which version of terratag is latest', () => { | ||
expect(mockedAxios.get.mock.calls).toEqual([['https://github.com/env0/terratag/releases']]); | ||
}); | ||
it('should download terratag from expected url', () => { | ||
expect(mockedTC.downloadTool.mock.calls).toEqual([ | ||
['https://github.com/env0/terratag/releases/download/v1.2.3/terratag_1.2.3_linux_amd64.tar.gz'] | ||
]); | ||
}); | ||
it('should extract downloaded terratag', () => { | ||
expect(mockedCore.addPath.mock.calls).toEqual([['FAKE PATH FOR EXTRACTED']]); | ||
}); | ||
it('should extract downloaded terratag', () => { | ||
expect(mockedTC.extractTar.mock.calls).toEqual([['FAKE PATH FOR DOWNLOADED TOOL TAR']]); | ||
}); | ||
it('should execute terratag with the correct cli arguments', () => { | ||
expect(mockedChildProcess.spawn.mock.calls).toEqual([['FAKE PATH FOR EXTRACTED/terratag', ['-tags={"a":"b"}']]]); | ||
}); | ||
}); | ||
|
||
describe('simple end to end, specific terratag', () => { | ||
beforeEach(async () => { | ||
mockedCore.getInput.mockImplementation((name: string) => { | ||
const inputs: { [key: string]: string } = { terratagVersion: '5.6.7', tags: JSON.stringify({ a: 'b' }) }; | ||
return inputs[name]; | ||
}); | ||
mockedAxios.get.mockRejectedValue('Should not be called'); | ||
await run(); | ||
}); | ||
it('shoud NOT query which terratag versions', () => { | ||
expect(mockedAxios.get.mock.calls).toEqual([]); | ||
}); | ||
it('should download terratag from expected url', () => { | ||
expect(mockedTC.downloadTool.mock.calls).toEqual([ | ||
['https://github.com/env0/terratag/releases/download/v5.6.7/terratag_5.6.7_linux_amd64.tar.gz'] | ||
]); | ||
}); | ||
it('should extract downloaded terratag', () => { | ||
expect(mockedCore.addPath.mock.calls).toEqual([['FAKE PATH FOR EXTRACTED']]); | ||
}); | ||
it('should extract downloaded terratag', () => { | ||
expect(mockedTC.extractTar.mock.calls).toEqual([['FAKE PATH FOR DOWNLOADED TOOL TAR']]); | ||
}); | ||
it('should execute terratag with the correct cli arguments', () => { | ||
expect(mockedChildProcess.spawn.mock.calls).toEqual([['FAKE PATH FOR EXTRACTED/terratag', ['-tags={"a":"b"}']]]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.