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.
|
|
3 months ago | |
|---|---|---|
| .. | ||
| factories | 3 months ago | |
| persistence | 3 months ago | |
| setup | 3 months ago | |
| .env.example | 3 months ago | |
| README.md | 3 months ago | |
| index.ts | 3 months ago | |
| utils.ts | 3 months ago | |
README.md
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.
-
Start Ghost development server (provides database):
yarn dev -
Configure database connection (optional - uses Ghost's database by default):
cp e2e/data-factory/.env.example e2e/data-factory/.env # Edit .env if using different database credentials -
Build the e2e package (includes data-factory):
cd e2e && yarn build
Usage
In Tests
// 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
# 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 ine2e/data-factory/ - Compiled JavaScript (
*.js) goes toe2e/build/data-factory/ - The
e2e/build/directory is gitignored - Always rebuild after making changes:
cd e2e && yarn build
Adding New Factories
- Create a new factory class extending
Factory<TOptions, TResult> - Implement the
build()method - Set
nameandentityTypeproperties - Export from
index.ts
Example:
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