Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: restructuring #51

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
69debc9
chore: initial conversion to typescript
svallory Jan 20, 2025
f342100
chore: cleanup
svallory Jan 21, 2025
9eeca3c
chore: refactoring
svallory Jan 21, 2025
48db831
docs: add description of each task
svallory Jan 27, 2025
21c2236
refactor: bring apps inside the plugma package
svallory Jan 28, 2025
04d1135
fix: tasks implementations
svallory Jan 29, 2025
71285b2
test: finish test implementation for all tasks and commands
svallory Jan 29, 2025
57c238b
fix: remove file cleanup after dev/preview
svallory Jan 31, 2025
43f81fe
WIP: before refactoring verification
svallory Feb 1, 2025
95647a3
WIP: plan checkpoint - tests passing
svallory Feb 2, 2025
9f0d8fe
WIP: new tests
svallory Feb 2, 2025
6130759
tests: add test utilities and sandbox
svallory Feb 3, 2025
78cf15a
chore: build scripts improvements
svallory Feb 3, 2025
f25780f
fix: lots of small issues
svallory Feb 3, 2025
72908e8
refactor: turn banner.js into plugma-runtime app
svallory Feb 4, 2025
b123fa6
fix: logger not working in browser
svallory Feb 5, 2025
afc543a
fix: omg I can't believe everything works
svallory Feb 5, 2025
8a12043
chore: rename placeholder-ui task to wrap-plugin-ui
svallory Feb 6, 2025
8ee3748
tests: fixing tests
svallory Feb 6, 2025
0d2a8d1
fix: cleanup utility
svallory Feb 6, 2025
4899754
tests: more fixes (only a few dev command tests failing)
svallory Feb 6, 2025
345525d
tests: all tests are passing
svallory Feb 6, 2025
b5a28ee
feature: plugma test command
svallory Feb 8, 2025
d8b7b9c
fix: remove unnecessary happy-dom and jsdom dependencies
svallory Feb 11, 2025
de25f23
fix: package name
svallory Feb 11, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .cursor/rules/test-location.mdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
description: Describes where to write test files
globs: *.test.ts
---
test files are placed in the same folder of the file they test. For example [get-files.test.ts](mdc:plugma/src/tasks/common/get-files.test.ts) is beside [get-files.ts](mdc:plugma/src/tasks/common/get-files.ts)
163 changes: 163 additions & 0 deletions .cursor/rules/vitest.mdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
---
description: Vitest usage guide
globs: **/*.test.ts
---
# Vitest Usage Guide

## Basic Test Structure

Tests are organized following these patterns:

~~~typescript
import { beforeEach, describe, expect, test, vi } from 'vitest';

describe('Component/Feature Name', () => {
// Setup and teardown
beforeEach(() => {
vi.clearAllMocks();
});

describe('Specific Aspect', () => {
test('should do something specific', () => {
// Test implementation
});
});
});
~~~

## Mocking Patterns

### Module Mocking

1. **Hoisted Mocks**: Use `vi.hoisted()` for mocks needed across multiple tests:
~~~typescript
const mocks = vi.hoisted(() => ({
registerCleanup: vi.fn(),
getDirName: vi.fn(),
}));

vi.mock('#utils/cleanup.js', () => ({
registerCleanup: mocks.registerCleanup,
}));
~~~

2. **Partial Module Mocks**: Keep original functionality while mocking specific exports:
~~~typescript
vi.mock('node:fs', () => ({
...vi.importActual('node:fs'),
readFileSync: vi.fn((path) => {
// Custom implementation
}),
}));
~~~

3. **External Module Mocks**: Mock external dependencies:
~~~typescript
vi.mock('vite', () => ({
createServer: vi.fn(),
}));
~~~

### Mock Implementations

1. **Mock Classes**: Create mock implementations for complex objects:
~~~typescript
class MockWebSocketServer extends EventEmitter {
clients = new Set();
close = vi.fn().mockResolvedValue(undefined);
// ... other methods
}
~~~

2. **Mock Functions**: Use `vi.fn()` with specific implementations:
~~~typescript
const mockFn = vi.fn()
.mockReturnValue(defaultValue)
.mockImplementation((arg) => {
// Custom implementation
});
~~~

3. **Async Mocks**: Handle promises in mocks:
~~~typescript
mockFunction.mockResolvedValue(value); // for Promise.resolve
mockFunction.mockRejectedValue(error); // for Promise.reject
~~~

## Testing Patterns

### Cleanup and Reset

1. **Clear Mocks**: Reset all mocks before each test:
~~~typescript
beforeEach(() => {
vi.clearAllMocks();
});
~~~

2. **Mock Reset**: Reset specific mocks:
~~~typescript
mockFunction.mockClear(); // Clear usage data
mockFunction.mockReset(); // Clear usage and implementation
mockFunction.mockRestore(); // Restore original implementation
~~~

### Assertions

1. **Function Calls**:
~~~typescript
expect(mockFn).toHaveBeenCalled();
expect(mockFn).toHaveBeenCalledWith(expect.any(Function));
expect(mockFn).toHaveBeenCalledTimes(1);
~~~

2. **Objects and Values**:
~~~typescript
expect(result).toBe(value); // Strict equality
expect(object).toEqual(expected); // Deep equality
expect(object).toMatchObject(partial); // Partial object match
~~~

3. **Async Tests**:
~~~typescript
await expect(asyncFn()).rejects.toThrow('error message');
await expect(asyncFn()).resolves.toBe(value);
~~~

### Event Testing

1. **Event Emitters**:
~~~typescript
mockServer.emit('connection', mockClient, { url: '?source=test' });
expect(mockClient.send).toHaveBeenCalledWith(
expect.stringContaining('"event":"client_list"')
);
~~~

2. **Error Events**:
~~~typescript
await expect(
() => new Promise((_, reject) => {
server.on('error', reject);
server.emit('error', new Error('Test error'));
})
).rejects.toThrow('Expected error');
~~~

## Best Practices

1. **Organize Tests**: Group related tests using nested `describe` blocks
2. **Mock Isolation**: Use `beforeEach` to reset mocks and state
3. **Type Safety**: Maintain type safety in mocks using TypeScript interfaces
4. **Error Cases**: Test both success and error scenarios
5. **Clear Descriptions**: Use descriptive test names that explain the expected behavior
6. **Avoid Test Interdependence**: Each test should be independent and not rely on other tests

## Common Gotchas

1. **Async Cleanup**: Always handle async cleanup in tests
2. **Mock Scope**: Be aware of mock hoisting and scoping rules
3. **Event Order**: Consider event timing in async tests
4. **Type Assertions**: Use proper type assertions for mocked objects
5. **Promise Handling**: Always await promises in async tests
~~~
1 change: 0 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true
Expand Down
16 changes: 0 additions & 16 deletions .prettierrc

This file was deleted.

Loading