Function bindReplaySequelizeModels

  • Parameters

    Returns Sequelize

    Description

    Test-setup helper that binds Sequelize models to an OFFLINE Sequelize instance in replay mode — the constructor performs no I/O, so no live DB is required.

    Why: in replay the original connect() is replayed from the snapshot, so the model is never bound to a Sequelize instance and its isInitialized flag stays false — the original static build() would throw ModelNotInitializedError during replay reconstruction (see rebuildInstance).

    The binding runs with the global mode temporarily switched to 'off' and restored afterwards, because Model.init() internally invokes wrapped statics (e.g. getTableName) which must NOT route into a replay lookup during setup.

    In replay the created instance is additionally made hermetic: its connection manager is replaced with an offline stub (see makeConnectionManagerOffline), so any stray raw call through the real library code (e.g. Model.sequelize?.query('SELECT setval(...)')) never opens a live connection. This makes replay fully DB-less. When UNIMOCK_FALLBACK_ON_MISS=1 the connection manager is left live so the replay-miss fallback can execute against the real backend.

    No-op (returns undefined) outside replay mode.

    Example

    import { bindReplaySequelizeModels } from '@biorate/unimock';

    beforeAll(async () => {
    // offline-биндинг, без I/O; вернёт undefined вне replay
    bindReplaySequelizeModels(TestModel, OtherModel);
    });