Seed Paystack test data in two places: (1) in Paystack test mode via API — create test plans, subaccounts, and customers using your sk_test key; (2) in your own database — insert matching orders, subscriptions, and payment records. Use a seed script that runs before your test suite and cleans up after. Reference the Paystack test objects by their codes (SUB_xxx, PLN_xxx) in your database seeds.
// scripts/seed-test-data.js
// Run before test suite: node scripts/seed-test-data.js
require('dotenv').config({ path: '.env.test' });
const db = require('./db');
async function paystackPost(endpoint, body) {
var res = await fetch('https://api.paystack.co' + endpoint, {
method: 'POST',
headers: {
Authorization: 'Bearer ' + process.env.PAYSTACK_SECRET_KEY, // sk_test_...
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
return res.json();
}
async function seed() {
// Create test subaccount
var subaccountRes = await paystackPost('/subaccount', {
business_name: 'Test Vendor',
settlement_bank: '057', // Zenith (test bank code)
account_number: '0000000001', // fake in test mode
percentage_charge: 80,
});
var subaccountCode = subaccountRes.data.subaccount_code;
// Create test plan for subscriptions
var planRes = await paystackPost('/plan', {
name: 'Test Monthly Plan',
interval: 'monthly',
amount: 500000, // NGN 5,000
});
var planCode = planRes.data.plan_code;
// Seed your database with matching records
await db.vendors.upsert({ id: 1, name: 'Test Vendor', subaccount_code: subaccountCode });
await db.plans.upsert({ id: 1, name: 'Monthly', paystack_plan_code: planCode, amount: 5000 });
await db.orders.upsert({ id: 1, email: 'test@example.com', amount: 10000, status: 'pending' });
// Save codes for use in tests
require('fs').writeFileSync('test-seeds.json', JSON.stringify({ subaccountCode, planCode }));
console.log('Test data seeded:', { subaccountCode, planCode });
process.exit(0);
}
seed().catch(e => { console.error(e); process.exit(1); });
// tests/split-payment.test.js
const seeds = require('../test-seeds.json');
describe('Split payment checkout', () => {
it('creates transaction with correct subaccount code', async () => {
var paystack = require('../lib/paystack');
paystack.initializeTransaction = jest.fn().mockResolvedValue({
status: true,
data: { authorization_url: 'https://checkout.paystack.com/test', reference: 'ref_001' },
});
await checkoutWithSplit({
email: 'buyer@test.com',
amount: 10000,
vendor_id: 1, // Matches seeded vendor with known subaccount_code
});
expect(paystack.initializeTransaction).toHaveBeenCalledWith(
expect.objectContaining({ subaccount: seeds.subaccountCode })
);
});
});
Get weekly developer tips
Join 25,000+ developers. Practical guides, job tips, and new content — straight to your inbox.
No spam. Unsubscribe anytime.
This guide is part of the Paystack testing guide.
KES 2,999
The definitive guide to building payment systems with Paystack — from your first transaction to production-grade payment infrastructure across Africa. 9 modules, 47 lessons. Free preview available.
Start Paystack Course