generated from JoshuaKGoldberg/create-typescript-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle errors in runPreset (#120)
- Loading branch information
1 parent
224268c
commit 9934655
Showing
8 changed files
with
154 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import chalk from "chalk"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { createClackDisplay } from "./createClackDisplay.js"; | ||
import { runSpinnerTask } from "./runSpinnerTask.js"; | ||
|
||
const mockLog = { | ||
error: vi.fn(), | ||
}; | ||
const mockSpinner = { | ||
start: vi.fn(), | ||
stop: vi.fn(), | ||
}; | ||
|
||
vi.mock("@clack/prompts", () => ({ | ||
get log() { | ||
return mockLog; | ||
}, | ||
spinner: () => mockSpinner, | ||
})); | ||
|
||
describe("runSpinnerTask", () => { | ||
it("displays the error when the task throws an error", async () => { | ||
const error = new Error("Oh no!"); | ||
|
||
const actual = await runSpinnerTask( | ||
createClackDisplay(), | ||
"Running task", | ||
"Ran task", | ||
vi.fn().mockRejectedValueOnce(error), | ||
); | ||
|
||
expect(actual).toBe(error); | ||
expect(mockLog.error).toHaveBeenCalledWith(chalk.red(error.stack)); | ||
expect(mockSpinner.stop.mock.calls).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
"Error running task:", | ||
1, | ||
], | ||
] | ||
`); | ||
}); | ||
|
||
it("displays a general log when the task resolves with a value", async () => { | ||
const expected = { ok: true }; | ||
|
||
const actual = await runSpinnerTask( | ||
createClackDisplay(), | ||
"Running task", | ||
"Ran task", | ||
vi.fn().mockResolvedValueOnce(expected), | ||
); | ||
|
||
expect(actual).toBe(expected); | ||
expect(mockLog.error).not.toHaveBeenCalled(); | ||
expect(mockSpinner.stop.mock.calls).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
"Ran task", | ||
], | ||
] | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters