Skip to content

Commit

Permalink
feat: introduce vitest and first homepage tests (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
maneike authored Nov 28, 2024
1 parent 3ff5925 commit cf35b69
Show file tree
Hide file tree
Showing 5 changed files with 883 additions and 30 deletions.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,58 @@ stplr

This will scaffold a new project in the directory you're currently in.

### Testing

Stapler uses **Vitest** for unit testing, providing a fast and efficient testing environment. The monorepo structure enables targeted testing for specific packages or the entire project.

#### Running Tests

1. To run all tests in the monorepo:

```bash
pnpm test
```

This runs all test files across the monorepo.

2. To test the core package specifically:

```bash
pnpm test:core
```

This focuses testing on the core functionality of Stapler.

3. To use the interactive Vitest UI:

```bash
pnpm test:ui
```

This opens a browser-based interface to monitor and debug your tests in real-time.

#### To add unit tests for Stapler CLI or core packages:

1. Create a `__tests__` directory within the relevant package (e.g., `packages/core/__tests__/`).

2. Write your test files using a `.test.ts` or `.spec.ts` extension.

3. Use Vitest's utilities like `describe`, `it`, and `expect` for test cases.

4. Example:

```js
import { describe, it, expect } from 'vitest';
import { exampleFunction } from '../src/example';

describe('exampleFunction', () => {
it('should return the correct value', () => {
const result = exampleFunction('input');
expect(result).toBe('expected output');
});
});
```

### Versioning

We use [Changesets](https://github.com/changesets/changesets) for versioning.
Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
"dev": "turbo dev",
"lint": "turbo lint",
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"release": "changeset publish"
"release": "changeset publish",
"test": "vitest",
"test:core": "vitest packages/core",
"test:ui": "vitest --ui"
},
"devDependencies": {
"@changesets/cli": "^2.27.9",
"@types/node": "^22.5.4",
"@vitest/ui": "^2.1.5",
"memfs": "^4.14.0",
"prettier": "^3.2.5",
"turbo": "^2.1.1",
"typescript": "^5.4.5"
"typescript": "^5.4.5",
"vitest": "^2.1.5"
},
"packageManager": "pnpm@9.13.2+sha512.88c9c3864450350e65a33587ab801acf946d7c814ed1134da4a924f6df5a2120fd36b46aab68f7cd1d413149112d53c7db3a4136624cfd00ff1846a0c6cef48a",
"engines": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, it, expect, vi } from 'vitest';
import { homepageFiles } from '../../../../templates/homepage/installConfig';
import { templateGenerator } from '../../../../utils/generator/generator';
import { getTemplateDirectory } from '../../../../utils/getTemplateDirectory';
import { modifyHomepage } from '../install';

// Mock dependencies
vi.mock('../../../../utils/generator/generator');
vi.mock('../../../../utils/getTemplateDirectory');
vi.mock('../../../../utils/logger', () => ({
logger: {
withSpinner: vi.fn((_, __, callback) => callback({ succeed: vi.fn(), fail: vi.fn() })),
},
}));

describe('modifyHomepage', () => {
it('should correctly set up homepage files', async () => {
// Arrange
const destinationDirectory = '/mock/destination';
const mockTemplateDirectory = '/mock/template/directory';

// Mock getTemplateDirectory to return a specific path
vi.mocked(getTemplateDirectory).mockReturnValue(mockTemplateDirectory);

// Act
await modifyHomepage(destinationDirectory);

// Assert
expect(getTemplateDirectory).toHaveBeenCalledWith('/templates/homepage/files/');
expect(templateGenerator).toHaveBeenCalledWith(homepageFiles, mockTemplateDirectory, destinationDirectory);
});
});
Loading

0 comments on commit cf35b69

Please sign in to comment.