dlest
Version:
Jest for your data layer - test runner for analytics tracking implementations
414 lines (303 loc) โข 9.89 kB
Markdown
[](https://www.npmjs.com/package/dlest)
[](https://opensource.org/licenses/MIT)
[](https://nodejs.org)
**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.
- ๐งช **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
```bash
npm install --save-dev dlest
```
```bash
npx dlest init
```
This creates:
- `dlest.config.js` - Configuration file
- `tests/example.test.js` - Example test file
- `test-page.html` - Sample HTML page for testing
```bash
npx dlest
npx dlest --serve
npx dlest https://staging.example.com
npx dlest https://staging.example.com --auth-user=admin --auth-pass=senha
npx dlest --verbose
npx dlest https://production.example.com --ci
```
```javascript
// 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'
]);
});
});
```
DLest uses familiar Jest-like syntax:
```javascript
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'
});
});
```
Test analytics on staging or production environments:
```javascript
// 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 --ci
```
Debug with detailed event information:
```bash
npx dlest --verbose
```
Shows:
- All captured events with full data
- Expected vs found comparison
- DataLayer structure analysis
- Helpful error messages in Portuguese
DLest provides specialized matchers for data layer testing:
- `await expect(dataLayer).toHaveEvent(eventName, eventData?)` - Check if event exists
- `await expect(dataLayer).toHaveEventData(eventData)` - Check if any event contains specific data
- `await expect(dataLayer).toHaveEventCount(eventName, count)` - Verify event count
- `await expect(dataLayer).toHaveEventSequence(eventNames[])` - Validate event sequence
```javascript
// 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');
```
Create a `dlest.config.js` file in your project root:
```javascript
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
}
};
```
```bash
npx dlest serve
npx dlest serve --port 8080
npx dlest serve --root ./dist
npx dlest
npx dlest --serve
npx dlest --serve --serve-port 8080
npx dlest tests/purchase.test.js
npx dlest --browser=firefox
npx dlest --no-headless
npx dlest init
npx dlest init --template=ecommerce
npx dlest install
npm test
npm run test:serve
npm run serve
npm run serve:dev
```
DLest includes a built-in static file server for development:
```bash
npx dlest serve
npx dlest serve --port 8080
npx dlest serve --root ./dist
npx dlest serve --host 0.0.0.0
npx dlest serve --verbose
```
The 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
```bash
npx dlest init --template=basic
```
Includes tests for:
- Page view tracking
- Button click tracking
- Form submission tracking
```bash
npx dlest init --template=ecommerce
```
Includes tests for:
- Product view tracking
- Add to cart events
- Purchase completion
- Complete funnel sequences
DLest supports familiar Jest patterns:
```javascript
// 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
});
```
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.
- โ
Chromium (Chrome, Edge)
- โ
Firefox
- โ
WebKit (Safari)
- Node.js 16+
- Modern browsers via Playwright
**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-headless` to debug visually
**Port already in use?**
- DLest will automatically find an available port
- Or specify a different port: `npx dlest serve --port 8080`
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
```bash
git clone https://github.com/metricasboss/dlest.git
cd dlest
npm install
npm test
npm run dev
```
- ๐ [Documentation](https://github.com/metricasboss/dlest#readme)
- ๐ [Issue Tracker](https://github.com/metricasboss/dlest/issues)
- ๐ฌ [Discussions](https://github.com/metricasboss/dlest/discussions)
MIT ยฉ [MetricasBoss](https://github.com/metricasboss)