Skip to content

Commit

Permalink
data migrate: Clean up upHandler test (#9796)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed Jan 3, 2024
1 parent 404c22f commit 6e1d0c4
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions packages/cli-packages/dataMigrate/src/__tests__/upHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ import { handler, NO_PENDING_MIGRATIONS_MESSAGE } from '../commands/upHandler'

const redwoodProjectPath = '/redwood-app'

let consoleLogMock: jest.SpyInstance
let consoleInfoMock: jest.SpyInstance
let consoleErrorMock: jest.SpyInstance
let consoleWarnMock: jest.SpyInstance

beforeEach(() => {
consoleLogMock = jest.spyOn(console, 'log').mockImplementation()
consoleInfoMock = jest.spyOn(console, 'info').mockImplementation()
consoleErrorMock = jest.spyOn(console, 'error').mockImplementation()
consoleWarnMock = jest.spyOn(console, 'warn').mockImplementation()
})

afterEach(() => {
consoleLogMock.mockRestore()
consoleInfoMock.mockRestore()
consoleErrorMock.mockRestore()
consoleWarnMock.mockRestore()
})

jest.mock('fs', () => require('memfs').fs)

const mockDataMigrations: { current: any[] } = { current: [] }
Expand Down Expand Up @@ -146,8 +165,6 @@ const ranDataMigration = {

describe('upHandler', () => {
it("noops if there's no data migrations directory", async () => {
console.info = jest.fn()

vol.fromNestedJSON(
{
'redwood.toml': '',
Expand All @@ -174,7 +191,9 @@ describe('upHandler', () => {
distPath: getPaths().api.dist,
})

expect(console.info.mock.calls[0][0]).toMatch(NO_PENDING_MIGRATIONS_MESSAGE)
expect(consoleInfoMock.mock.calls[0][0]).toMatch(
NO_PENDING_MIGRATIONS_MESSAGE
)
})

it("noops if there's no pending migrations", async () => {
Expand All @@ -199,21 +218,17 @@ describe('upHandler', () => {
redwoodProjectPath
)

console.info = jest.fn()

await handler({
importDbClientFromDist: true,
distPath: getPaths().api.dist,
})

expect(console.info.mock.calls[0][0]).toMatch(NO_PENDING_MIGRATIONS_MESSAGE)
expect(consoleInfoMock.mock.calls[0][0]).toMatch(
NO_PENDING_MIGRATIONS_MESSAGE
)
})

it('runs pending migrations', async () => {
console.info = jest.fn()
console.error = jest.fn()
console.warn = jest.fn()

mockDataMigrations.current = [
{
version: '20230822075441',
Expand Down Expand Up @@ -253,13 +268,13 @@ describe('upHandler', () => {
// or test suite itself will fail.
process.exitCode = 0

expect(console.info.mock.calls[0][0]).toMatch(
expect(consoleInfoMock.mock.calls[0][0]).toMatch(
'1 data migration(s) completed successfully.'
)
expect(console.error.mock.calls[1][0]).toMatch(
expect(consoleErrorMock.mock.calls[1][0]).toMatch(
'1 data migration(s) exited with errors.'
)
expect(console.warn.mock.calls[0][0]).toMatch(
expect(consoleWarnMock.mock.calls[0][0]).toMatch(
'1 data migration(s) skipped due to previous error'
)
})
Expand Down

0 comments on commit 6e1d0c4

Please sign in to comment.