Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ npm-debug.log*
# Test artifacts
test-results/
playwright-report/
test-fixtures/sample-coverage/test-merged/

# Misc
*.tgz
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to this project will be documented in this file.

## [0.9.3] - 2024-12-24

### Enhanced

- **`collectServer: false` now skips `startServerCoverage()` entirely** - Previously only affected `finalizeCoverage()`
- No CDP connection attempts are made when server coverage is disabled
- Safe for static sites, SPAs, or deployed environments
- No `NODE_V8_COVERAGE`, `--inspect`, or `global-setup.ts` required

## [0.9.2] - 2024-12-22

### Fixed
Expand Down
99 changes: 97 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Now you can finally see the complete coverage picture for your Next.js applicati

- **Next.js focused** - Built specifically for Next.js applications
- **Client + Server coverage** - Collects coverage from both browser and Node.js server
- **Client-only mode** - Skip server coverage for static sites, SPAs, or deployed environments
- **Dev mode support** - Works with `next dev` (no build required), auto-detected
- **Production mode support** - Works with `next build && next start` using external source maps
- **Auto-detection** - Automatically detects dev vs production mode, no configuration needed
Expand Down Expand Up @@ -343,6 +344,100 @@ npx playwright test

Both modes produce identical Istanbul-compatible output that can be merged with Vitest coverage.

## Client-Only Mode

For scenarios where you don't need server-side coverage, use `collectServer: false`. This is useful for:

- **Static sites** - Next.js static exports (`next export` or `output: 'export'`)
- **SPAs** - Single page applications with external/serverless backends
- **Deployed environments** - Testing against staging or production URLs
- **Simpler setup** - No `NODE_V8_COVERAGE` or `--inspect` flags needed

### Configuration

Disable server coverage in your `playwright.config.ts`:

```typescript
export const nextcov: NextcovConfig = {
collectServer: false, // Skip all server coverage collection
outputDir: 'coverage/e2e',
reporters: ['html', 'lcov', 'json', 'text-summary'],
}
```

### Simplified Setup

With `collectServer: false`, you don't need a `global-setup.ts` file at all. The setup is simpler:

**1. Coverage fixture** (`e2e/fixtures.ts`) - same as before:
```typescript
import { test as base, expect } from '@playwright/test'
import { collectClientCoverage } from 'nextcov/playwright'

export const test = base.extend({
coverage: [
async ({ page }, use, testInfo) => {
await collectClientCoverage(page, testInfo, use)
},
{ scope: 'test', auto: true },
],
})

export { expect }
```

**2. Global teardown** (`e2e/global-teardown.ts`):
```typescript
import { finalizeCoverage, loadNextcovConfig } from 'nextcov/playwright'

export default async function globalTeardown() {
const config = await loadNextcovConfig()
await finalizeCoverage(config) // Only processes client coverage
}
```

**3. Run tests** - no special server flags needed:
```bash
# Just start your server normally and run tests
npm start &
npx playwright test

# Or test against a deployed environment
npx playwright test --config=playwright.staging.config.ts
```

### When to Use Client-Only Mode

| Scenario | Use `collectServer: false`? |
|----------|------------------|
| Testing Next.js with `next dev` or `next start` | No - use full mode for server coverage |
| Testing static export (`next export`) | Yes |
| Testing against deployed staging/production | Yes |
| Testing SPA with external API | Yes |
| Quick local testing without inspector setup | Yes |

### Behavior

When `collectServer: false`:
- `startServerCoverage()` becomes a no-op (safe to call, does nothing)
- `finalizeCoverage()` only processes client-side coverage from Playwright
- No CDP connection attempts are made

### Server-Only Mode

For scenarios where you only want server coverage (e.g., API testing without browser), use `collectClient: false`:

```typescript
export const nextcov: NextcovConfig = {
collectClient: false, // Skip client coverage collection
outputDir: 'coverage/e2e',
}
```

When `collectClient: false`:
- `collectClientCoverage()` still needs to be called (for test fixtures), but collected data is ignored during finalization
- `finalizeCoverage()` only processes server-side coverage

## Merging with Vitest Coverage

The main power of nextcov is combining E2E coverage with unit test coverage.
Expand Down Expand Up @@ -505,8 +600,8 @@ Finalizes coverage collection and generates reports. Call in globalTeardown.
| `include` | `string[]` | `['src/**/*']` | Glob patterns to include |
| `exclude` | `string[]` | `['node_modules/**']` | Glob patterns to exclude |
| `reporters` | `string[]` | `['html', 'lcov', 'json']` | Report formats |
| `collectServer` | `boolean` | `true` | Collect server-side coverage |
| `collectClient` | `boolean` | `true` | Collect client-side coverage |
| `collectServer` | `boolean` | `true` | Collect server-side coverage (set `false` for static sites, SPAs) |
| `collectClient` | `boolean` | `true` | Collect client-side coverage from Playwright |
| `cleanup` | `boolean` | `true` | Clean up temp files |
| `cdpPort` | `number` | `9230` | CDP port for triggering v8.takeCoverage() |
| `log` | `boolean` | `false` | Enable verbose logging output |
Expand Down
39 changes: 34 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nextcov",
"version": "0.9.2",
"version": "0.9.3",
"description": "Collect test coverage for Playwright E2E tests in Next.js applications",
"author": "Steve Zhang",
"license": "MIT",
Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,10 @@ describe('CLI', () => {
const outputDir = join(projectRoot, 'test-fixtures', 'sample-coverage', 'test-merged')

// Clean up previous test output to ensure fresh merge
if (realExistsSync(outputDir)) {
rmSync(outputDir, { recursive: true })
try {
rmSync(outputDir, { recursive: true, force: true })
} catch {
// Directory may not exist, ignore
}

const output = execSync(
Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ describe('config', () => {

expect(config.reporters).toEqual(['json', 'text'])
})

it('should allow disabling server collection', () => {
const config = resolveNextcovConfig({
collectServer: false,
})
expect(config.collectServer).toBe(false)
expect(config.collectClient).toBe(true)
})

it('should allow disabling client collection', () => {
const config = resolveNextcovConfig({
collectClient: false,
})
expect(config.collectServer).toBe(true)
expect(config.collectClient).toBe(false)
})

})

describe('normalizePath', () => {
Expand Down
12 changes: 10 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,18 @@ export interface NextcovConfig {
*/
v8CoverageDir?: string

/** Whether to collect server-side coverage (default: true) */
/**
* Collect server-side coverage (default: true).
* When false, startServerCoverage() becomes a no-op and finalizeCoverage()
* skips server coverage collection. Use this for static sites, SPAs, or
* deployed environments where no Node.js server with inspector is available.
*/
collectServer?: boolean

/** Whether to collect client-side coverage (default: true) */
/**
* Collect client-side coverage (default: true).
* When false, client coverage from Playwright is not collected.
*/
collectClient?: boolean

/** Source files root relative to project root (default: './src') */
Expand Down
Loading