Skip to content

Commit d9f16f1

Browse files
authored
feat: add unit test project and update Vitest best practices (#11)
* feat: add unit test project and update Vitest best practices - Add dual project setup: unit tests (jsdom) and storybook tests (browser) - Create src/utils.ts with formatGreeting utility as example - Create test/utils.test.ts demonstrating unit test patterns - Add jsdom to devDependencies for unit test environment - Update @Neovici/testing from ^2.0.0 to ^2.2.0 (adds renderHook, waitUntil) - Update package.json scripts: test, test:unit, test:storybook, test:watch - Update README with testing documentation and best practices - Document important warning: never import from 'vitest' in story files NEO-934 * fix: formatting of cosmoz-component usage example * docs: remove renderHook mention from unit tests renderHook from @Neovici/testing has ESM resolution issues in jsdom and multiple Lit version conflicts in browser mode. Hooks should be tested via Storybook interaction tests instead. * chore: update @Neovici/testing to ^2.3.0 for ESM compatibility * docs: add note about jsdom limitations for Pion/Lit imports Unit tests in jsdom cannot use renderHook or import Pion/Lit components due to ESM resolution issues in @pionjs/pion. Document this limitation and recommend using Storybook interaction tests for hooks and components. * fix: split usage example into separate JS and HTML code blocks
1 parent 3ac0a10 commit d9f16f1

File tree

6 files changed

+530
-142
lines changed

6 files changed

+530
-142
lines changed

docs/README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ npm install
1212

1313
- `npm run lint` - Run ESLint and TypeScript type checking
1414
- `npm run build` - Build TypeScript to dist/
15-
- `npm run test` - Run tests with coverage
15+
- `npm run test` - Run all tests (unit + storybook)
16+
- `npm run test:unit` - Run unit tests only (fast, jsdom)
17+
- `npm run test:storybook` - Run storybook interaction tests only (browser)
1618
- `npm run test:watch` - Run tests in watch mode
1719
- `npm run storybook:start` - Start Storybook development server
1820
- `npm run storybook:build` - Build static Storybook
@@ -23,8 +25,11 @@ Import the component:
2325

2426
```javascript
2527
import '@neovici/cosmoz-component';
28+
```
29+
30+
Use in HTML:
2631

27-
// Use in HTML
32+
```html
2833
<cosmoz-component greeting="Hi"></cosmoz-component>
2934
```
3035

@@ -35,6 +40,58 @@ import '@neovici/cosmoz-component';
3540
3. Start development with `npm run storybook:start`
3641
4. Make changes and verify with tests
3742

43+
## Testing
44+
45+
This template uses Vitest with two test projects:
46+
47+
### Unit Tests (`test:unit`)
48+
49+
Fast tests that run in jsdom. Use for testing:
50+
51+
- Utility functions
52+
- Pure logic
53+
- Data transformations
54+
55+
**Note**: Unit tests cannot import Pion/Lit components or use `renderHook` from `@neovici/testing` due to ESM resolution issues in jsdom. For testing hooks and components, use Storybook interaction tests instead.
56+
57+
```typescript
58+
// test/example.test.ts
59+
import { describe, expect, it } from 'vitest';
60+
import { myFunction } from '../src/utils';
61+
62+
describe('myFunction', () => {
63+
it('does something', () => {
64+
expect(myFunction()).toBe(expected);
65+
});
66+
});
67+
```
68+
69+
### Storybook Tests (`test:storybook`)
70+
71+
Browser-based tests using Playwright. Use for testing:
72+
73+
- Component rendering
74+
- User interactions
75+
- Visual behavior
76+
77+
Tests are written as `play` functions in story files.
78+
79+
### Important: Imports in Story Files
80+
81+
**Never import from `'vitest'` in story files:**
82+
83+
```typescript
84+
import { expect } from 'vitest'; // Crashes deployed Storybook
85+
```
86+
87+
**Use `'storybook/test'` instead:**
88+
89+
```typescript
90+
import { expect } from 'storybook/test'; // Works everywhere
91+
```
92+
93+
Vitest's `expect` requires an active test context and crashes when stories run in the deployed Storybook UI.
94+
3895
## Publishing
3996

4097
This package uses Semantic Release for automated versioning and publishing. Commits are analyzed and releases are created automatically based on Conventional Commits.

0 commit comments

Comments
 (0)