Skip to content

Commit d808bde

Browse files
committed
Add tests that expected actions are called
1 parent 955b135 commit d808bde

1 file changed

Lines changed: 69 additions & 1 deletion

File tree

__tests__/current.test.ts

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
jest.mock("child_process");
12
import "./helpers"; // Has side-effects; must come first
23

4+
import { exec } from "child_process";
35
import mockFs from "mock-fs";
46

57
import { current } from "../src";
68
import { withClient } from "../src/pg";
7-
import { ParsedSettings, parseSettings } from "../src/settings";
9+
import { ParsedSettings, parseSettings, Settings } from "../src/settings";
810
import { makeMigrations, resetDb, settings } from "./helpers";
911

1012
beforeEach(resetDb);
@@ -134,3 +136,69 @@ it("runs migrations", async () => {
134136
expect(newTables).toEqual(tables);
135137
expect(newEnums).toEqual(enums);
136138
});
139+
140+
it("runs actions when forceActions is set", async () => {
141+
const ACTIONS = {
142+
initial: {
143+
forceActions: false,
144+
currentSql: "",
145+
expectedActions: [
146+
"beforeAllMigrations",
147+
"afterAllMigrations",
148+
"beforeCurrent",
149+
"afterCurrent",
150+
],
151+
},
152+
currentChange: {
153+
forceActions: false,
154+
currentSql: MIGRATION_NOTRX_TEXT,
155+
expectedActions: ["beforeCurrent", "afterCurrent"],
156+
},
157+
noop: {
158+
forceActions: false,
159+
currentSql: MIGRATION_NOTRX_TEXT,
160+
expectedActions: [],
161+
},
162+
forceActions: {
163+
forceActions: true,
164+
currentSql: MIGRATION_NOTRX_TEXT,
165+
expectedActions: [
166+
"beforeAllMigrations",
167+
"afterAllMigrations",
168+
"beforeCurrent",
169+
"afterCurrent",
170+
],
171+
},
172+
} as const;
173+
const settingsWithHooks: Settings = {
174+
...settings,
175+
beforeAllMigrations: [
176+
{ _: "command", command: "echo did_beforeAllMigrations" },
177+
],
178+
afterAllMigrations: [
179+
{ _: "command", command: "echo did_afterAllMigrations" },
180+
],
181+
beforeCurrent: [{ _: "command", command: "echo did_beforeCurrent" }],
182+
afterCurrent: [{ _: "command", command: "echo did_afterCurrent" }],
183+
};
184+
for (const mode of Object.keys(ACTIONS) as Array<keyof typeof ACTIONS>) {
185+
const { forceActions, currentSql, expectedActions } = ACTIONS[mode];
186+
187+
mockFs({
188+
[`migrations/committed/000001.sql`]: MIGRATION_1_COMMITTED,
189+
[`migrations/committed/000002.sql`]: MIGRATION_ENUM_COMMITTED, // Creates enum with 1 value
190+
"migrations/current.sql": currentSql,
191+
});
192+
193+
const mockedExec: jest.Mock<typeof exec> = exec as any;
194+
mockedExec.mockClear();
195+
mockedExec.mockImplementation((_cmd, _options, callback) =>
196+
callback(null, { stdout: "", stderr: "" }),
197+
);
198+
await current(settingsWithHooks, { forceActions });
199+
const calledActions = mockedExec.mock.calls.map((c) =>
200+
c[0].substring("echo did_".length),
201+
);
202+
expect(calledActions).toEqual(expectedActions);
203+
}
204+
});

0 commit comments

Comments
 (0)