tests/sims/dockerbuild.test.ts
The automated tests for the /simulators/container-layers simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.
- Once a step misses the cache every later step rebuilds, and a code edit reruns npm ci only when the code is copied before it.
- Files in .dockerignore never bust the cache, undoing a change hits the older layers again, and --no-cache rebuilds everything.
// /simulators/container-layers: Docker's build cache across edits, .dockerignore and --no-cache.
import { test } from 'node:test';
import { Builder, DOCKERFILES, type Context } from '../../src/lib/sims/dockerBuild.ts';
import { checker } from './check.ts';
test('once a step misses the cache every later step rebuilds, and a code edit reruns npm ci only when the code is copied before it', () => {
const { ok, done } = checker();
for (const DF of DOCKERFILES) {
const b = new Builder();
const ctx: Context = { 'package.json': 1, 'package-lock.json': 1, 'src/server.js': 1, 'src/routes.js': 1, 'README.md': 1, '.git/': 1 };
ok(b.build(DF.lines, ctx, []).steps.every((s) => !s.hit), `${DF.id}: first build should miss everywhere`);
const again = b.build(DF.lines, ctx, []);
ok(again.steps.every((s) => s.hit) && again.seconds === 0, `${DF.id}: an unchanged rebuild should hit everywhere`);
ctx['src/server.js']++;
const r = b.build(DF.lines, ctx, []);
const first = r.steps.findIndex((s) => !s.hit);
ok(r.steps.slice(first).every((s) => !s.hit), `${DF.id}: a step after the first miss was a hit`);
const npm = r.steps.find((s) => s.instr.arg === 'npm ci')!.hit;
ok(DF.id === 'deps-first' ? npm : !npm, `${DF.id}: npm ci ${npm ? 'hit' : 'missed'} after a code edit`);
}
done();
});
test('files in .dockerignore never bust the cache, undoing a change hits the older layers again, and --no-cache rebuilds everything', () => {
const { ok, done } = checker();
for (const DF of DOCKERFILES) {
const b = new Builder();
const ctx: Context = { 'package.json': 1, 'package-lock.json': 1, 'src/server.js': 1, 'src/routes.js': 1, 'README.md': 1, '.git/': 1 };
b.build(DF.lines, ctx, ['README.md', '.git/']);
ctx['README.md']++;
ok(b.build(DF.lines, ctx, ['README.md', '.git/']).steps.every((s) => s.hit), `${DF.id}: an ignored README edit missed the cache`);
ctx['.git/']++;
ok(!b.build(DF.lines, ctx, []).steps.every((s) => s.hit), `${DF.id}: a .git change without .dockerignore should bust COPY . .`);
ctx['package.json']++; ctx['package-lock.json']++;
ok(!b.build(DF.lines, ctx, []).steps.find((s) => s.instr.arg === 'npm ci')!.hit, `${DF.id}: a dependency change should rerun npm ci`);
ctx['package.json']--; ctx['package-lock.json']--;
ok(b.build(DF.lines, ctx, []).steps.every((s) => s.hit), `${DF.id}: undoing the change should hit the earlier layers`);
ok(b.build(DF.lines, ctx, [], true).steps.every((s) => !s.hit), `${DF.id}: --no-cache should miss everywhere`);
}
done();
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.