♻️ Migrate storybook tests to node:test#157
Conversation
- Replace vitest with node:test and node:assert/strict - Rename test files from .spec.js to .test.js - Port vi.fn() mocks to node:test mock helpers - Remove vitest dependency and config - Port CI_OPTIMIZED_ARGS and navigation timeout fallback from static-site Closes #142
Add comprehensive tests for: - viewport utilities (parseViewport, formatViewport, setViewport, getCommonViewports) - pattern matching (matchPattern, filterByPattern, findMatchingHook edge cases) - screenshot functions (captureScreenshot, captureAndSendScreenshot) - crawler functions (readIndexJson, discoverStories, generateStoryUrl validation) Coverage: 99.33% lines, 92.86% branches, 97.06% functions
Code ReviewI've reviewed PR #157 and have the following feedback: ✅ Strengths1. Test Migration Quality
2. Improved Test Coverage
3. Puppeteer Optimizations
🐛 Potential Issues1. Critical: Browser Args Applied Unconditionally (src/browser.js:60) The args: [...CI_OPTIMIZED_ARGS, ...args],Issue: Flags like Recommendation: const isCI = process.env.CI || process.env.GITHUB_ACTIONS || process.env.JENKINS_HOME;
const baseArgs = isCI ? CI_OPTIMIZED_ARGS : ['--no-sandbox', '--disable-setuid-sandbox'];
args: [...baseArgs, ...args],2. Navigation Fallback Hides Real Issues (src/browser.js:102-116) The try/catch silently falls back from catch (error) {
if (error.message.includes('timeout') || error.message.includes('Navigation timeout')) {
await page.goto(url, { waitUntil: 'domcontentloaded', ... });
}
}Issues:
Recommendations:
3. Missing Input Validation (src/crawler.js:124-132) Good addition of validation in if (\!baseUrl || typeof baseUrl \!== 'string') {
throw new Error('baseUrl must be a non-empty string');
}Issue: Doesn't validate against malformed URLs (e.g., Recommendation: try {
new URL(baseUrl); // Validates URL format
} catch {
throw new Error('baseUrl must be a valid URL');
}🔍 Code Quality & Best Practices4. Test Cleanup Could Be More Robust Tests in testDir = join(tmpdir(), `storybook-test-${Date.now()}`);
await rm(testDir, { recursive: true });Minor Issue: If tests fail/crash, temp directories won't be cleaned up. Recommendation: Use import { before, after } from 'node:test';
describe('readIndexJson', () => {
let testDir;
after(async () => {
if (testDir) await rm(testDir, { recursive: true, force: true });
});
it('should read...', async () => {
testDir = join(tmpdir(), `storybook-test-${Date.now()}`);
// test code
});
});5. Hard-Coded Memory Limit May Be Too Restrictive '--js-flags=--max-old-space-size=512', // Limit V8 heapConcern: 512MB is quite low for modern JS apps. Large Storybooks may crash with OOM errors. Recommendation:
6. Test Script Uses Shell Command Substitution (package.json:44) "test": "node --test --test-reporter=spec $(find tests -name '*.test.js')"Issue: Won't work on Windows (no Recommendation: "test": "node --test --test-reporter=spec tests/**/*.test.js"Node's 🔒 SecurityNo security concerns identified. Input validation additions are a good security practice. ⚡ PerformanceThe Puppeteer optimizations should improve CI performance:
However, concern #1 (unconditional CI args) may hurt local dev performance. 📝 Summary & RecommendationsThis is a high-quality PR with excellent test coverage improvements. The migration from Vitest to Node.js test runner is clean and well-executed. Must Fix:
Should Fix: Consider: Overall: Approve with requested changes ✅ Test Plan Verification: ✅ All items in PR test plan are covered |
5c5bbc8 to
5d08c61
Compare
- Only apply CI_OPTIMIZED_ARGS in CI environments (detect via env vars) - Add console.warn when navigation falls back to domcontentloaded - Check error.name === 'TimeoutError' in addition to message matching - Increase V8 heap limit from 512MB to 1024MB for larger Storybooks - Fix test script to use glob pattern instead of find (Windows compat) - Use after() hooks for guaranteed temp directory cleanup in tests
Summary
@vizzly-testing/storybooktest suite from Vitest to Node's built-in test runner (node:test)@vizzly-testing/static-siteChanges
Test Migration:
vitestimports withnode:testandnode:assert/strict.spec.jsto.test.jsvi.fn()mocks tonode:testmockhelperspackage.jsontest scripts to usenode --testvitestdependency andvitest.config.jsPuppeteer Performance (ported from static-site):
CI_OPTIMIZED_ARGSfor reduced memory usage in CI environmentsprotocolTimeout: 60_000for faster failure detectionnetworkidle2todomcontentloadedNew Tests:
viewport.test.js- parseViewport, formatViewport, setViewport, getCommonViewportspatterns.test.js- matchPattern, filterByPattern, findMatchingHook edge casesscreenshot.test.js- captureScreenshot, captureAndSendScreenshotcrawler.test.js- readIndexJson, discoverStories, generateStoryUrl validationTest Plan
npm testnpm run lintCloses #142