Skip to content

Commit

Permalink
test: vitest workspace (#26)
Browse files Browse the repository at this point in the history
* test: vitest workspace

* test(actions): pull request action
  • Loading branch information
AriPerkkio committed Jul 28, 2023
1 parent b618244 commit aa746b0
Show file tree
Hide file tree
Showing 20 changed files with 1,049 additions and 111 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: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "oulu-dev-meetups-monorepo",
"private": true,
"type": "module",
"version": "0.0.1",
"description": "TODO",
"packageManager": "pnpm@8.6.2",
"scripts": {
"test": "pnpm -r --parallel run test",
"test": "vitest",
"build": "pnpm -r run build",
"lint": "eslint . --max-warnings 0 --ext .astro,.ts,.js",
"typecheck": "pnpm -r --parallel run typecheck"
Expand All @@ -21,6 +22,7 @@
"eslint-plugin-unicorn": "^48.0.1",
"prettier": "^3.0.0",
"prettier-plugin-astro": "^0.11.0",
"typescript": "^5.1.6"
"typescript": "^5.1.6",
"vitest": "^0.33.0"
}
}
1 change: 1 addition & 0 deletions packages/actions-new-meetup/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "new-meetup-action",
"private": true,
"type": "module",
"version": "0.0.1",
"scripts": {
"start": "tsx src/index.ts",
Expand Down
3 changes: 3 additions & 0 deletions packages/actions-new-meetup/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from 'vitest';

test.todo('add tests');
7 changes: 7 additions & 0 deletions packages/actions-new-meetup/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
name: 'actions-new-meetup',
},
});
5 changes: 5 additions & 0 deletions packages/actions-post-create-pr/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
{
"name": "post-create-pr-action",
"private": true,
"type": "module",
"version": "0.0.1",
"scripts": {
"start": "tsx src/index.ts",
"test": "vitest",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@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',
}),
);
},
),
);
50 changes: 50 additions & 0 deletions packages/actions-post-create-pr/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { test, expect, vi } from 'vitest';
import { onCommentUpdated } from './GithubAPI.mock';

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());
13 changes: 13 additions & 0 deletions packages/actions-post-create-pr/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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')],
},
});
6 changes: 3 additions & 3 deletions packages/site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"deploy": "touch dist/.nojekyll && gh-pages --dist dist --dotfiles true",
"preview": "astro preview",
"astro": "astro",
"test": "playwright test"
"test": "vitest"
},
"dependencies": {
"@astrojs/mdx": "^0.19.7",
Expand All @@ -23,7 +23,7 @@
"tailwindcss": "^3.3.3"
},
"devDependencies": {
"@playwright/test": "^1.36.1",
"gh-pages": "^5.0.0"
"gh-pages": "^5.0.0",
"playwright": "^1.36.2"
}
}
17 changes: 0 additions & 17 deletions packages/site/playwright.config.ts

This file was deleted.

18 changes: 0 additions & 18 deletions packages/site/test/index.spec.ts

This file was deleted.

29 changes: 29 additions & 0 deletions packages/site/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Browser, chromium } from 'playwright';
import { afterAll, beforeAll, expect, test } from 'vitest';

let browser: Browser;
beforeAll(async () => {
browser = await chromium.launch({ headless: true });
});
afterAll(async () => {
await browser.close();
});

test('meta is correct', async () => {
const page = await browser.newPage();
page.on('console', (msg) => console.log(msg.text()));

await page.goto('http://localhost:3000/oulu-dev-meetups/');

const title = await page.title();
expect(title).toBe('Oulu developer meetups');
});

test('main heading is set', async () => {
const page = await browser.newPage();
page.on('console', (msg) => console.log(msg.text()));

await page.goto('http://localhost:3000/oulu-dev-meetups/');

page.getByRole('heading', { level: 1, name: 'Oulu developer meetups' });
});
53 changes: 53 additions & 0 deletions packages/site/test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ChildProcess, exec, execSync } from 'node:child_process';
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const root = resolve(fileURLToPath(import.meta.url), '../');

let subprocess: ChildProcess;

export async function setup() {
log('Building astro site');
execSync('pnpm build', { cwd: root });

log('Starting astro server');
subprocess = exec('pnpm preview', { cwd: root });

await new Promise((resolve, reject) => {
const timer = setTimeout(
() => reject(new Error('Timeout waiting for "pnpm preview"')),
10_000,
);

subprocess.stdout?.on('data', (data: Buffer) => {
if (data.toString().includes('started in')) {
clearTimeout(timer);
resolve(null);
}
});

subprocess.stderr?.on('data', (data: Buffer) => {
reject(data.toString());
});
});
}

export async function teardown() {
subprocess.kill();
log('Stopping astro server');

await new Promise((resolve, reject) => {
const timer = setTimeout(
() => reject(new Error('Timeout waiting for "pnpm preview" to exit')),
10_000,
);
subprocess.on('exit', () => {
clearTimeout(timer);
resolve(null);
});
});
}

function log(...messages: Parameters<typeof console.log>) {
console.log('[global-setup]', ...messages);
}
13 changes: 13 additions & 0 deletions packages/site/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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: 'site',
globalSetup: resolve(__dirname, './test/setup.ts'),
},
});
Loading

0 comments on commit aa746b0

Please sign in to comment.