Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests #8

Merged
merged 14 commits into from
Dec 28, 2022
34 changes: 34 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Node.js tests

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x, 18.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- uses: pnpm/action-setup@v2
with:
version: 7
- name: Install packages
run: pnpm install
- name: Install Playwright
run: pnpx playwright install
- name: Build
run: pnpm build
- name: Test
run: pnpm test
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"test": "playwright test",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"test:unit": "vitest",
"test:playwright": "playwright test",
"test": "pnpm test:unit run && pnpm test:playwright",
"lint": "prettier --plugin-search-dir . --check . && eslint .",
"format": "prettier --plugin-search-dir . --write ."
},
Expand Down
7 changes: 0 additions & 7 deletions src/index.test.ts

This file was deleted.

35 changes: 35 additions & 0 deletions src/lib/slugFromPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, it, expect } from 'vitest';
import { slugFromPath } from './slugFromPath';

describe('slugFromPath', () => {
it('extracts slug from paths correctly', () => {
const cases = [
{
path: '/foo/bar/test-slug.md',
expected: 'test-slug'
},
{
path: '/foo/bar/test-slug.svx',
expected: 'test-slug'
},
{
path: '/foo/bar/test-slug.svelte.md',
expected: 'test-slug'
}
];

cases.forEach(({ path, expected }) => expect(slugFromPath(path)).toBe(expected));
});

it('returns null for unknown extension', () => {
const path = '/foo/bar/test-slug.abc';

expect(slugFromPath(path)).toBeNull();
});

it('returns null for no extension', () => {
const path = '/foo/bar/test-slug';

expect(slugFromPath(path)).toBeNull();
});
});
21 changes: 19 additions & 2 deletions tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { expect, test } from '@playwright/test';

test('index page has expected h1', async ({ page }) => {
test('index page has expected content', async ({ page }) => {
await page.goto('/');
expect(await page.textContent('h1')).toBe('Welcome to SvelteKit');

const articles = await page.$$('article');

expect(await page.textContent('h1')).toBe('SvelteKit + MDsveX Blog');
expect(articles.length).toBeGreaterThan(0);
expect(articles.length).not.toBeGreaterThan(10);
for (const article of articles) {
expect(await article.$('a')).not.toBeFalsy();
}
});

test('clicking on article title in home page navigates to the article', async ({ page }) => {
await page.goto('/');
const title = await page.textContent('article h3');

await page.getByText(title || '').click();

expect(await page.textContent('h2')).toBe(title);
});