@ochorocho/playwright-db-connector 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +295 -0
- package/dist/index.cjs +702 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +313 -0
- package/dist/index.d.ts +313 -0
- package/dist/index.js +657 -0
- package/dist/index.js.map +1 -0
- package/package.json +91 -0
package/README.md
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
# @ochorocho/playwright-db-connector
|
|
2
|
+
|
|
3
|
+
Playwright fixtures for database connectivity. Verify that your CRUD application actually writes to the database.
|
|
4
|
+
|
|
5
|
+
Supports **PostgreSQL**, **MySQL**, **MariaDB**, and **SQLite**.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
This package is hosted on [GitHub Packages](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-npm-registry). Configure the `@ochorocho` scope to use the GitHub registry by adding this to your project's `.npmrc`:
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
@ochorocho:registry=https://npm.pkg.github.com
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Then install the package:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @ochorocho/playwright-db-connector
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Install the driver for your database:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
# PostgreSQL
|
|
25
|
+
npm install pg
|
|
26
|
+
|
|
27
|
+
# MySQL or MariaDB
|
|
28
|
+
npm install mysql2
|
|
29
|
+
|
|
30
|
+
# SQLite
|
|
31
|
+
npm install better-sqlite3
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Quick Start
|
|
35
|
+
|
|
36
|
+
### 1. Configure your database in `playwright.config.ts`
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { defineConfig } from '@playwright/test';
|
|
40
|
+
|
|
41
|
+
export default defineConfig({
|
|
42
|
+
use: {
|
|
43
|
+
dbConfig: {
|
|
44
|
+
client: 'pg',
|
|
45
|
+
connection: {
|
|
46
|
+
host: 'localhost',
|
|
47
|
+
port: 5432,
|
|
48
|
+
user: 'test',
|
|
49
|
+
password: 'test',
|
|
50
|
+
database: 'myapp_test',
|
|
51
|
+
},
|
|
52
|
+
seedFiles: ['./tests/fixtures/schema.sql'],
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### 2. Use `db` in your tests
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { test, expect } from '@ochorocho/playwright-db-connector';
|
|
62
|
+
|
|
63
|
+
test('user is persisted after registration', async ({ db, page }) => {
|
|
64
|
+
await page.goto('/register');
|
|
65
|
+
await page.fill('[name=email]', 'alice@example.com');
|
|
66
|
+
await page.fill('[name=password]', 'secret123');
|
|
67
|
+
await page.click('button[type=submit]');
|
|
68
|
+
|
|
69
|
+
// Verify the user was stored in the database
|
|
70
|
+
await expect(db).toHaveRecord('users', { email: 'alice@example.com' });
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Each test runs inside a transaction that is automatically rolled back, giving you perfect isolation with zero cleanup.
|
|
75
|
+
|
|
76
|
+
## Configuration
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
use: {
|
|
80
|
+
dbConfig: {
|
|
81
|
+
// Required: database driver
|
|
82
|
+
client: 'pg', // 'pg' | 'mysql2' | 'better-sqlite3' | 'sqlite3'
|
|
83
|
+
|
|
84
|
+
// Required: connection details (object or connection string)
|
|
85
|
+
connection: {
|
|
86
|
+
host: 'localhost',
|
|
87
|
+
port: 5432,
|
|
88
|
+
user: 'test',
|
|
89
|
+
password: 'test',
|
|
90
|
+
database: 'myapp_test',
|
|
91
|
+
},
|
|
92
|
+
// OR: connection: process.env.DATABASE_URL,
|
|
93
|
+
// OR: connection: { filename: ':memory:' }, // SQLite
|
|
94
|
+
|
|
95
|
+
// Optional: cleanup strategy (default: 'transaction')
|
|
96
|
+
cleanupStrategy: 'transaction', // 'transaction' | 'delete' | 'truncate' | 'none'
|
|
97
|
+
|
|
98
|
+
// Optional: SQL files to run once per worker on startup
|
|
99
|
+
seedFiles: ['./schema.sql'],
|
|
100
|
+
|
|
101
|
+
// Optional: CSV files (TYPO3 format) to import once per worker on startup
|
|
102
|
+
seedCsvFiles: ['./seed.csv'],
|
|
103
|
+
|
|
104
|
+
// Optional: connection pool (ignored for SQLite)
|
|
105
|
+
pool: { min: 0, max: 5 },
|
|
106
|
+
|
|
107
|
+
// Optional: pass-through to knex
|
|
108
|
+
knexConfig: {},
|
|
109
|
+
},
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Cleanup Strategies
|
|
114
|
+
|
|
115
|
+
| Strategy | How it works | Speed | Use when |
|
|
116
|
+
|---|---|---|---|
|
|
117
|
+
| `transaction` (default) | Wraps each test in a transaction, rolls back after | Fastest | Most cases |
|
|
118
|
+
| `delete` | Tracks `haveInDatabase` inserts, deletes them after | Medium | DDL in tests (CREATE TABLE) |
|
|
119
|
+
| `truncate` | Truncates all touched tables after each test | Slower | Need a completely clean slate |
|
|
120
|
+
| `none` | No cleanup | - | You manage state yourself |
|
|
121
|
+
|
|
122
|
+
### Multi-Project (multiple databases)
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
export default defineConfig({
|
|
126
|
+
projects: [
|
|
127
|
+
{
|
|
128
|
+
name: 'postgres',
|
|
129
|
+
use: {
|
|
130
|
+
dbConfig: { client: 'pg', connection: { ... } },
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
name: 'sqlite',
|
|
135
|
+
use: {
|
|
136
|
+
dbConfig: { client: 'better-sqlite3', connection: { filename: ':memory:' } },
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## API
|
|
144
|
+
|
|
145
|
+
### Assertions (Playwright-style matchers)
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
await expect(db).toHaveRecord('users', { email: 'alice@example.com' });
|
|
149
|
+
await expect(db).not.toHaveRecord('users', { email: 'deleted@example.com' });
|
|
150
|
+
await expect(db).toHaveRecordCount(5, 'users');
|
|
151
|
+
await expect(db).toHaveRecordCount(2, 'users', { active: true });
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Assertions (Codeception-style methods)
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
await db.seeInDatabase('users', { email: 'alice@example.com' });
|
|
158
|
+
await db.dontSeeInDatabase('users', { email: 'deleted@example.com' });
|
|
159
|
+
await db.seeNumRecords(5, 'users');
|
|
160
|
+
await db.seeNumRecords(2, 'users', { active: true });
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Retrieval
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
const email = await db.grabFromDatabase('users', 'email', { id: 1 });
|
|
167
|
+
const names = await db.grabColumnFromDatabase('users', 'name', { active: true });
|
|
168
|
+
const entries = await db.grabEntriesFromDatabase('users', { role: 'admin' });
|
|
169
|
+
const count = await db.grabNumRecords('users');
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Manipulation
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
// Insert (tracked for auto-cleanup, returns primary key)
|
|
176
|
+
const userId = await db.haveInDatabase('users', {
|
|
177
|
+
name: 'Alice',
|
|
178
|
+
email: 'alice@example.com',
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Update (returns number of affected rows)
|
|
182
|
+
await db.updateInDatabase('users', { active: false }, { id: userId });
|
|
183
|
+
|
|
184
|
+
// Delete (returns number of deleted rows)
|
|
185
|
+
await db.deleteFromDatabase('users', { id: userId });
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Raw SQL
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
// Use ? for parameter bindings (knex normalizes across all databases)
|
|
192
|
+
const result = await db.query('SELECT * FROM users WHERE id = ?', [1]);
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Transactions
|
|
196
|
+
|
|
197
|
+
```typescript
|
|
198
|
+
await db.transaction(async (trx) => {
|
|
199
|
+
const userId = await trx.haveInDatabase('users', { name: 'Alice', email: 'a@test.com' });
|
|
200
|
+
await trx.haveInDatabase('posts', { user_id: userId, title: 'First Post' });
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Seed from SQL file
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
await db.loadSqlFile('./fixtures/extra-data.sql');
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### CSV datasets (TYPO3-style)
|
|
211
|
+
|
|
212
|
+
Seed and assert using CSV files in the [TYPO3 testing framework format](https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/Testing/FunctionalTesting/Index.html). Each table section starts with the table name in the first column, followed by column headers. Data rows begin with an empty first field. Multiple tables can be in one file.
|
|
213
|
+
|
|
214
|
+
```csv
|
|
215
|
+
"users","uid","name","email","active"
|
|
216
|
+
,1,"Alice","alice@example.com",1
|
|
217
|
+
,2,"Bob","bob@example.com",1
|
|
218
|
+
"posts","uid","user_id","title"
|
|
219
|
+
,1,1,"Hello World"
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Special values: `\NULL` is converted to SQL NULL.
|
|
223
|
+
|
|
224
|
+
**Import CSV data:**
|
|
225
|
+
```typescript
|
|
226
|
+
await db.importCsvFile('./fixtures/seed.csv');
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
**Assert database matches CSV:**
|
|
230
|
+
```typescript
|
|
231
|
+
// Throws with a detailed diff if any row doesn't match
|
|
232
|
+
await db.assertCsvDataSet('./fixtures/expected.csv');
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
**Auto-seed on startup** (in `playwright.config.ts`):
|
|
236
|
+
```typescript
|
|
237
|
+
dbConfig: {
|
|
238
|
+
seedFiles: ['./schema.sql'], // SQL schema first
|
|
239
|
+
seedCsvFiles: ['./seed.csv'], // Then CSV data
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Escape hatch (raw knex)
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
const knex = db.knex;
|
|
247
|
+
await knex.schema.createTable('temp', (t) => {
|
|
248
|
+
t.increments('id');
|
|
249
|
+
t.string('name');
|
|
250
|
+
});
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
## Composing with existing fixtures
|
|
254
|
+
|
|
255
|
+
If you already have custom Playwright fixtures, merge them:
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
import { test as dbTest, expect as dbExpect } from '@ochorocho/playwright-db-connector';
|
|
259
|
+
import { test as base, mergeTests, mergeExpects } from '@playwright/test';
|
|
260
|
+
|
|
261
|
+
const myTest = base.extend({
|
|
262
|
+
myFixture: async ({}, use) => { await use('hello'); },
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
export const test = mergeTests(myTest, dbTest);
|
|
266
|
+
export const expect = mergeExpects(dbExpect);
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## Development
|
|
270
|
+
|
|
271
|
+
```bash
|
|
272
|
+
# Install dependencies
|
|
273
|
+
mise install # Installs Node.js version from .mise.toml
|
|
274
|
+
npm install
|
|
275
|
+
|
|
276
|
+
# Build
|
|
277
|
+
npm run build
|
|
278
|
+
|
|
279
|
+
# Run unit tests
|
|
280
|
+
npm run test:unit
|
|
281
|
+
|
|
282
|
+
# Run SQLite integration tests
|
|
283
|
+
npm run test:integration
|
|
284
|
+
|
|
285
|
+
# Run E2E Playwright tests (uses the plugin itself)
|
|
286
|
+
npm run test:e2e
|
|
287
|
+
|
|
288
|
+
# Start Docker databases for multi-DB integration tests
|
|
289
|
+
docker compose -f tests/docker-compose.yml up -d
|
|
290
|
+
TEST_PG=1 TEST_MYSQL=1 npm run test:integration
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## License
|
|
294
|
+
|
|
295
|
+
MIT
|