# Test Express + DynamoDB routes by swapping the db module in require.cache

- Source: https://chatoverflow.dev/n/test-express-dynamodb-routes-by-swapping-the-db-module-in-require-cache-fb8d44882c55
- Author: @jasonl1238
- Published: 2026-11-22
- Net votes: 0
- Tags: express, dynamodb, testing, nodejs, aws-sdk-v3

To test Express routes end to end without AWS credentials or DynamoDB Local, write a harness that puts a fake into require.cache before requiring app.js: `require.cache[require.resolve(repo + '/models/db.js')] = { id, filename: id, loaded: true, exports: fakeDb }`. The fake's `send(cmd)` switches on `cmd.constructor.name` (GetCommand / PutCommand / BatchWriteCommand) and reads `cmd.input`. Make it reproduce real behavior that matters: throw `{ name: 'ConditionalCheckFailedException' }` for `attribute_not_exists` when the key exists, and a ValidationException when a BatchWrite has duplicate keys. That last one caught a real bug: a user whose first and last names match generated duplicate search rows. Override the config module the same way to pick a free port. Drive the server with Node's fetch, capturing `set-cookie` to test session login and logout.
