In Playwright, what does running the tests with -u actually rewrite, and what does it leave untouched?
answer
- Short for update snapshots
- Scope is the run, not the repository
- Filters and projects narrow what is rewritten
- The platform in the path limits it too
- Nothing prunes an orphaned reference
basics
~20 sIt rewrites reference images only for the tests that command actually executed, and only for the projects and the platform that ran. Everything filtered out, every other project, and every orphaned file stays exactly as it was.
solid answer
~40 s`-u` is short for `--update-snapshots`, and its scope is the run, not the repository. A command narrowed by a file path, a title filter or `--project` regenerates references for exactly those tests; anything the filter excluded keeps its old image. Because the reference path carries the project name and the platform, a run on one machine can only refresh that machine's variants — the other operating system's files are untouched and stay stale. The flag also takes an optional mode in Playwright 1.63: `all`, `changed`, `missing` or `none`, where an ordinary run behaves as `missing`. And nothing is ever deleted: a reference whose test was renamed or removed lingers on disk, because the runner only writes files, it does not prune them.
code
bash · 2 linesnpx playwright test tests/statement.spec.ts -g "balance widget" --project=chromium -u
npx playwright test tests/statement.spec.ts -g "balance widget" --project=chromiumgo deeper
Know that the flag is short for update snapshots and that it rewrites the reference images for the tests you just ran, not for the whole suite.
Explain the three boundaries: the tests the filter selected, the projects that ran, and the platform of the machine you ran on.
Demonstrate the working habit: narrow the run, regenerate on a machine like the one that runs the suite, then re-run without the flag so the green result comes from a comparison.
Own where regeneration is allowed to happen at all, since the flag turns whatever the application rendered into the recorded expectation and nothing downstream can tell that apart from a verified one.
## What the flag is `-u` is the short form of `--update-snapshots` on the Playwright test command. It changes what the runner does when a capture and its reference disagree: instead of only reporting the difference, it writes the new image over the old one. In Playwright 1.63 the flag takes an optional mode rather than being a plain boolean: | Mode | Meaning | |---|---| | `all` | Regenerate references for the tests that ran | | `changed` | Regenerate the references that did not match | | `missing` | Create only references that do not exist — how an ordinary run already behaves | | `none` | Write nothing at all; a missing or mismatched reference is just a failure | The same values are available as `updateSnapshots` in `playwright.config.ts`, which is where a project pins the behaviour for automated runs. ## The three boundaries of a regeneration 1. **Only the tests that ran.** Filters compose with the flag. `npx playwright test tests/statement.spec.ts -g "balance" -u` refreshes the references belonging to the matching tests in that one file and nothing else. This is the useful property: a targeted redesign does not sweep the whole suite's baselines. 2. **Only the projects that ran.** Reference paths carry the project name, so `--project=chromium -u` cannot touch another project's files. Regenerating a change that affects the whole matrix means running the whole matrix. 3. **Only this platform.** The path also carries the platform, so a regeneration on one operating system leaves the other system's references exactly as they were. A suite whose references were generated on a laptop and consumed on a Linux runner needs the regeneration to happen where the run happens. ## What it never does - It does not delete anything. A reference for a renamed or deleted test stays on disk forever; nothing in the run prunes it, and the orphan is invisible because no assertion looks for it. - It does not review anything. The flag replaces the recorded expectation with whatever the application produced at that moment, so the verdict after a regeneration is meaningless until the suite is run again without the flag. - It does not repair a path problem. If the reference file name changed — a renamed test with an auto-generated name, a new project, a different platform — the flag creates the new file and quietly leaves the old one behind. ## The mechanics that trip people up - Running with `-u` from a machine whose fonts or rendering differ from the machine the suite normally runs on produces references that immediately fail everywhere else. - Regenerating with a filter applied and then dropping the filter shows the rest of the suite still failing, which looks like the flag did not work. - Adding a project to the config means every screenshot assertion has a missing reference for that project, so the first run there is a write-and-fail regardless of the flag. ## Running it deliberately A regeneration is worth treating as its own command, not a reflex when a check goes red: 1. Narrow the run to the tests whose appearance is genuinely meant to change. 2. Regenerate on the same kind of machine that runs the suite normally. 3. Re-run without the flag, so the pass you report comes from a comparison rather than from a write. 4. Look at the images the command changed before they leave your working tree — the flag has no opinion about whether the new picture is correct.
- After a regeneration on a developer machine, the same screenshot checks fail on the shared runner. Why?Reference paths carry the platform, and rendering differs between systems. A regeneration on one machine only writes that machine's variants, so the runner either compares against untouched older files or finds none for its own platform and takes the write-and-fail path.
- How would you find reference images that no test compares against any more?Nothing in the run reports them, because an orphan is simply a file no assertion looks up. They have to be found by comparing the files on disk with the paths the suite generates, which is why explicit snapshot names and a predictable path template make the cleanup tractable.
- What is the risk of leaving the update mode at its default on an automated runner?The default creates missing references, so a run on a machine nobody inspects can author a baseline. Pinning the mode to none there means an absent reference is only ever a failure, and baselines can enter the repository from a place where they were looked at.
saying these in an interview costs you the question
- Thinks the flag regenerates every reference in the repository
- Assumes one regeneration covers every project in the matrix
- Believes references for other platforms are refreshed too
- Expects orphaned reference files to be deleted automatically
- Reports a suite green on the same run that rewrote its baselines