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

Vitest #4

Merged
merged 12 commits into from
Feb 11, 2025
5 changes: 5 additions & 0 deletions .changeset/fifty-drinks-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chialab/sveltekit-utils': patch
---

Fix headers not being written to logs on fetch failures.
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
6 changes: 5 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ jobs:
uses: ./.github/workflows/lint.yml
secrets: inherit

test:
uses: ./.github/workflows/test.yml
secrets: inherit

release:
needs: lint
needs: [lint, test]
uses: ./.github/workflows/release.yml
secrets: inherit
4 changes: 4 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ jobs:
lint:
uses: ./.github/workflows/lint.yml
secrets: inherit

test:
uses: ./.github/workflows/test.yml
secrets: inherit
49 changes: 49 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Test
on:
workflow_call:

jobs:
unit:
name: Run unit tests
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Checkout the repository
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v4
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:coverage

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ node_modules
/.env.production
/.env.*.local
/*.log
/tests/coverage/
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
*.tsbuildinfo
Expand Down
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dbaeumer.vscode-eslint", // ESLint official extension
"esbenp.prettier-vscode", // Prettier official extension
"stylelint.vscode-stylelint", // Stylelint official extension
"svelte.svelte-vscode" // Svelte official extension
"svelte.svelte-vscode", // Svelte official extension
"vitest.explorer" // Vitest official extension
]
}
24 changes: 16 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js",
"svelte": "./dist/index.js"
"svelte": "./dist/index.js",
"default": "./dist/index.js"
},
"./server": {
"types": "./dist/server/index.d.ts",
"node": "./dist/server/index.js",
"default": null,
"svelte": null
"svelte": null,
"default": null
},
"./logger": {
"types": "./dist/logger.d.ts",
"default": "./dist/logger.js"
},
"./utils": {
"types": "./dist/utils.d.ts",
"default": "./dist/utils.js"
"types": "./dist/utils/index.d.ts",
"default": "./dist/utils/index.js"
}
},
"scripts": {
Expand All @@ -34,13 +34,17 @@
"build": "svelte-kit sync && svelte-package",
"app:build": "vite build",
"app:preview-build": "vite preview | pino-pretty",
"test:unit": "vitest run",
"test:unit:coverage": "vitest run --coverage",
"test:unit:watch": "vitest watch",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"eslint-check": "eslint --ignore-path .gitignore . --ext .js,.cjs,.ts,.svelte",
"eslint-fix": "eslint --fix --ignore-path .gitignore . --ext .js,.cjs,.ts,.svelte",
"prettier-check": "prettier --check \"./**/*.{json,css,js,cjs,ts,svelte}\"",
"prettier-fix": "prettier --write \"./**/*.{json,css,js,cjs,ts,svelte}\"",
"lint-fix-all": "yarn eslint-fix && yarn prettier-fix"
"lint": "yarn run check && yarn run eslint-check && yarn run prettier-fix",
"lint-fix-all": "yarn run eslint-fix && yarn run prettier-fix"
},
"dependencies": {
"cookie": "^1.0.2",
Expand All @@ -57,17 +61,21 @@
"@types/node": "^22.9.1",
"@typescript-eslint/eslint-plugin": "^8.15.0",
"@typescript-eslint/parser": "^8.15.0",
"@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"
"vite": "^6.0.0",
"vitest": "^3.0.5"
},
"peerDependencies": {
"@sveltejs/kit": "^2.0.0",
Expand Down
3 changes: 1 addition & 2 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './logger.js';
export * from './url.js';
export * from './utils.js';
export * from './utils/index.js';
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
2 changes: 1 addition & 1 deletion src/lib/server/cache/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { JitterFn, JitterMode } from '../../utils.js';
import type { JitterFn, JitterMode } from '../../utils/misc.js';

/**
* Base class for caching.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/server/cache/in-memory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createJitter, JitterMode, type JitterFn } from '../../utils.js';
import { createJitter, JitterMode, type JitterFn } from '../../utils/misc.js';
import { BaseCache } from './base.js';

type ValueWrapper<T> = { value: T; expire: number | undefined };
Expand Down
2 changes: 1 addition & 1 deletion src/lib/server/cache/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'redis';
import { logger } from '../../logger.js';
import { BaseCache } from './base.js';
import { createJitter, JitterMode, type JitterFn } from '../../utils.js';
import { createJitter, JitterMode, type JitterFn } from '../../utils/misc.js';

type RedisCacheOptions = {
keyPrefix?: string;
Expand Down
7 changes: 1 addition & 6 deletions src/lib/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,4 @@ export type Hashed<T> = { hash: string } & T;
* @param input String to compute hash for.
* @param algo Algorithm.
*/
export const computeHash = (input: string, algo = 'sha256'): string => {
const hash = createHash(algo);
hash.update(input);

return hash.digest('hex');
};
export const computeHash = (input: string, algo = 'sha256'): string => createHash(algo).update(input).digest('hex');
Loading
Loading