Skip to content

Commit

Permalink
test: use vitest (#802)
Browse files Browse the repository at this point in the history
* wip

* feat: roles

* ci: rename steps

* ci: do not fail fast

* ci: combine linting

* ci: combine linting

* ci: combine linting

* ci: test api

* wip

* wip

* chore: merge

* feat: ui for root key permission settings

* feat: new root keys

* feat: ui

* chore: merge

* test: use vitest

* chore(pr.yaml): update workflow file to use unit_test.yaml instead of test_packages.yaml
feat(unit_test.yaml): add unit test workflow for multiple paths using matrix strategy

* fix: tests

* fix: tests

* docs: explain how to run tests

* chore: drop strict mode

* lint: knip

* fix(test_api.yaml): change test command from 'pnpm test:e2e' to 'pnpm test:routes'
  • Loading branch information
chronark authored Jan 16, 2024
1 parent 52c4db5 commit 3526172
Show file tree
Hide file tree
Showing 84 changed files with 1,061 additions and 942 deletions.
1 change: 0 additions & 1 deletion .github/actions/install/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ runs:
shell: bash
run: |
pnpm install --recursive
npm i -g bun
npm i -g wrangler
13 changes: 6 additions & 7 deletions .github/workflows/deploy_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ env:
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }}

jobs:
test_api_worker:
test_routes:
name: Unit Test API
runs-on: ubuntu-latest

Expand Down Expand Up @@ -54,8 +54,8 @@ jobs:
uses: ./.github/actions/install


- name: Test
run: bun test src/routes --coverage --timeout 10000 --rerun-each 3
- name: Test Routes
run: pnpm test:routes
working-directory: apps/api
env:
UNKEY_ROOT_KEY: "not-empty" # this is not requried for these tests, but it is required for the env validation, I should fix this
Expand Down Expand Up @@ -104,15 +104,15 @@ jobs:
uses: ./.github/actions/install

- name: Test
run: bun test src/integration --timeout 10000 --rerun-each 10
run: pnpm test:e2e
working-directory: apps/api
env:
UNKEY_BASE_URL: https://preview-api.unkey.dev
UNKEY_ROOT_KEY: ${{ secrets.PREVIEW_ROOT_KEY }}

canary:
needs:
- test_api_worker
- test_routes
- preview-tests
environment: Canary
runs-on: ubuntu-latest
Expand All @@ -138,15 +138,14 @@ jobs:
name: End to End Test Canary
runs-on: ubuntu-latest


steps:
- uses: actions/checkout@v3

- name: Install
uses: ./.github/actions/install

- name: Test
run: bun test src/integration --timeout 10000 --rerun-each 10
run: pnpm test:e2e
working-directory: apps/api
env:
UNKEY_BASE_URL: https://canary.unkey.dev
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

test_packages:
name: Test Packages
uses: ./.github/workflows/test_packages.yaml
uses: ./.github/workflows/unit_test.yaml

build_apps:
name: Build Apps
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/test_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
jobs:

test_routes:
name: Routes
name: Test API Routes
runs-on: ubuntu-latest
services:
mysql:
Expand Down Expand Up @@ -34,8 +34,6 @@ jobs:
- name: Install
uses: ./.github/actions/install

- name: Install bun
run: npm i -g bun

- name: Load Schema into MySQL
run: pnpm drizzle-kit push:mysql
Expand All @@ -45,7 +43,7 @@ jobs:


- name: Test
run: bun test src/routes --coverage --timeout 10000 --rerun-each 3
run: pnpm test:routes
working-directory: apps/api
env:
DATABASE_HOST: localhost:8080
Expand Down
32 changes: 0 additions & 32 deletions .github/workflows/test_packages.yaml

This file was deleted.

34 changes: 34 additions & 0 deletions .github/workflows/unit_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Unit Tests
on:
workflow_call:

jobs:
test:
strategy:
fail-fast: false
matrix:
path:
- "./internal/billing"
- "./internal/hash"
- "./internal/hash"
- "./internal/keys"
- "./internal/rbac"
- "./pacakges/nextjs"
- "./packages/api"
- "./packages/hono"
- "./packages/nuxt"

name: Test ${{matrix.path}}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3


- name: Install
uses: ./.github/actions/install

- name: Build
run: pnpm turbo run build --filter=${{matrix.path}}

- name: Test
run: pnpm turbo run test --filter=${{matrix.path}}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ dist
.vscode
.dev.vars
.wrangler
.vitest
.vitest
1 change: 1 addition & 0 deletions apps/api/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.wrangler
.vitest
Binary file modified apps/api/.vitest/html.meta.json.gz
Binary file not shown.
8 changes: 6 additions & 2 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
"scripts": {
"build": "tsc",
"deploy": "pnpm build && wrangler deploy",
"dev": "pnpm build && wrangler dev"
"dev": "pnpm build && wrangler dev",
"test:unit": "vitest run -c vitest.unit.ts",
"test:routes": "vitest run -c vitest.routes.ts",
"test:e2e": "vitest run -c vitest.e2e.ts"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20231121.0",
"@unkey/tsconfig": "workspace:^",
"bun-types": "^1.0.15",
"@vitest/ui": "^1.2.0",
"typescript": "^5.3.2",
"vitest": "^0.34.3",
"wrangler": "^3.19.0"
},
"dependencies": {
Expand Down
35 changes: 19 additions & 16 deletions apps/api/src/integration/create_verify_delete_key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import type { V1ApisCreateApiRequest, V1ApisCreateApiResponse } from "@/routes/v
import type { V1KeysCreateKeyRequest, V1KeysCreateKeyResponse } from "@/routes/v1_keys_createKey";
import { V1KeysDeleteKeyRequest, V1KeysDeleteKeyResponse } from "@/routes/v1_keys_deleteKey";
import { V1KeysVerifyKeyRequest, V1KeysVerifyKeyResponse } from "@/routes/v1_keys_verifyKey";
import { expect, test } from "bun:test";
import { afterAll, expect, test } from "vitest";

const apiIds: string[] = [];

const env = integrationTestEnv.parse(process.env);
test("create, verify and delete a key", async () => {
Expand All @@ -21,6 +23,7 @@ test("create, verify and delete a key", async () => {
});
expect(createApiResponse.status).toEqual(200);
expect(createApiResponse.body.apiId).toBeDefined();
apiIds.push(createApiResponse.body.apiId);
expect(createApiResponse.headers).toHaveProperty("unkey-request-id");

const key = await step<V1KeysCreateKeyRequest, V1KeysCreateKeyResponse>({
Expand Down Expand Up @@ -80,20 +83,20 @@ test("create, verify and delete a key", async () => {
});
expect(validAfterRevoke.status).toEqual(200);
expect(validAfterRevoke.body.valid).toEqual(false);
});

/**
* Tear down
*/
const deleteApi = await step({
url: `${env.UNKEY_BASE_URL}/v1/apis.deleteApi`,
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${env.UNKEY_ROOT_KEY}`,
},
body: {
apiId: createApiResponse.body.apiId,
},
});
expect(deleteApi.status).toEqual(200);
afterAll(async () => {
for (const apiId of apiIds) {
await step({
url: `${env.UNKEY_BASE_URL}/v1/apis.deleteApi`,
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${env.UNKEY_ROOT_KEY}`,
},
body: {
apiId,
},
});
}
});
4 changes: 2 additions & 2 deletions apps/api/src/integration/list_keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { V1ApisDeleteApiRequest, V1ApisDeleteApiResponse } from "@/routes/v1_api
import type { V1ApisListKeysResponse } from "@/routes/v1_apis_listKeys";
import type { V1KeysCreateKeyRequest, V1KeysCreateKeyResponse } from "@/routes/v1_keys_createKey";
import { V1KeysDeleteKeyRequest } from "@/routes/v1_keys_deleteKey";
import { expect, test } from "bun:test";
import { expect, test } from "vitest";

const env = integrationTestEnv.parse(process.env);
test("create and list keys", async () => {
Expand Down Expand Up @@ -161,4 +161,4 @@ test("list keys does not return revoked keys", async () => {
},
});
expect(deleteApi.status).toEqual(200);
});
}, 10_000);
6 changes: 3 additions & 3 deletions apps/api/src/integration/remaining_is_consistent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { V1ApisDeleteApiRequest, V1ApisDeleteApiResponse } from "@/routes/v
import type { V1KeysCreateKeyRequest, V1KeysCreateKeyResponse } from "@/routes/v1_keys_createKey";
import { V1KeysGetKeyResponse } from "@/routes/v1_keys_getKey";
import type { V1KeysVerifyKeyRequest, V1KeysVerifyKeyResponse } from "@/routes/v1_keys_verifyKey";
import { expect, test } from "bun:test";
import { expect, test } from "vitest";

const env = integrationTestEnv.parse(process.env);

Expand Down Expand Up @@ -59,7 +59,7 @@ test("remaining consistently counts down", async () => {
});

expect(valid.status).toEqual(200);
expect(valid.body.valid).toBeTrue();
expect(valid.body.valid).toBe(true);
expect(valid.body.remaining).toEqual(i);
}

Expand All @@ -76,7 +76,7 @@ test("remaining consistently counts down", async () => {
},
});
expect(invalid.status).toEqual(200);
expect(invalid.body.valid).toBeFalse();
expect(invalid.body.valid).toBe(false);
expect(invalid.body.remaining).toEqual(0);

const key = await step<never, V1KeysGetKeyResponse>({
Expand Down
Loading

1 comment on commit 3526172

@vercel
Copy link

@vercel vercel bot commented on 3526172 Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

web – ./apps/web

unkey-web-lac.vercel.app
www.unkey.dev
web-unkey.vercel.app
web-git-main-unkey.vercel.app
unkey.dev

Please sign in to comment.