How to unit test a script? #442
Unanswered
gaggle
asked this question in
Help needed
Replies: 2 comments 4 replies
-
Inspirationally, I suppose I'm hoping to write tests that could look like this: describe("example.mjs", () => {
it("calls `ls` with expected args", async () => {
const stub = jest.fn()
somehowRunScriptUnderTest("./example.mjs", { shell: stub })
expect(stub).toHaveBeenCalledWith("ls -lha")
})
it("formats the output", async () => {
const result = somehowRunScriptUnderTest("./example.mjs", {
shell: jest.fn(() => `
Permissions Size User Date Modified Name
drwxr-xr-x - user 16 Jun 21:59 .
drwxr-xr-x - user 7 Jun 17:30 ..
drwxr-xr-x - user 16 Jun 21:59 foo
drwxr-xr-x - user 27 May 14:53 bar
`),
})
expect(result.stdout).toEqual(`
Permissions Size User Date Modified Name
drwxr-xr-x - user 16 Jun 21:59 .
drwxr-xr-x - user 7 Jun 17:30 ..
drwxr-xr-x - user 16 Jun 21:59 foo
drwxr-xr-x - user 27 May 14:53 bar
`)
})
}) I'm no-doubt handwaving away important details in this pseudo-code, but just adding it here to explain my direction and intent with my question. I'm actually happy to pursue whatever approach zx supports and suggests. |
Beta Was this translation helpful? Give feedback.
4 replies
-
@antonmedv, can you provide an example of how to do this? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Let's say we have this super simple script that just calls
ls
:example.mjs
How may I test that this script actually calls
ls -lha
? How can I test the output is rendered correctly?In the real world I'm looking to test various calls and logic like
git ...
commands whose result are parsed and accumulated in various ways, but I think my question fundamentally boils down to how I can test anything at all. The bane of my existence has been effectively untestable Bash scripts, so I'm hoping to take a leap forward in maintainability with zx.Beta Was this translation helpful? Give feedback.
All reactions