Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: jest testing #29

Merged
merged 8 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dev.vars.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
WEBHOOK_PROXY_URL=https://smee.io/new
WEBHOOK_SECRET=xxxxxx
APP_ID=123456
24 changes: 24 additions & 0 deletions .github/workflows/bun-testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Run Bun testing suite
on:
workflow_dispatch:
pull_request_target:
types: [ opened, synchronize ]

env:
NODE_ENV: "test"

jobs:
testing:
permissions: write-all
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v4
with:
node-version: '20.10.0'
- uses: actions/checkout@master
with:
fetch-depth: 0
- name: Build & Run test suite
run: |
bun i
bun test --coverage
34 changes: 0 additions & 34 deletions .github/workflows/jest-testing.yml

This file was deleted.

Binary file modified bun.lockb
Binary file not shown.
12 changes: 0 additions & 12 deletions jest.config.ts

This file was deleted.

10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"scripts": {
"dev": "run-p worker proxy",
"predev": "lsof -i tcp:8787 | grep LISTEN | awk '{print $2}' | xargs kill -9",
"predev": "lsof -i tcp:8787 | grep LISTEN | awk '{print $2}' | (if [ -n \"$(awk '{print $2}')\" ]; then xargs kill -9; fi)",
0x4007 marked this conversation as resolved.
Show resolved Hide resolved
"format": "run-s format:lint format:prettier format:cspell",
"format:lint": "eslint --fix .",
"format:prettier": "prettier --write .",
Expand All @@ -22,8 +22,7 @@
"worker": "wrangler dev --env dev --port 8787",
"proxy": "tsx src/proxy.ts",
"knip": "knip --config .github/knip.ts",
"knip-ci": "knip --no-exit-code --reporter json --config .github/knip.ts",
"test": "jest --setupFiles dotenv/config --coverage"
"knip-ci": "knip --no-exit-code --reporter json --config .github/knip.ts"
},
"keywords": [
"typescript",
Expand All @@ -50,7 +49,6 @@
"@cspell/dict-software-terms": "^3.3.18",
"@cspell/dict-typescript": "^3.1.2",
"@mswjs/data": "0.16.1",
"@types/jest": "29.5.12",
"@types/node": "^20.11.19",
"@typescript-eslint/eslint-plugin": "^7.0.1",
"@typescript-eslint/parser": "^7.0.1",
Expand All @@ -61,14 +59,10 @@
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-sonarjs": "^0.24.0",
"husky": "^9.0.11",
"jest": "29.7.0",
"jest-junit": "16.0.0",
"knip": "^5.0.1",
"lint-staged": "^15.2.2",
"npm-run-all": "^4.1.5",
"prettier": "^3.2.5",
"ts-jest": "29.1.2",
"ts-node": "10.9.2",
"tsx": "^4.7.1",
"typescript": "^5.3.3",
"wrangler": "^3.23.0"
Expand Down
13 changes: 12 additions & 1 deletion src/github/types/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { Type as T, type Static } from "@sinclair/typebox";

export const envSchema = T.Object({ WEBHOOK_SECRET: T.String(), APP_ID: T.String(), PRIVATE_KEY: T.String() });
export const envSchema = T.Object({ WEBHOOK_SECRET: T.String({ minLength: 1 }), APP_ID: T.String({ minLength: 1 }), PRIVATE_KEY: T.String({ minLength: 1 }) });

export type Env = Static<typeof envSchema> & {
PLUGIN_CHAIN_STATE: KVNamespace;
};

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace NodeJS {
interface ProcessEnv {
APP_ID: string;
WEBHOOK_SECRET: string;
PRIVATE_KEY: string;
}
}
}
17 changes: 1 addition & 16 deletions src/github/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}

export const pluginOutputSchema = Type.Object({
state_id: Type.String(), // Github forces snake_case
state_id: Type.String(), // GitHub forces snake_case
output: jsonString(),
});

Expand Down Expand Up @@ -55,18 +55,3 @@
inputs: DelegatedComputeInputs[];
outputs: PluginOutput[];
};

// convert top level properties to string
export function convertToString(obj: Record<string, unknown>): Record<string, string> {
const newObj: Record<string, string> = {};
for (let i = 0; i < Object.keys(obj).length; i++) {
const key = Object.keys(obj)[i];
const val = obj[key];
if (typeof val === "string") {
newObj[key] = val;
} else {
newObj[key] = JSON.stringify(val);
}
}
return newObj;
}
79 changes: 62 additions & 17 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,69 @@
// import { mainModule } from "../static/main";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "@jest/globals";
import { db } from "./__mocks__/db";
/* eslint-disable @typescript-eslint/naming-convention */

// @ts-expect-error package name is correct, TypeScript doesn't recognize it
import { afterAll, afterEach, beforeAll, describe, expect, it, jest, mock, spyOn } from "bun:test";
mock.module("@octokit/webhooks", () => ({
Webhooks: WebhooksMocked,
}));

class WebhooksMocked {
constructor(_: unknown) {}
verifyAndReceive(_: unknown) {
return Promise.resolve();
}
onAny(_: unknown) {}
on(_: unknown) {}
onError(_: unknown) {}
removeListener(_: unknown, __: unknown) {}
sign(_: unknown) {}
verify(_: unknown, __: unknown) {}
receive(_: unknown) {}
}

import { config } from "dotenv";
import worker from "../src/worker";
import { server } from "./__mocks__/node";
import usersGet from "./__mocks__/users-get.json";

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
config({ path: ".dev.vars" });

beforeAll(() => {
server.listen();
});
afterEach(() => {
server.resetHandlers();
});
afterAll(() => {
server.close();
});

describe("User tests", () => {
beforeEach(() => {
for (const item of usersGet) {
db.users.create(item);
}
describe("Worker tests", () => {
it("Should fail on missing env variables", async () => {
const req = new Request("http://localhost:8080");
const consoleSpy = spyOn(console, "error").mockImplementation(() => jest.fn());
const res = await worker.fetch(req, {
WEBHOOK_SECRET: "",
APP_ID: "",
PRIVATE_KEY: "",
PLUGIN_CHAIN_STATE: {} as KVNamespace,
});
expect(res.status).toEqual(500);
consoleSpy.mockReset();
});

it("Should fetch all the users", async () => {
const res = await fetch("https://api.ubiquity.com/users");
const data = await res.json();
expect(data).toMatchObject(usersGet);
// expect(async () => await mainModule()).not.toThrow();
it("Should start a worker", async () => {
const req = new Request("http://localhost:8080", {
headers: {
"x-github-event": "issues.opened",
"x-github-delivery": "1",
"x-hub-signature-256": "123456",
},
});
const res = await worker.fetch(req, {
WEBHOOK_SECRET: process.env.WEBHOOK_SECRET,
APP_ID: process.env.APP_ID,
PRIVATE_KEY: process.env.PRIVATE_KEY,
PLUGIN_CHAIN_STATE: {} as KVNamespace,
});
expect(res.status).toEqual(200);
});
});
Loading