Asagiriは、高品質なCSSフレームワークを保証するため、複数の層でテストを実施します。
UIの意図しない変更を検出
WCAG 2.1 AA準拠を保証
主要ブラウザでの互換性
ロード時間とレンダリング最適化
CSS変数の整合性チェック
関数とミックスインの検証
UIの予期しない変更を自動的に検出するための重要なテスト層です。
# インストール
npm install --save-dev chromatic
# package.json にスクリプト追加
{
"scripts": {
"chromatic": "chromatic --project-token=<your-token>"
}
}
# 実行
npm run chromatic
# .github/workflows/visual-tests.yml
name: Visual Tests
on: [pull_request]
jobs:
percy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npm run build
- name: Percy Test
run: npx percy snapshot ./showcase.html
env:
PERCY_TOKEN: ${{ secrets.PERCY_TOKEN }}
// backstop.config.js
module.exports = {
id: "asagiri",
viewports: [
{
label: "phone",
width: 375,
height: 667
},
{
label: "tablet",
width: 768,
height: 1024
},
{
label: "desktop",
width: 1920,
height: 1080
}
],
scenarios: [
{
label: "Components Showcase",
url: "http://localhost:8080/showcase.html",
selectors: [".btn", ".card", ".alert"],
delay: 500
},
{
label: "Dark Mode",
url: "http://localhost:8080/showcase.html",
selectors: ["body"],
onBeforeScript: "dark-mode.js"
}
],
paths: {
bitmaps_reference: "backstop_data/bitmaps_reference",
bitmaps_test: "backstop_data/bitmaps_test",
html_report: "backstop_data/html_report"
}
};
WCAG 2.1 AA準拠を保証し、すべてのユーザーが利用可能なUIを提供します。
// accessibility.test.js
const { AxePuppeteer } = require('@axe-core/puppeteer');
const puppeteer = require('puppeteer');
describe('Accessibility Tests', () => {
let browser;
let page;
beforeAll(async () => {
browser = await puppeteer.launch();
page = await browser.newPage();
});
afterAll(async () => {
await browser.close();
});
test('Components showcase should be accessible', async () => {
await page.goto('http://localhost:8080/showcase.html');
const results = await new AxePuppeteer(page).analyze();
expect(results.violations).toHaveLength(0);
});
test('Dark mode should be accessible', async () => {
await page.goto('http://localhost:8080/showcase.html');
await page.evaluate(() => {
document.documentElement.setAttribute('data-theme', 'dark');
});
const results = await new AxePuppeteer(page).analyze();
expect(results.violations).toHaveLength(0);
});
});
// pa11y.config.js
module.exports = {
standard: 'WCAG2AA',
urls: [
'http://localhost:8080/showcase.html',
'http://localhost:8080/docs/getting-started.html'
],
chromeLaunchConfig: {
args: ['--no-sandbox']
},
runners: ['axe', 'htmlcs'],
threshold: 0 // 0 violations allowed
};
// package.json
{
"scripts": {
"test:a11y": "pa11y-ci --config pa11y.config.js"
}
}
// contrast-check.js
const Color = require('colorjs.io');
const colorPairs = [
{ bg: '#ffffff', fg: '#4c5059', name: 'Text on Light' },
{ bg: '#0f0f0f', fg: '#e4e4e7', name: 'Text on Dark' },
{ bg: '#31a9c7', fg: '#ffffff', name: 'Primary Button' }
];
colorPairs.forEach(pair => {
const ratio = Color.contrastRatio(pair.bg, pair.fg);
const wcagAA = ratio >= 4.5;
const wcagAAA = ratio >= 7;
console.log(`${pair.name}: ${ratio.toFixed(2)}:1`);
console.log(` WCAG AA: ${wcagAA ? 'PASS' : 'FAIL'}`);
console.log(` WCAG AAA: ${wcagAAA ? 'PASS' : 'FAIL'}`);
});
主要ブラウザ(Chrome、Firefox、Safari、Edge)での互換性を保証します。
// playwright.config.js
module.exports = {
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] }
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] }
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] }
},
{
name: 'Mobile Chrome',
use: { ...devices['Pixel 5'] }
},
{
name: 'Mobile Safari',
use: { ...devices['iPhone 13'] }
}
]
};
// tests/cross-browser.spec.js
test.describe('Cross Browser Tests', () => {
test('buttons render correctly', async ({ page }) => {
await page.goto('http://localhost:8080/showcase.html');
const button = page.locator('.btn-primary').first();
await expect(button).toBeVisible();
await expect(button).toHaveCSS('background-color', 'rgb(49, 169, 199)');
});
});
// browserstack.config.js
exports.config = {
user: process.env.BROWSERSTACK_USERNAME,
key: process.env.BROWSERSTACK_ACCESS_KEY,
capabilities: [
{
browserName: 'Chrome',
browser_version: 'latest',
os: 'Windows',
os_version: '11'
},
{
browserName: 'Safari',
browser_version: 'latest',
os: 'OS X',
os_version: 'Ventura'
},
{
browserName: 'Edge',
browser_version: 'latest',
os: 'Windows',
os_version: '11'
}
]
};
高速なロード時間とスムーズなレンダリングを保証します。
// lighthouserc.js
module.exports = {
ci: {
collect: {
url: [
'http://localhost:8080/showcase.html',
'http://localhost:8080/docs/getting-started.html'
],
numberOfRuns: 3
},
assert: {
assertions: {
'categories:performance': ['error', { minScore: 0.9 }],
'categories:accessibility': ['error', { minScore: 0.95 }],
'categories:best-practices': ['error', { minScore: 0.9 }],
'first-contentful-paint': ['error', { maxNumericValue: 2000 }],
'largest-contentful-paint': ['error', { maxNumericValue: 2500 }],
'cumulative-layout-shift': ['error', { maxNumericValue: 0.1 }]
}
},
upload: {
target: 'temporary-public-storage'
}
}
};
// package.json
{
"scripts": {
"size": "size-limit",
"size:why": "size-limit --why"
}
}
// .size-limit.js
module.exports = [
{
name: "Asagiri Full",
path: "dist/asagiri.min.css",
limit: "50 KB",
gzip: true
},
{
name: "Asagiri Core",
path: "dist/asagiri.core.min.css",
limit: "20 KB",
gzip: true
}
];
| 指標 | 目標値 | 説明 |
|---|---|---|
FCP |
< 2.0s | First Contentful Paint |
LCP |
< 2.5s | Largest Contentful Paint |
CLS |
< 0.1 | Cumulative Layout Shift |
TTI |
< 3.8s | Time to Interactive |
Bundle Size |
< 50 KB (gzip) | Full framework size |
Sassの関数、ミックスイン、ロジックをテストします。
// tests/scss/_colors.scss
@use 'true' as *;
@use '../../scss/Tokens/Color' as *;
@include test-module('Color Functions') {
@include test('color-mix function') {
@include assert-equal(
color-mix(in srgb, red 50%, blue),
#8000ff,
'Should mix colors correctly'
);
}
@include test('CSS variable generation') {
@include assert-true(
variable-exists(color-primary),
'Primary color variable should exist'
);
}
}
// tests/design-tokens.test.js
const sass = require('sass');
const path = require('path');
describe('Design Tokens', () => {
let compiledCSS;
beforeAll(() => {
const result = sass.compile(
path.join(__dirname, '../scss/main.scss')
);
compiledCSS = result.css.toString();
});
test('Primary color is defined', () => {
expect(compiledCSS).toContain('--color-primary');
});
test('Spacing scale is consistent', () => {
const spacingVars = [
'--spacing-1', '--spacing-2', '--spacing-3',
'--spacing-4', '--spacing-5', '--spacing-6'
];
spacingVars.forEach(variable => {
expect(compiledCSS).toContain(variable);
});
});
test('Dark theme overrides are present', () => {
expect(compiledCSS).toContain('[data-theme="dark"]');
});
});
GitHub Actionsを使用した自動テストとデプロイメント。
# .github/workflows/test.yml
name: Test Suite
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npm run lint:scss
- run: npm run lint:css
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npm run build
- run: npm run size
unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npm test
visual-regression:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npm run build
- run: npm run chromatic
env:
CHROMATIC_PROJECT_TOKEN: ${{ secrets.CHROMATIC_TOKEN }}
accessibility:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npm run build
- run: npm start &
- run: npm run test:a11y
cross-browser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npx playwright install --with-deps
- run: npm run test:cross-browser
performance:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npm run build
- run: npm start &
- run: npm run lighthouse
env:
LHCI_GITHUB_APP_TOKEN: ${{ secrets.LHCI_TOKEN }}
| カテゴリ | ツール | 理由 |
|---|---|---|
| ビジュアル | Chromatic | Storybook統合、PR自動コメント |
| アクセシビリティ | axe-core + pa11y | 業界標準、CI/CD対応 |
| クロスブラウザ | Playwright | 高速、信頼性高い、実機不要 |
| パフォーマンス | Lighthouse CI | 無料、詳細なレポート |
| ユニットテスト | Jest + True | JavaScriptとSassの両対応 |