---
title: Commit Worker Script to Version Control
impact: CRITICAL
impactDescription: Eliminates setup friction for team members; prevents CI failures
tags: setup, worker, git, mockServiceWorker, browser
---

## Commit Worker Script to Version Control

The `mockServiceWorker.js` file generated by `msw init` should be committed to Git. This ensures team members and CI environments can run browser tests without additional setup commands.

**Incorrect (gitignoring worker script):**

```gitignore
# .gitignore
# This forces every developer to run msw init manually
public/mockServiceWorker.js
```

```bash
# New team member clones repo, runs tests, gets cryptic errors
# because worker script doesn't exist
npm test
# Error: Cannot find mockServiceWorker.js
```

**Correct (commit and auto-update worker script):**

```bash
# Generate worker script with --save flag
npx msw init public/ --save
```

```json
// package.json - auto-updates worker on msw version changes
{
  "msw": {
    "workerDirectory": ["public"]
  }
}
```

```gitignore
# .gitignore - do NOT ignore the worker script
# public/mockServiceWorker.js  <- remove this line
```

**Monorepo configuration:**

```json
// Root package.json for monorepos
{
  "msw": {
    "workerDirectory": [
      "apps/web/public",
      "apps/admin/public"
    ]
  }
}
```

**When NOT to use this pattern:**
- If treating as generated artifact, ensure CI runs `msw init` before tests

Reference: [Managing the Worker](https://mswjs.io/docs/best-practices/managing-the-worker/)
