|
1 | | -// You can import your modules |
2 | | -// import index from '../src/index' |
| 1 | +// tests/handlers.test.ts |
| 2 | +import { test, expect, beforeEach, afterEach } from 'vitest'; |
| 3 | +import nock from 'nock'; |
| 4 | +import { Probot, ProbotOctokit } from 'probot'; |
| 5 | +import myProbotApp from '../src'; |
3 | 6 |
|
4 | | -import nock from "nock"; |
5 | | -// Requiring our app implementation |
6 | | -import myProbotApp from "../src/index.js"; |
7 | | -import { Probot, ProbotOctokit } from "probot"; |
8 | | -// Requiring our fixtures |
9 | | -//import payload from "./fixtures/issues.opened.json" with { "type": "json"}; |
10 | | -import fs from "fs"; |
11 | | -import path from "path"; |
12 | | -import { fileURLToPath } from "url"; |
13 | | -import { describe, beforeEach, afterEach, test, expect } from "vitest"; |
| 7 | +const issuePayload = require('./fixtures/issues.opened.json'); |
| 8 | +const pullRequestPayload = require('./fixtures/pull_request.opened.json'); |
14 | 9 |
|
15 | | -const issueCreatedBody = { body: "Thanks for opening this issue!" }; |
| 10 | +let probot: Probot; |
16 | 11 |
|
17 | | -const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 12 | +beforeEach(() => { |
| 13 | + probot = new Probot({ |
| 14 | + appId: 123, |
| 15 | + privateKey: 'test', |
| 16 | + octokit: ProbotOctokit.defaults({ |
| 17 | + throttle: { enabled: false }, |
| 18 | + retry: { enabled: false }, |
| 19 | + }), |
| 20 | + }); |
| 21 | + probot.load(myProbotApp); |
| 22 | +}); |
18 | 23 |
|
19 | | -const privateKey = fs.readFileSync( |
20 | | - path.join(__dirname, "fixtures/mock-cert.pem"), |
21 | | - "utf-8", |
22 | | -); |
| 24 | +afterEach(() => { |
| 25 | + nock.cleanAll(); |
| 26 | +}); |
23 | 27 |
|
24 | | -const payload = JSON.parse( |
25 | | - fs.readFileSync(path.join(__dirname, "fixtures/issues.opened.json"), "utf-8"), |
26 | | -); |
| 28 | +test('sends a notification when an issue is opened', async () => { |
| 29 | + nock('https://hooks.slack.com') |
| 30 | + .post('/services/your/slack/webhook') |
| 31 | + .reply(200); |
27 | 32 |
|
28 | | -describe("My Probot app", () => { |
29 | | - let probot: any; |
| 33 | + nock('https://discord.com') |
| 34 | + .post('/api/webhooks/your/discord/webhook') |
| 35 | + .reply(200); |
30 | 36 |
|
31 | | - beforeEach(() => { |
32 | | - nock.disableNetConnect(); |
33 | | - probot = new Probot({ |
34 | | - appId: 937788, |
35 | | - privateKey, |
36 | | - // disable request throttling and retries for testing |
37 | | - Octokit: ProbotOctokit.defaults({ |
38 | | - retry: { enabled: false }, |
39 | | - throttle: { enabled: false }, |
40 | | - }), |
41 | | - }); |
42 | | - // Load our app into probot |
43 | | - probot.load(myProbotApp); |
| 37 | + const transporter = { |
| 38 | + sendMail: vi.fn().mockResolvedValueOnce({}), |
| 39 | + }; |
| 40 | + vi.mock('nodemailer', () => { |
| 41 | + return { |
| 42 | + createTransport: () => transporter, |
| 43 | + }; |
44 | 44 | }); |
45 | 45 |
|
46 | | - test("creates a comment when an issue is opened", async () => { |
47 | | - const mock = nock("https://api.github.com") |
48 | | - // Test that we correctly return a test token |
49 | | - .post("/app/installations/2/access_tokens") |
50 | | - .reply(200, { |
51 | | - token: "test", |
52 | | - permissions: { |
53 | | - issues: "write", |
54 | | - }, |
55 | | - }) |
| 46 | + await probot.receive({ name: 'issues', payload: issuePayload }); |
56 | 47 |
|
57 | | - // Test that a comment is posted |
58 | | - .post("/repos/hiimbex/testing-things/issues/1/comments", (body: any) => { |
59 | | - expect(body).toMatchObject(issueCreatedBody); |
60 | | - return true; |
61 | | - }) |
62 | | - .reply(200); |
| 48 | + expect(transporter.sendMail).toHaveBeenCalled(); |
| 49 | +}); |
63 | 50 |
|
64 | | - // Receive a webhook event |
65 | | - await probot.receive({ name: "issues", payload }); |
| 51 | +test('sends a notification when a pull request is opened', async () => { |
| 52 | + nock('https://hooks.slack.com') |
| 53 | + .post('/services/your/slack/webhook') |
| 54 | + .reply(200); |
66 | 55 |
|
67 | | - expect(mock.pendingMocks()).toStrictEqual([]); |
68 | | - }); |
| 56 | + nock('https://discord.com') |
| 57 | + .post('/api/webhooks/your/discord/webhook') |
| 58 | + .reply(200); |
69 | 59 |
|
70 | | - afterEach(() => { |
71 | | - nock.cleanAll(); |
72 | | - nock.enableNetConnect(); |
| 60 | + const transporter = { |
| 61 | + sendMail: vi.fn().mockResolvedValueOnce({}), |
| 62 | + }; |
| 63 | + vi.mock('nodemailer', () => { |
| 64 | + return { |
| 65 | + createTransport: () => transporter, |
| 66 | + }; |
73 | 67 | }); |
74 | | -}); |
75 | 68 |
|
76 | | -// For more information about testing with Jest see: |
77 | | -// https://facebook.github.io/jest/ |
| 69 | + await probot.receive({ name: 'pull_request', payload: pullRequestPayload }); |
78 | 70 |
|
79 | | -// For more information about using TypeScript in your tests, Jest recommends: |
80 | | -// https://github.com/kulshekhar/ts-jest |
81 | | - |
82 | | -// For more information about testing with Nock see: |
83 | | -// https://github.com/nock/nock |
| 71 | + expect(transporter.sendMail).toHaveBeenCalled(); |
| 72 | +}); |
0 commit comments