generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 19
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 #29 from FernandVEYRIER/feat/jest
feat: jest testing
- Loading branch information
Showing
9 changed files
with
102 additions
and
88 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
WEBHOOK_PROXY_URL=https://smee.io/new | ||
WEBHOOK_SECRET=xxxxxx | ||
APP_ID=123456 |
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,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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
}); | ||
}); |