Skip to content

Commit

Permalink
Add Jest configuration and update test script
Browse files Browse the repository at this point in the history
  • Loading branch information
Jagoda11 committed Mar 29, 2024
1 parent 60fa3d3 commit 4904de0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 52 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ module.exports = {
commonjs: true,
es2021: true,
node: true,
//'jest/globals': true,
'jest/globals': true,
},
extends: [
'eslint:recommended',
'plugin:import/errors',
'plugin:import/warnings',
],
//plugins: ['jest'],
plugins: ['jest'],
rules: {
'no-shadow': 'error',
eqeqeq: ['error', 'always'],
Expand Down
7 changes: 7 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// jest.config.cjs
module.exports = {
testEnvironment: 'node',
transform: {
'^.+\\.js$': 'babel-jest',
},
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"scripts": {
"start": "echo '🚀 Starting the app...' && probot run ./index.js",
"test": "echo '🧪 Running tests...' && node --test",
"test": "echo '🧪 Running tests...' && jest --config jest.config.cjs",
"lint": "echo '🧹 Linting code...' && eslint './**/*.js'"
},
"dependencies": {
Expand Down
95 changes: 46 additions & 49 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ import myProbotApp from '../index.js';
import { Probot, ProbotOctokit } from 'probot';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { describe, beforeEach, afterEach, test } from 'node:test';
import assert from 'node:assert';
import assert from 'assert';
import { config } from 'dotenv';
config();

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const privateKey = process.env.PRIVATE_KEY;

const issuePayload = JSON.parse(
fs.readFileSync(path.join(__dirname, 'fixtures/issues.opened.json'), 'utf-8')
const issuePayloadPath = path.join(
__dirname,
'..',
'test',
'fixtures',
'issues.opened.json'
);
const issuePayload = JSON.parse(fs.readFileSync(issuePayloadPath, 'utf-8'));

const issueCreatedBody = { body: '👋 Thanks for opening this issue! 🙌 🎉 🚀' };

Expand All @@ -26,7 +25,7 @@ describe('My Probot app', () => {
nock.disableNetConnect();
probot = new Probot({
appId: 123,
privateKey,
privateKey: process.env.PRIVATE_KEY,
Octokit: ProbotOctokit.defaults({
retry: { enabled: false },
throttle: { enabled: false },
Expand All @@ -36,65 +35,63 @@ describe('My Probot app', () => {
});

test('🎉 creates a comment when an issue is opened 🚀', async () => {
const mock = nock('https://api.github.com')
nock('https://api.github.com')
.post('/app/installations/2/access_tokens')
.reply(200, { token: 'test', permissions: { issues: 'write' } })
.reply(200, { token: 'test', permissions: { issues: 'write' } });

nock('https://api.github.com')
.post('/repos/hiimbex/testing-things/issues/1/comments', (body) => {
assert.deepEqual(body, issueCreatedBody);
return true;
})
.reply(200);

await probot.receive({ name: 'issues', payload: issuePayload });

assert.deepStrictEqual(mock.pendingMocks(), []);
assert.ok(nock.isDone());
});

test('❌🚀 handles issue creation failure 🚀❌', async () => {
const mock = nock('https://api.github.com')
nock('https://api.github.com')
.post('/app/installations/2/access_tokens')
.reply(200, { token: 'test', permissions: { issues: 'write' } })
.post('/repos/hiimbex/testing-things/issues/1/comments', (body) => {
assert.deepEqual(body, issueCreatedBody);
return true;
})
.reply(200, { token: 'test', permissions: { issues: 'write' } });

nock('https://api.github.com')
.post('/repos/hiimbex/testing-things/issues/1/comments')
.reply(500); // Simulate an API failure

// Declare errorWasThrown variable
let errorWasThrown = false;

// Receive a webhook event
try {
await probot.receive({ name: 'issues', payload: issuePayload });
} catch (error) {
errorWasThrown = true;
}
assert.strictEqual(errorWasThrown, true);
assert.deepStrictEqual(mock.pendingMocks(), []);
await assert.rejects(
async () => {
await probot.receive({ name: 'issues', payload: issuePayload });
},
(error) => {
assert(error instanceof Error);
return true;
}
);

assert.ok(nock.isDone());
});

test('🕵️‍♀️ handles issue not found error 🕵️‍♀️', async () => {
const mock = nock('https://api.github.com')
nock('https://api.github.com')
.post('/app/installations/2/access_tokens')
.reply(200, { token: 'test', permissions: { issues: 'write' } })
.post('/repos/hiimbex/testing-things/issues/1/comments', (body) => {
assert.deepEqual(body, issueCreatedBody);
.reply(200, { token: 'test', permissions: { issues: 'write' } });

nock('https://api.github.com')
.post('/repos/hiimbex/testing-things/issues/1/comments')
.reply(404); // Simulate a "Not Found" error

await assert.rejects(
async () => {
await probot.receive({ name: 'issues', payload: issuePayload });
},
(error) => {
assert(error instanceof Error);
return true;
})
.reply(404); // Simulate an API failure with a "Not Found" error

// Declare errorWasThrown variable
let errorWasThrown = false;

// Receive a webhook event
try {
await probot.receive({ name: 'issues', payload: issuePayload });
} catch (error) {
errorWasThrown = true;
}
}
);

assert.strictEqual(errorWasThrown, true);
assert.deepStrictEqual(mock.pendingMocks(), []);
assert.ok(nock.isDone());
});

afterEach(() => {
Expand Down

0 comments on commit 4904de0

Please sign in to comment.