diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..fb9cdeb --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,39 @@ +name: Test + +on: + pull_request: + types: [opened, synchronize] + paths: + - '**/*.[tj]s' + - '**/*.?[tj]s' + - 'tsconfig.json' + - 'package.json' + - 'package-lock.json' + - '.nvmrc' + - '.github/workflows/test.yml' + +jobs: + test: + runs-on: blacksmith-2vcpu-ubuntu-2404 + steps: + - name: Checkout + # 6.0.3 + uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 + + # v6.1.0 + - name: Setup Node + uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f + with: + node-version-file: .nvmrc + cache: npm + cache-dependency-path: package-lock.json + + - name: Install npm packages + run: npm ci + + - name: Run unit tests + run: npm test + + - name: Tell people how to run the failing check + if: failure() + run: echo "::error::The test check failed! To run it locally, run \`npm ci && npm test\` from the GitHub-Actions repo root." diff --git a/package.json b/package.json index 557feb7..5220491 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "scripts": { "fmt": "oxfmt --write .", "typecheck": "tsgo --noEmit -p tsconfig.json", - "verify-peer-review": "tsx scripts/verifyPeerReview.ts" + "verify-peer-review": "tsx scripts/verifyPeerReview.ts", + "test": "tsx --test tests/*.test.ts" }, "dependencies": { "expensify-common": "2.0.189" diff --git a/tests/verifyPeerReviewCli.test.ts b/tests/verifyPeerReviewCli.test.ts new file mode 100644 index 0000000..b906c13 --- /dev/null +++ b/tests/verifyPeerReviewCli.test.ts @@ -0,0 +1,33 @@ +import assert from 'node:assert/strict'; +import {afterEach, beforeEach, describe, it} from 'node:test'; + +import VerifyPeerReview from '../scripts/verifyPeerReview'; + +const ORIGINAL_ARGV = process.argv; + +describe('main CLI parsing', () => { + let originalExit: typeof process.exit; + + beforeEach(() => { + process.argv = ['tsx', 'scripts/verifyPeerReview.ts']; + originalExit = process.exit.bind(process); + process.exit = (code?: string | number | null) => { + throw new Error(`exit ${code ?? 0}`); + }; + }); + + afterEach(() => { + process.argv = ORIGINAL_ARGV; + process.exit = originalExit; + }); + + it('parses required pull request CLI arguments', async () => { + process.argv.push('--owner', 'Expensify', '--repo', 'Auth', '--pull-request-number', '21136', '--base-ref', 'main'); + + await assert.doesNotReject(() => VerifyPeerReview.main()); + }); + + it('fails when required arguments are missing', async () => { + await assert.rejects(() => VerifyPeerReview.main(), /exit 1/); + }); +});