Skip to content

Commit

Permalink
use jest-test/vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Apr 21, 2024
1 parent f7a5d37 commit 04c57bd
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"signal-polyfill": "^0.1.0"
},
"devDependencies": {
"@testing-library/jest-dom": "^6.4.2",
"@testing-library/react": "^15.0.2",
"@testing-library/user-event": "^14.5.2",
"@types/node": "^20.12.7",
Expand Down
84 changes: 84 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions tests/03_component.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { test } from 'vitest';
import { render, screen } from '@testing-library/react';
import { afterEach, expect, test } from 'vitest';
import { cleanup, render, screen } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';

import { Signal, useSignal } from 'use-signals';

afterEach(cleanup);

test('counter app', async () => {
const user = userEvent.setup();
const counter = new Signal.State(0);
Expand All @@ -12,17 +14,17 @@ test('counter app', async () => {
const inc = () => counter.set(counter.get() + 1);
return (
<>
<div>Count: {count}</div>
<div data-testid="count">{count}</div>
<button type="button" onClick={inc}>
+1
</button>
</>
);
};
render(<App />);
await screen.findByText('Count: 0');
expect(screen.getByTestId('count')).toHaveTextContent('0');
await user.click(screen.getByRole('button'));
await screen.findByText('Count: 1');
expect(screen.getByTestId('count')).toHaveTextContent('1');
await user.click(screen.getByRole('button'));
await screen.findByText('Count: 2');
expect(screen.getByTestId('count')).toHaveTextContent('2');
});
1 change: 1 addition & 0 deletions tests/vitest-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@testing-library/jest-dom/vitest';
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"downlevelIteration": true,
"esModuleInterop": true,
"module": "nodenext",
"skipLibCheck": true,
"allowJs": true,
"verbatimModuleSyntax": true,
"noUncheckedIndexedAccess": true,
Expand Down
5 changes: 4 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export default defineConfig(({ mode }) => {
if (mode === 'test') {
return {
resolve: { alias: { 'use-signals': resolve('src') } },
test: { environment: 'jsdom' },
test: {
environment: 'jsdom',
setupFiles: ['./tests/vitest-setup.js'],
},
};
}
if (!DIR) {
Expand Down

0 comments on commit 04c57bd

Please sign in to comment.