All quizzesHard
Advanced Testing — Series 2
Preview — 3 of 10 questions
What is the argument for form (2) despite its cost?
javascript
// (1) TypeOrmModule.forRoot({ type: 'sqlite', database: ':memory:', synchronize: true })
// (2) a PostgreSQL container started for the test runASQLite cannot be used with TypeORM at all
BContainers start faster than an in-memory database
CSQLite's dialect differs from PostgreSQL in ways that matter — JSONB, arrays, ILIKE, partial indexes, ON CONFLICT behaviour, transaction isolation, even type coercion — so tests can pass against SQLite while the same query fails or behaves differently in production; testing against the real engine removes that whole class of false confidence
DIn-memory databases lose data between tests, making assertions impossible
How should this be tested?
javascript
@Injectable()
export class ReportJob {
@Cron('0 3 * * *')
async run() { await this.reports.generateDaily(); }
}ATest run() directly as an ordinary method for its behaviour, and — if the schedule itself matters — assert on it separately through SchedulerRegistry, rather than trying to make the test wait for a cron tick
BUse jest.setTimeout(86_400_000) and let the schedule fire naturally
CTemporarily change the expression to * * * * * * in the test so it fires every second
DScheduled jobs cannot be tested and should be verified manually in staging
How is this tested without a seven-second test?
javascript
async fetchWithRetry(url: string) {
for (let i = 0; i < 3; i++) {
try { return await this.http.get(url); }
catch { await sleep(2 ** i * 1000); }
}
throw new ServiceUnavailableException();
}AMock fetchWithRetry itself and assert it was called
BReduce the real delays to one millisecond in a test-only configuration branch
CAccept the runtime; retry logic is inherently slow to verify
DInject the delay mechanism — a clock or sleep function — and substitute one that resolves immediately, or use jest.useFakeTimers() and advance them; the test then asserts the number of attempts and the backoff values requested, without any real elapsed time
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.