Skip to content

Commit 58cbc47

Browse files
committed
Add a first working test, backed by the storybook
1 parent ae9ea47 commit 58cbc47

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

.storybook/vitest.setup.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { setProjectAnnotations } from '@storybook/react-vite';
2+
import * as projectAnnotations from './preview';
3+
4+
// This is an important step to apply the right configuration when testing your stories.
5+
// More info at: https://storybook.js.org/docs/api/portable-stories/portable-stories-vitest#setprojectannotations
6+
setProjectAnnotations([projectAnnotations]);

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"scripts": {
1515
"build": "rimraf dist/ && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json && tsc -p tsconfig.web.json",
1616
"pretest": "npm run build",
17-
"test": "echo \"It built ok, that'll do for now\"",
17+
"test": "vitest --run",
1818
"prepare": "npm run build",
1919
"storybook": "storybook dev -p 6006",
2020
"build-storybook": "storybook build -o examples"
@@ -42,18 +42,22 @@
4242
"@storybook/addon-docs": "^9.1.10",
4343
"@storybook/react": "^9.1.10",
4444
"@storybook/react-vite": "^9.1.10",
45+
"@testing-library/react": "^16.3.0",
4546
"@types/events": "^3.0.0",
4647
"@types/node": "^24.6.2",
4748
"@types/react": "^18.3.1",
4849
"@types/react-dom": "^18.3.1",
4950
"@vitejs/plugin-react": "^5.0.4",
51+
"@vitest/browser": "^3.2.4",
52+
"playwright": "^1.55.1",
5053
"react": "^18.3.1",
5154
"react-dom": "^18.3.1",
5255
"rimraf": "^3.0.0",
5356
"storybook": "^9.1.10",
5457
"typescript": "^5.9.3",
5558
"vite": "^7.1.8",
56-
"vite-plugin-checker": "^0.11.0"
59+
"vite-plugin-checker": "^0.11.0",
60+
"vitest": "^3.2.4"
5761
},
5862
"peerDependencies": {
5963
"react": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",

tests/.claude/settings.local.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"Bash(cd:*)",
5+
"Bash(/dev/null)",
6+
"Bash(echo \"Exit code: $?\")"
7+
],
8+
"deny": [],
9+
"ask": []
10+
}
11+
}

tests/html.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { expect, test } from 'vitest';
2+
import { render, screen } from '@testing-library/react';
3+
import { composeStory } from '@storybook/react';
4+
import * as stories from '../stories/html.stories';
5+
6+
const RenderThingsInDifferentPlaces = composeStory(
7+
stories.RenderThingsInDifferentPlaces,
8+
stories.default
9+
);
10+
11+
test('RenderThingsInDifferentPlaces', async () => {
12+
const { container } = render(<RenderThingsInDifferentPlaces />);
13+
14+
expect(container.innerHTML).toContain(
15+
'<div>Portal renders here:<div>Hi!</div></div>'
16+
);
17+
});

vite.config.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1+
/// <reference types="vitest/config" />
12
import { defineConfig } from 'vite';
23
import react from '@vitejs/plugin-react';
34

45
export default defineConfig({
56
plugins: [react()],
6-
});
7+
test: {
8+
include: ['tests/**/*.test.{ts,tsx}'],
9+
browser: {
10+
enabled: true,
11+
headless: true,
12+
provider: 'playwright',
13+
screenshotFailures: false,
14+
instances: [{
15+
browser: 'chromium'
16+
}]
17+
},
18+
setupFiles: ['.storybook/vitest.setup.ts']
19+
}
20+
});

0 commit comments

Comments
 (0)