dlest
v0.5.0
Published
Jest for your data layer - test runner for analytics tracking implementations
Maintainers
Readme
DLest
Jest for your data layer - A test runner specifically designed for testing analytics tracking implementations.
DLest provides familiar Jest-like syntax for validating data layer events in web applications. Built on top of Playwright, it allows developers to write unit tests for their analytics implementations and catch tracking bugs before they reach production.
Why DLest?
- 🧪 Jest-like API - Familiar syntax for JavaScript developers
- 🎯 Purpose-built - Designed specifically for analytics testing
- 🚀 Fast & Reliable - Built on Playwright for real browser testing
- 🌐 Remote Testing - Test staging/production sites directly
- 📦 Zero Config - Works out of the box with sensible defaults
- 🔧 Extensible - Custom matchers and templates for your needs
- 🛠️ Built-in Server - No need for Python or external tools
- 🔍 Verbose Mode - Detailed debugging with event inspection
- 💡 Smart Error Messages - Helpful tips in Portuguese
Quick Start
Installation
npm install --save-dev dlestInitialize Project
npx dlest initThis creates:
dlest.config.js- Configuration filetests/example.test.js- Example test filetest-page.html- Sample HTML page for testing
Run Tests
# Test local development
npx dlest
# Test with local server
npx dlest --serve
# Test remote URL (NEW!)
npx dlest https://staging.example.com
# Test with authentication
npx dlest https://staging.example.com --auth-user=admin --auth-pass=senha
# Verbose mode for debugging
npx dlest --verbose
# CI mode for pipelines
npx dlest https://production.example.com --ciReal-World Example
Testing an E-commerce Purchase Flow
// tests/purchase-flow.test.js
const { test, expect } = require('dlest');
test.describe('Purchase Flow', () => {
test('complete purchase journey', async ({ page, dataLayer }) => {
// 1. View product
await page.goto('http://localhost:3000/products.html');
await expect(dataLayer).toHaveEvent('view_item', {
value: 99.99,
currency: 'USD'
});
// 2. Add to cart
await page.click('#add-to-cart');
await expect(dataLayer).toHaveEvent('add_to_cart', {
value: 99.99,
items: expect.arrayContaining([
expect.objectContaining({
item_id: 'prod-123',
quantity: 1
})
])
});
// 3. Complete purchase
await page.click('#checkout');
await page.waitForTimeout(1000); // Wait for async purchase
await expect(dataLayer).toHaveEvent('purchase', {
transaction_id: expect.any(String),
value: 99.99,
currency: 'USD'
});
// 4. Verify the complete sequence
await expect(dataLayer).toHaveEventSequence([
'view_item',
'add_to_cart',
'purchase'
]);
});
});Writing Tests
DLest uses familiar Jest-like syntax:
const { test, expect } = require('dlest');
test('page view tracking', async ({ page, dataLayer }) => {
await page.goto('http://localhost:3000');
await expect(dataLayer).toHaveEvent('page_view');
});
test('purchase tracking', async ({ page, dataLayer }) => {
await page.goto('/product/123');
await page.click('#add-to-cart');
await page.click('#checkout');
await expect(dataLayer).toHaveEvent('purchase', {
transaction_id: expect.any(String),
value: expect.any(Number),
currency: 'BRL'
});
});Remote Testing (NEW!)
Test analytics on staging or production environments:
// Test remote URL directly
npx dlest https://staging.mysite.com
// With authentication
npx dlest https://staging.mysite.com --auth-user=admin --auth-pass=senha
// Use environment variables
export DLEST_BASE_URL=https://staging.mysite.com
export DLEST_AUTH_USER=admin
export DLEST_AUTH_PASS=senha
npx dlest
// Custom test file for remote
npx dlest https://production.mysite.com --test=tests/production.test.js
// CI/CD Pipeline
npx dlest $STAGING_URL --ciVerbose Mode
Debug with detailed event information:
npx dlest --verboseShows:
- All captured events with full data
- Expected vs found comparison
- DataLayer structure analysis
- Helpful error messages in Portuguese
Custom Matchers
DLest provides specialized matchers for data layer testing:
await expect(dataLayer).toHaveEvent(eventName, eventData?)- Check if event existsawait expect(dataLayer).toHaveEventData(eventData)- Check if any event contains specific dataawait expect(dataLayer).toHaveEventCount(eventName, count)- Verify event countawait expect(dataLayer).toHaveEventSequence(eventNames[])- Validate event sequence
// Event existence
await expect(dataLayer).toHaveEvent('purchase');
// Event with specific data
await expect(dataLayer).toHaveEvent('purchase', { value: 99.90 });
// Event count
await expect(dataLayer).toHaveEventCount('page_view', 1);
// Event sequence
await expect(dataLayer).toHaveEventSequence(['page_view', 'add_to_cart', 'purchase']);
// Negation
await expect(dataLayer).not.toHaveEvent('error');Configuration
Create a dlest.config.js file in your project root:
module.exports = {
// Base URL for tests
baseURL: 'http://localhost:3000',
// Browser settings
browsers: ['chromium'], // chromium, firefox, webkit
headless: true,
// Test settings
timeout: 30000,
testDir: './tests',
testMatch: ['**/*.test.js'],
// Data layer settings
dataLayer: {
variableName: 'dataLayer', // Custom variable name
waitTimeout: 5000, // Event wait timeout
}
};CLI Commands
# Start development server
npx dlest serve
npx dlest serve --port 8080 # Custom port
npx dlest serve --root ./dist # Custom root directory
# Run all tests
npx dlest
# Run tests with auto-server (recommended)
npx dlest --serve
npx dlest --serve --serve-port 8080
# Run specific test file
npx dlest tests/purchase.test.js
# Use different browser
npx dlest --browser=firefox
# Run with visible browser (non-headless)
npx dlest --no-headless
# Initialize project
npx dlest init
# Initialize with e-commerce template
npx dlest init --template=ecommerce
# Install Playwright browsers
npx dlest install
# NPM Scripts (alternative to npx commands)
npm test # Run tests
npm run test:serve # Run tests with auto-server
npm run serve # Start development server
npm run serve:dev # Start server with verbose loggingDevelopment Server
DLest includes a built-in static file server for development:
# Start server on default port (3000)
npx dlest serve
# Custom port
npx dlest serve --port 8080
# Custom root directory
npx dlest serve --root ./dist
# Custom host (for network access)
npx dlest serve --host 0.0.0.0
# Verbose logging
npx dlest serve --verboseThe server provides:
- ✅ Static file serving with proper MIME types
- ✅ Directory listings for folders without index.html
- ✅ SPA support (serves index.html for routes)
- ✅ CORS headers for development
- ✅ Automatic port detection if default is busy
- ✅ Graceful shutdown with Ctrl+C
Templates
DLest comes with pre-built templates:
Basic Template
npx dlest init --template=basicIncludes tests for:
- Page view tracking
- Button click tracking
- Form submission tracking
E-commerce Template
npx dlest init --template=ecommerceIncludes tests for:
- Product view tracking
- Add to cart events
- Purchase completion
- Complete funnel sequences
Jest-like Features
DLest supports familiar Jest patterns:
// Test suites
test.describe('E-commerce Flow', () => {
test('product view', async ({ page, dataLayer }) => {
// Test implementation
});
});
// Expect helpers
expect.any(String)
expect.arrayContaining([...])
expect.objectContaining({...})
// Hooks (planned for future releases)
beforeEach(({ page, dataLayer }) => {
// Setup
});Problem Statement
Analytics tracking breaks frequently due to:
- Frontend changes removing tracking elements
- Refactoring that changes event parameters
- A/B tests breaking conversion tracking
- Missing events from JavaScript errors
- No systematic testing of analytics code
DLest solves this by enabling automated testing of your data layer implementation.
Browser Support
- ✅ Chromium (Chrome, Edge)
- ✅ Firefox
- ✅ WebKit (Safari)
Requirements
- Node.js 16+
- Modern browsers via Playwright
Troubleshooting
Common Issues
Tests timing out?
- Increase timeout in config:
timeout: 60000 - Check if your server is running:
npx dlest serve
Events not being captured?
- Verify dataLayer variable name in config
- Check browser console for JavaScript errors
- Use
--no-headlessto debug visually
Port already in use?
- DLest will automatically find an available port
- Or specify a different port:
npx dlest serve --port 8080
Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development
# Clone the repo
git clone https://github.com/metricasboss/dlest.git
cd dlest
# Install dependencies
npm install
# Run tests
npm test
# Start development
npm run devSupport
License
MIT © MetricasBoss
