Skip to content
🚀 This documentation is for unreal-orm 1.0.0 alpha which requires SurrealDB 2.0 SDK. For use with version 1.x, see here.

Testing

SurrealDB can run fully in-memory, which makes UnrealORM models easy to test without Docker.

import { Surreal } from 'surrealdb';
import { createNodeEngines } from '@surrealdb/node';
import { applySchema } from 'unreal-orm';
import { User, Post } from './tables';
async function setupTestDb() {
const db = new Surreal({ engines: { ...createNodeEngines() } });
await db.connect('mem://', {
namespace: 'test',
database: 'test',
});
await applySchema(db, [User, Post]);
return db;
}
import { test, expect, beforeAll } from 'vitest';
import { RecordId } from 'surrealdb';
import { User } from './tables';
let db: Surreal;
beforeAll(async () => {
db = await setupTestDb();
});
test('creates a user', async () => {
const user = await User.create(db, { name: 'Alice', email: 'alice@example.com' });
expect(user.name).toBe('Alice');
expect(user.id).toBeInstanceOf(RecordId);
});
test('enforces email assertion', async () => {
await expect(
User.create(db, { name: 'Bad', email: 'not-an-email' })
).rejects.toThrow();
});

For parallel test runners, use a unique namespace per worker:

await db.connect('mem://', {
namespace: `test-${process.pid}`,
database: 'test',
});

Or create a new Surreal instance per test and reconnect each time.

You can snapshot the schema DDL to catch accidental changes:

import { Unreal } from 'unreal-orm';
const ddl = Unreal.generateFullSchemaQl([User, Post]);
expect(ddl).toMatchSnapshot();