Skip to content

Commit

Permalink
test(actions): pull request action
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jul 28, 2023
1 parent badd933 commit 0e6ab89
Show file tree
Hide file tree
Showing 10 changed files with 601 additions and 39 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ module.exports = {
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
},
{
files: ['*.test.ts'],
rules: {
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
},
},
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
};
6 changes: 2 additions & 4 deletions packages/actions-new-meetup/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { test, expect } from 'vitest';
import { test } from 'vitest';

test('todo', () => {
expect('todo').toBe('todo');
});
test.todo('add tests');
3 changes: 3 additions & 0 deletions packages/actions-post-create-pr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"@actions/github": "^5.1.1",
"tsx": "^3.12.7",
"zod": "^3.21.4"
},
"devDependencies": {
"msw": "^1.2.3"
}
}
65 changes: 65 additions & 0 deletions packages/actions-post-create-pr/test/GithubAPI.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { setupServer } from 'msw/node';
import { rest } from 'msw';
import { vi } from 'vitest';

export const API_URL = 'https://api.github.com';
export const onCommentUpdated = vi.fn();

export default setupServer(
// Ref. https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#update-an-issue-comment
rest.patch(
`${API_URL}/repos/:owner/:repo/issues/comments/:comment_id`,
async (req, res, ctx) => {
const { owner, repo, comment_id } = req.params;
const body = await req.text();

onCommentUpdated({
owner,
repo,
comment_id,
body,
});

return res(
ctx.status(200),
ctx.json({
id: 1,
node_id: 'MDEyOklzc3VlQ29tbWVudDE=',
url: 'https://api.github.com/repos/octocat/Hello-World/issues/comments/1',
html_url:
'https://github.com/octocat/Hello-World/issues/1347#issuecomment-1',
body: 'Me too',
user: {
login: 'octocat',
id: 1,
node_id: 'MDQ6VXNlcjE=',
avatar_url: 'https://github.com/images/error/octocat_happy.gif',
gravatar_id: '',
url: 'https://api.github.com/users/octocat',
html_url: 'https://github.com/octocat',
followers_url: 'https://api.github.com/users/octocat/followers',
following_url:
'https://api.github.com/users/octocat/following{/other_user}',
gists_url: 'https://api.github.com/users/octocat/gists{/gist_id}',
starred_url:
'https://api.github.com/users/octocat/starred{/owner}{/repo}',
subscriptions_url:
'https://api.github.com/users/octocat/subscriptions',
organizations_url: 'https://api.github.com/users/octocat/orgs',
repos_url: 'https://api.github.com/users/octocat/repos',
events_url: 'https://api.github.com/users/octocat/events{/privacy}',
received_events_url:
'https://api.github.com/users/octocat/received_events',
type: 'User',
site_admin: false,
},
created_at: '2011-04-14T16:00:49Z',
updated_at: '2011-04-14T16:00:49Z',
issue_url:
'https://api.github.com/repos/octocat/Hello-World/issues/1347',
author_association: 'COLLABORATOR',
}),
);
},
),
);
51 changes: 48 additions & 3 deletions packages/actions-post-create-pr/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
import { test, expect } from 'vitest';
import { test, expect, vi } from 'vitest';
import { onCommentUpdated } from './GithubAPI.mock';

test('todo', () => {
expect('todo').toBe('todo');
vi.mock('@actions/github', async () => {
const actual = await vi.importActual<typeof import('@actions/github')>(
'@actions/github',
);

return {
...actual,
context: {
repo: {
owner: 'test-owner',
repo: 'test-repo',
},
},
};
});

test('sends pull request', async () => {
const owner = 'test-owner';
const repo = 'test-repo';
const comment_id = '456';

vi.stubEnv('PULL_REQUEST_NUMBER', '123');
vi.stubEnv('COMMENT_ID', comment_id);
vi.stubEnv('GITHUB_TOKEN', 'test-github-token');

await import('../src/index');

expect(onCommentUpdated).toHaveBeenCalledTimes(1);
expect(onCommentUpdated).toHaveBeenCalledWith({
owner,
repo,
comment_id,
body: expect.any(String) as string,
});

const { body } = JSON.parse(onCommentUpdated.mock.calls[0][0].body);
expect(body).toMatchInlineSnapshot(`
"
Hi there! Thanks for creating a new meetup. I'm going to create a new branch and pull request for you.
1. Validating meetup details... Done! ✅
2. Creating meetup file... Done! ✅
3. Creating new branch and pull request... Done! ✅
Here's the new pull request: #123"
`);
});
7 changes: 7 additions & 0 deletions packages/actions-post-create-pr/test/msw.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { afterAll, afterEach, beforeAll } from 'vitest';

import server from './GithubAPI.mock';

beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
afterAll(() => server.close());
afterEach(() => server.resetHandlers());
6 changes: 6 additions & 0 deletions packages/actions-post-create-pr/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { defineConfig } from 'vitest/config';

const __filename = fileURLToPath(import.meta.url);
const __dirname = resolve(__filename, '..');

export default defineConfig({
test: {
name: 'actions-post-create-pr',
setupFiles: [resolve(__dirname, './test/msw.setup.ts')],
},
});
Loading

0 comments on commit 0e6ab89

Please sign in to comment.