1
0
Fork 0
mirror of synced 2024-05-22 21:33:45 +12:00
budibase/packages/backend-core/src/migrations/tests/index.spec.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

require("../../../tests")
2022-01-27 23:40:31 +13:00
const { runMigrations, getMigrationsDoc } = require("../index")
const { dangerousGetDB } = require("../../db")
2021-10-27 03:47:36 +13:00
const {
StaticDatabases,
} = require("../../db/utils")
let db
describe("migrations", () => {
const migrationFunction = jest.fn()
2022-01-27 23:40:31 +13:00
const MIGRATIONS = [{
type: "global",
name: "test",
fn: migrationFunction
}]
2021-10-27 03:47:36 +13:00
beforeEach(() => {
db = dangerousGetDB(StaticDatabases.GLOBAL.name)
2021-10-27 03:47:36 +13:00
})
afterEach(async () => {
jest.clearAllMocks()
await db.destroy()
})
2022-01-27 23:40:31 +13:00
const migrate = () => {
return runMigrations(MIGRATIONS)
2021-10-27 03:47:36 +13:00
}
it("should run a new migration", async () => {
2022-01-27 23:40:31 +13:00
await migrate()
2021-10-27 03:47:36 +13:00
expect(migrationFunction).toHaveBeenCalled()
2022-01-27 23:40:31 +13:00
const doc = await getMigrationsDoc(db)
expect(doc.test).toBeDefined()
2021-10-27 03:47:36 +13:00
})
it("should match snapshot", async () => {
2022-01-27 23:40:31 +13:00
await migrate()
2021-10-27 03:47:36 +13:00
const doc = await getMigrationsDoc(db)
expect(doc).toMatchSnapshot()
})
it("should skip a previously run migration", async () => {
2022-01-27 23:40:31 +13:00
await migrate()
const previousMigrationTime = await getMigrationsDoc(db).test
await migrate()
const currentMigrationTime = await getMigrationsDoc(db).test
2021-10-27 03:47:36 +13:00
expect(migrationFunction).toHaveBeenCalledTimes(1)
2022-01-27 23:40:31 +13:00
expect(currentMigrationTime).toBe(previousMigrationTime)
2021-10-27 03:47:36 +13:00
})
})