From 97acacd1caf38879c90bdef57fbe676343c1f2a2 Mon Sep 17 00:00:00 2001 From: steebchen Date: Tue, 5 Mar 2024 23:52:01 +0700 Subject: [PATCH] feat(test): add e2e tests --- .github/workflows/tests.yml | 36 +++++++++++++++++-- examples/simple-worker.e2e.ts | 68 +++++++++++++++++++++++++++++++++++ package.json | 9 +++-- 3 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 examples/simple-worker.e2e.ts diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3bb78d8..caa7434 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -2,7 +2,7 @@ name: tests on: pull_request jobs: - run-tests: + unit: runs-on: ubuntu-latest steps: - name: Checkout @@ -29,5 +29,35 @@ jobs: - name: Install dependencies run: pnpm install - - name: Jest tests - run: pnpm test + - name: Unit tests + run: pnpm test:unit + + e2e: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Install pnpm + run: npm install -g pnpm@8 + + - name: Get pnpm store directory + shell: bash + run: | + echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV + + - name: Setup pnpm cache + uses: actions/cache@v4 + with: + path: ${{ env.STORE_PATH }} + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('pnpm-lock.yaml') }} + restore-keys: | + ${{ runner.os }}-pnpm-store- + + - name: Install dependencies + run: pnpm install + + - name: E2E tests + run: pnpm test:e2e diff --git a/examples/simple-worker.e2e.ts b/examples/simple-worker.e2e.ts new file mode 100644 index 0000000..62edc0b --- /dev/null +++ b/examples/simple-worker.e2e.ts @@ -0,0 +1,68 @@ +import { Workflow } from '../src'; +import sleep from '../src/util/sleep'; +import Hatchet from '../src/sdk'; + +describe('e2e', () => { + let hatchet: Hatchet; + + beforeEach(() => { + hatchet = Hatchet.init(); + }); + + describe('workflow', () => { + it('should pass', async () => { + let invoked = 0; + + const workflow: Workflow = { + id: 'simple-e2e-workflow', + description: 'test', + on: { + event: 'user:create', + }, + steps: [ + { + name: 'step1', + run: async (ctx) => { + console.log('starting step1 with the following input', ctx.workflowInput()); + invoked += 1; + return { step1: 'step1 results!' }; + }, + }, + { + name: 'step2', + parents: ['step1'], + run: (ctx) => { + console.log('executed step2 after step1 returned ', ctx.stepOutput('step1')); + invoked += 1; + return { step2: 'step2 results!' }; + }, + }, + ], + }; + + console.log('starting worker...'); + + const worker = await hatchet.worker('example-worker'); + console.log('registering workflow...'); + await worker.registerWorkflow(workflow); + + console.log('worker started.'); + + void worker.start(); + + await sleep(20000); + + console.log('pushing event...'); + + await hatchet.event.push('user:create', { + test: 'test', + }); + + await sleep(5000); + + expect(invoked).toEqual(2); + + // await worker.stop(); + }, 60000); + }); +}); diff --git a/package.json b/package.json index f482306..5cd7a17 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,9 @@ "files": [ "*", "!**/*.test.js", - "!**/*.test.d.ts" + "!**/*.test.d.ts", + "!**/*.e2e.js", + "!**/*.e2e.d.ts" ], "repository": { "type": "git", @@ -17,8 +19,9 @@ "build": "echo 'build hatchet sdk with `npn run tsc:build` to ensure it is not build during the publish step' && exit 0", "prepare": "npm run build", "tsc:build": "tsc && resolve-tspaths", - "test": "jest", - "test:watch": "jest --watch", + "test:unit": "jest --testMatch='**/*.test.ts'", + "test:e2e": "jest --testMatch='**/*.e2e.ts'", + "test:unit:watch": "jest --testMatch='**/*.test.ts' --watch", "generate": "pnpm run '/generate-.*/'", "generate-api": "npx --yes swagger-cli bundle ./hatchet/api-contracts/openapi/openapi.yaml --outfile openapi.yaml --type yaml && npx swagger-typescript-api -p openapi.yaml -o src/clients/rest/generated -n hatchet.ts --modular --axios", "generate-protoc": "./generate-protoc.sh",