You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.0 KiB
119 lines
3.0 KiB
# Ghost Data Factory
|
|
|
|
A minimal test data factory for Ghost e2e tests, written in TypeScript.
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
e2e/data-factory/ # Source files (TypeScript) - committed to git
|
|
├── factories/
|
|
│ ├── factory.ts # Base factory class
|
|
│ └── posts/
|
|
│ └── post-factory.ts # Post factory implementation
|
|
├── persistence/
|
|
│ ├── adapter.ts # Persistence interface
|
|
│ └── adapters/
|
|
│ └── knex.ts # Knex SQL adapter
|
|
├── index.ts # Main exports
|
|
├── utils.ts # Utility functions
|
|
└── tests/
|
|
└── post-factory.test.js # Test file (JavaScript)
|
|
|
|
e2e/build/data-factory/ # Compiled files (JavaScript) - gitignored
|
|
└── ... (generated by TypeScript compiler)
|
|
```
|
|
|
|
## Setup
|
|
|
|
This is part of the Ghost e2e test suite. All dependencies are managed by the main Ghost monorepo.
|
|
|
|
1. **Start Ghost development server** (provides database):
|
|
```bash
|
|
yarn dev
|
|
```
|
|
|
|
2. **Configure database connection** (optional - uses Ghost's database by default):
|
|
```bash
|
|
cp e2e/data-factory/.env.example e2e/data-factory/.env
|
|
# Edit .env if using different database credentials
|
|
```
|
|
|
|
3. **Build the e2e package** (includes data-factory):
|
|
```bash
|
|
cd e2e && yarn build
|
|
```
|
|
|
|
## Usage
|
|
|
|
### In Tests
|
|
|
|
```javascript
|
|
// Import from the built files
|
|
const {PostFactory, KnexPersistenceAdapter} = require('../build/data-factory');
|
|
|
|
// Set up
|
|
const db = knex(dbConfig);
|
|
const adapter = new KnexPersistenceAdapter(db);
|
|
const postFactory = new PostFactory(adapter);
|
|
|
|
// Build in memory (not persisted)
|
|
const draftPost = postFactory.build({
|
|
title: 'My Draft',
|
|
status: 'draft'
|
|
});
|
|
|
|
// Create and persist
|
|
const publishedPost = await postFactory.create({
|
|
title: 'My Published Post',
|
|
status: 'published'
|
|
});
|
|
```
|
|
|
|
### Running Tests
|
|
|
|
```bash
|
|
# From e2e directory
|
|
yarn test:factory
|
|
|
|
# This will:
|
|
# 1. Build the TypeScript files
|
|
# 2. Run Playwright tests in the data-factory directory
|
|
|
|
# Or manually:
|
|
yarn build
|
|
yarn playwright test data-factory
|
|
```
|
|
|
|
## Development
|
|
|
|
### Build Process
|
|
|
|
- **TypeScript source files** (`*.ts`) are in `e2e/data-factory/`
|
|
- **Compiled JavaScript** (`*.js`) goes to `e2e/build/data-factory/`
|
|
- The `e2e/build/` directory is gitignored
|
|
- Always rebuild after making changes: `cd e2e && yarn build`
|
|
|
|
### Adding New Factories
|
|
|
|
1. Create a new factory class extending `Factory<TOptions, TResult>`
|
|
2. Implement the `build()` method
|
|
3. Set `name` and `entityType` properties
|
|
4. Export from `index.ts`
|
|
|
|
Example:
|
|
```typescript
|
|
export class UserFactory extends Factory<Partial<User>, User> {
|
|
name = 'user';
|
|
entityType = 'users';
|
|
|
|
build(options: Partial<User> = {}): User {
|
|
// Generate test data
|
|
}
|
|
}
|
|
```
|
|
|
|
## Notes
|
|
|
|
- The test file (`test/post-factory.test.js`) is JavaScript because it's a standalone script
|
|
- All factory code is TypeScript and must be compiled before use
|
|
- The compiled files in `e2e/build/` should never be committed to git |