Skip to content

Commit

Permalink
add browser testing
Browse files Browse the repository at this point in the history
  • Loading branch information
fquffio committed Feb 4, 2025
1 parent 948afe1 commit 779a1c8
Show file tree
Hide file tree
Showing 10 changed files with 568 additions and 88 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ jobs:
with:
cache: yarn

- name: Get Yarn cache directory path
id: yarn-cache-dir-path
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT

- name: Setup Yarn cache
uses: actions/cache@v4
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install project dependencies
run: yarn install

Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,29 @@ jobs:
with:
cache: yarn

- name: Get Yarn cache directory path
id: yarn-cache-dir-path
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT

- name: Setup Yarn cache
uses: actions/cache@v4
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install project dependencies
run: yarn install

- name: Setup Playwright cache
uses: actions/cache@v4
with:
path: ~/.cache/ms-playwright
key: ${{ runner.os }}-playwright

- name: Install Playwright browsers
run: yarn run playwright install --with-deps

- name: Check
run: yarn run test:unit
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,21 @@
"@types/node": "^22.9.1",
"@typescript-eslint/eslint-plugin": "^8.15.0",
"@typescript-eslint/parser": "^8.15.0",
"@vitest/coverage-v8": "^3.0.4",
"@vitest/browser": "^3.0.5",
"@vitest/coverage-v8": "^3.0.5",
"eslint": "^8.57.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-svelte": "^2.46.0",
"pino-pretty": "^13.0.0",
"playwright": "^1.50.1",
"prettier": "^3.3.3",
"prettier-plugin-svelte": "^3.3.1",
"svelte": "^5.0.0",
"svelte-check": "^4.1.4",
"tslib": "^2.8.1",
"typescript": "^5.6.3",
"vite": "^6.0.0",
"vitest": "^3.0.4"
"vitest": "^3.0.5"
},
"peerDependencies": {
"@sveltejs/kit": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Pino from 'pino';
import { dev } from '$app/environment';

const pino: typeof Pino.pino = typeof Pino === 'function' ? (Pino as any) : Pino.default;
const pino: typeof Pino.pino = typeof Pino === 'function' ? Pino : Pino.default;
export const logger: Pino.Logger = pino({
transport: dev ? { target: 'pino-pretty' } : undefined,
level: dev ? 'debug' : 'info',
Expand Down
34 changes: 20 additions & 14 deletions tests/test-logger.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { destination, type Level } from 'pino';
import type { Level } from 'pino';

const METADATA = Symbol.for('pino.metadata');
class DebugTransport {
#data: string[] = [];

type PinoMetadata = ReturnType<typeof destination> & {
[METADATA]: boolean;
lastLevel?: Level;
lastMsg?: string;
lastObj?: unknown;
lastTime?: number;
};
lastLevel?: Level = undefined;
lastMsg?: string = undefined;
lastObj?: unknown = undefined;
lastTime?: number = undefined;

export const testTransportFactory = () => {
const dest = destination('/dev/null') as PinoMetadata;
dest[METADATA] = true;
get [Symbol.for('pino.metadata')]() {
return true;
}

return dest;
};
get writable() {
return true;
}

write(datum: string): void {
this.#data.push(datum);
}
}

export const testTransportFactory = () => new DebugTransport();
2 changes: 1 addition & 1 deletion tests/utils/browser.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sanitizeHtml } from '$lib/utils/browser';
import { describe, expect, it } from 'vitest';

describe.skipIf(typeof window === 'undefined')(sanitizeHtml.name, async () => {
describe(sanitizeHtml.name, async () => {
const CASES = {
'should remove tag': { html: 'hello <a href="https://example.org/">world</a>', expected: 'hello world' },
} satisfies Record<string, { html: string | undefined | null; expected: string }>;
Expand Down
File renamed without changes.
20 changes: 10 additions & 10 deletions tests/utils/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe(timeout.name, () => {
const result = await timeout(80);
const end = Date.now();

expect(end - start).to.be.approximately(80, 2);
expect(end - start).to.be.approximately(80, 5);
expect(result).to.equal(undefined);
});

Expand All @@ -16,7 +16,7 @@ describe(timeout.name, () => {
const result = await timeout(120, { foo: 'bar' });
const end = Date.now();

expect(end - start).to.be.approximately(120, 2);
expect(end - start).to.be.approximately(120, 5);
expect(result).to.deep.equal({ foo: 'bar' });
});
});
Expand Down Expand Up @@ -64,9 +64,9 @@ describe(backoffRetry.name, () => {
await expect(backoffRetry(factory, 10, 50, 3, JitterMode.None)).resolves.to.equal('foo');
expect(count).to.equals(3);
expect(laps).to.have.length(3);
expect(laps[0]).to.be.approximately(0, 2);
expect(laps[1]).to.be.approximately(10, 2);
expect(laps[2]).to.be.approximately(20, 2);
expect(laps[0]).to.be.approximately(0, 5);
expect(laps[1]).to.be.approximately(10, 5);
expect(laps[2]).to.be.approximately(20, 5);
});

it('should not wait longer than cap', async () => {
Expand All @@ -85,9 +85,9 @@ describe(backoffRetry.name, () => {
await expect(backoffRetry(factory, 10, 15, 3, JitterMode.None)).resolves.to.equal('foo');
expect(count).to.equals(3);
expect(laps).to.have.length(3);
expect(laps[0]).to.be.approximately(0, 2);
expect(laps[1]).to.be.approximately(10, 2);
expect(laps[2]).to.be.approximately(15, 2);
expect(laps[0]).to.be.approximately(0, 5);
expect(laps[1]).to.be.approximately(10, 5);
expect(laps[2]).to.be.approximately(15, 5);
});

it('should not invoke the function more than max attempts times', async () => {
Expand All @@ -106,7 +106,7 @@ describe(backoffRetry.name, () => {
await expect(backoffRetry(factory, 10, 15, 2, JitterMode.None)).resolves.to.equal(undefined);
expect(count).to.equals(2);
expect(laps).to.have.length(2);
expect(laps[0]).to.be.approximately(0, 2);
expect(laps[1]).to.be.approximately(10, 2);
expect(laps[0]).to.be.approximately(0, 5);
expect(laps[1]).to.be.approximately(10, 5);
});
});
26 changes: 26 additions & 0 deletions vitest.workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { defineWorkspace } from 'vitest/config';

export default defineWorkspace([
{
extends: './vitest.config.ts',
test: {
include: ['tests/**/*.{test,spec}.ts', '!tests/**/*.browser.{test,spec}.ts', '!tests/**/browser.{test,spec}.ts'],
name: 'unit',
environment: 'node',
},
},
{
extends: './vitest.config.ts',
test: {
include: ['tests/**/*.{test,spec}.ts', '!tests/**/*.server.{test,spec}.ts'],
name: 'browser',
browser: {
enabled: true,
provider: 'playwright',
headless: true,
screenshotFailures: false,
instances: [{ browser: 'chromium' }],
},
},
},
]);
Loading

0 comments on commit 779a1c8

Please sign in to comment.