Skip to content

Commit

Permalink
Reliable snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
sourishkrout committed Nov 14, 2023
1 parent db4ba4a commit 132d323
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
38 changes: 15 additions & 23 deletions tests/__snapshots__/module.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`createServer 1`] = `
{
"exitCode": 0,
"stderr": "",
"stdout": " ► Running task export...
► ✓ Task export exited with code 0
Running task print...
exported FOO=bar
► ✓ Task print exited with code 0
",
}
"{
\\"exitCode\\": 0,
\\"stdout\\": \\" ► Running task export...\\\\n ► ✓ Task export exited with code 0\\\\n ► Running task print...\\\\nexported FOO=bar\\\\r\\\\n ► ✓ Task print exited with code 0\\\\n\\",
\\"stderr\\": \\"\\"
}"
`;

exports[`run 1`] = `
{
"exitCode": 0,
"stderr": "",
"stdout": "Hello World
",
}
"{
\\"exitCode\\": 0,
\\"stdout\\": \\"Hello World\\\\r\\\\n\\",
\\"stderr\\": \\"\\"
}"
`;

exports[`run 2`] = `
{
"exitCode": 1,
"stderr": "",
"stdout": "exit
failed to run command \\"fail\\": exit code: 1
",
}
"{
\\"exitCode\\": 1,
\\"stdout\\": \\"failed to run command \\\\\\"fail\\\\\\": exit code: 1\\\\n\\",
\\"stderr\\": \\"\\"
}"
`;
11 changes: 8 additions & 3 deletions tests/module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { run, createServer } from '../src/index.js'

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))

// reliable snapshots x-platform and node versions
function toJsonSnapshot(obj: any) {
return JSON.stringify(obj, null, 1)
}

beforeAll(async () => {
await fs.rm(
path.resolve(__dirname, '..', '.bin'),
Expand All @@ -15,15 +20,15 @@ beforeAll(async () => {
})

test('run', async () => {
expect(await run(['helloWorld']))
expect(toJsonSnapshot(await run(['helloWorld'])))
.toMatchSnapshot()
expect(await run(['fail'], { ignoreReturnCode: true }))
expect(toJsonSnapshot(await run(['fail'], { ignoreReturnCode: true })))
.toMatchSnapshot()
})

test('createServer', async () => {
const server = await createServer()
expect(await run(['export', 'print'], { server }))
expect(toJsonSnapshot(await run(['export', 'print'], { server })))
.toMatchSnapshot()
server.kill()
})

0 comments on commit 132d323

Please sign in to comment.