Skip to content

Commit

Permalink
test: use more strict matchers to avoid snapshotting EventEmitter pro…
Browse files Browse the repository at this point in the history
…perties
  • Loading branch information
iiroj committed Dec 2, 2023
1 parent c18b7c5 commit 934ce5c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 260 deletions.
96 changes: 21 additions & 75 deletions test/integration/gitWorkFlow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { jest as jestGlobals } from '@jest/globals'
import { writeFile } from '../../lib/file.js'
import { GitWorkflow } from '../../lib/gitWorkflow.js'
import { getInitialState } from '../../lib/state.js'
import {
GetBackupStashError,
GitError,
HideUnstagedChangesError,
RestoreMergeStatusError,
} from '../../lib/symbols.js'

import { normalizeWindowsNewlines } from './__utils__/normalizeWindowsNewlines.js'
import { withGitIntegration } from './__utils__/withGitIntegration.js'
Expand Down Expand Up @@ -38,24 +44,9 @@ describe('gitWorkflow', () => {
await expect(gitWorkflow.prepare(ctx, false)).rejects.toThrowErrorMatchingInlineSnapshot(
`"test"`
)
expect(ctx).toMatchInlineSnapshot(`
{
"errors": Set {
Symbol(GitError),
},
"events": EventEmitter {
"_events": {},
"_eventsCount": 0,
"_maxListeners": undefined,
Symbol(shapeMode): false,
Symbol(kCapture): false,
},
"hasPartiallyStagedFiles": true,
"output": [],
"quiet": false,
"shouldBackup": null,
}
`)

expect(ctx.errors).toBeInstanceOf(Set)
expect(ctx.errors.has(GitError)).toBe(true)
})
)
})
Expand All @@ -70,25 +61,10 @@ describe('gitWorkflow', () => {
await expect(gitWorkflow.cleanup(ctx)).rejects.toThrowErrorMatchingInlineSnapshot(
`"lint-staged automatic backup is missing!"`
)
expect(ctx).toMatchInlineSnapshot(`
{
"errors": Set {
Symbol(GetBackupStashError),
Symbol(GitError),
},
"events": EventEmitter {
"_events": {},
"_eventsCount": 0,
"_maxListeners": undefined,
Symbol(shapeMode): false,
Symbol(kCapture): false,
},
"hasPartiallyStagedFiles": null,
"output": [],
"quiet": false,
"shouldBackup": null,
}
`)

expect(ctx.errors).toBeInstanceOf(Set)
expect(ctx.errors.has(GetBackupStashError)).toBe(true)
expect(ctx.errors.has(GitError)).toBe(true)
})
)
})
Expand Down Expand Up @@ -145,25 +121,10 @@ describe('gitWorkflow', () => {
await expect(gitWorkflow.hideUnstagedChanges(ctx)).rejects.toThrowError(
`pathspec '${totallyRandom}' did not match any file(s) known to git`
)
expect(ctx).toMatchInlineSnapshot(`
{
"errors": Set {
Symbol(GitError),
Symbol(HideUnstagedChangesError),
},
"events": EventEmitter {
"_events": {},
"_eventsCount": 0,
"_maxListeners": undefined,
Symbol(shapeMode): false,
Symbol(kCapture): false,
},
"hasPartiallyStagedFiles": null,
"output": [],
"quiet": false,
"shouldBackup": null,
}
`)

expect(ctx.errors).toBeInstanceOf(Set)
expect(ctx.errors.has(HideUnstagedChangesError)).toBe(true)
expect(ctx.errors.has(GitError)).toBe(true)
})
)

Expand Down Expand Up @@ -200,25 +161,10 @@ describe('gitWorkflow', () => {
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Merge state could not be restored due to an error!"`
)
expect(ctx).toMatchInlineSnapshot(`
{
"errors": Set {
Symbol(GitError),
Symbol(RestoreMergeStatusError),
},
"events": EventEmitter {
"_events": {},
"_eventsCount": 0,
"_maxListeners": undefined,
Symbol(shapeMode): false,
Symbol(kCapture): false,
},
"hasPartiallyStagedFiles": null,
"output": [],
"quiet": false,
"shouldBackup": null,
}
`)

expect(ctx.errors).toBeInstanceOf(Set)
expect(ctx.errors.has(GitError)).toBe(true)
expect(ctx.errors.has(RestoreMergeStatusError)).toBe(true)
})
)
})
Expand Down
72 changes: 10 additions & 62 deletions test/unit/index2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,10 @@ describe('lintStaged', () => {

await lintStaged({ configPath: MOCK_CONFIG_FILE, quiet: true }, makeConsoleMock())

expect(Listr.mock.calls[0][1]).toMatchInlineSnapshot(`
{
"ctx": {
"errors": Set {},
"events": EventEmitter {
"_events": {},
"_eventsCount": 0,
"_maxListeners": undefined,
Symbol(shapeMode): false,
Symbol(kCapture): false,
},
"hasPartiallyStagedFiles": null,
"output": [],
"quiet": true,
"shouldBackup": true,
},
"exitOnError": false,
"fallbackRenderer": "silent",
"registerSignalListeners": false,
"renderer": "silent",
}
`)
expect(Listr.mock.calls[0][1]).toMatchObject({
fallbackRenderer: 'silent',
renderer: 'silent',
})
})

it('should pass debug flag to Listr', async () => {
Expand All @@ -79,45 +61,10 @@ describe('lintStaged', () => {
makeConsoleMock()
)

expect(Listr.mock.calls[0][1]).toMatchInlineSnapshot(`
{
"ctx": {
"errors": Set {},
"events": EventEmitter {
"_events": {},
"_eventsCount": 0,
"_maxListeners": undefined,
Symbol(shapeMode): false,
Symbol(kCapture): false,
},
"hasPartiallyStagedFiles": null,
"output": [],
"quiet": false,
"shouldBackup": true,
},
"exitOnError": false,
"fallbackRenderer": "test",
"registerSignalListeners": false,
"renderer": "test",
"rendererOptions": {
"logger": ListrLogger {
"applyFormat": [MockFunction],
"fields": [MockFunction],
"format": [MockFunction],
"icon": [MockFunction],
"log": [MockFunction],
"prefix": [MockFunction],
"spacing": [MockFunction],
"splat": [MockFunction],
"style": [MockFunction],
"suffix": [MockFunction],
"toStderr": [MockFunction],
"toStdout": [MockFunction],
"wrap": [MockFunction],
},
},
}
`)
expect(Listr.mock.calls[0][1]).toMatchObject({
fallbackRenderer: 'test',
renderer: 'test',
})
})

it('should catch errors from js function config', async () => {
Expand All @@ -129,10 +76,11 @@ describe('lintStaged', () => {
}

expect.assertions(2)

await expect(lintStaged({ config }, logger)).rejects.toThrowErrorMatchingInlineSnapshot(
`"failed config"`
)

expect(logger.printHistory()).toMatchInlineSnapshot(`""`)
expect(logger.printHistory()).toEqual('')
})
})
84 changes: 14 additions & 70 deletions test/unit/resolveTaskFn.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,22 +241,8 @@ describe('resolveTaskFn', () => {
const taskFn = resolveTaskFn({ ...defaultOpts, command: 'mock cmd', verbose: true })
const context = getInitialState()
await expect(taskFn(context)).resolves.toMatchInlineSnapshot(`undefined`)
expect(context).toMatchInlineSnapshot(`
{
"errors": Set {},
"events": EventEmitter {
"_events": {},
"_eventsCount": 0,
"_maxListeners": undefined,
Symbol(shapeMode): false,
Symbol(kCapture): false,
},
"hasPartiallyStagedFiles": null,
"output": [],
"quiet": false,
"shouldBackup": null,
}
`)

expect(context.output).toEqual([])
})

it('should add output even when task succeeds if `verbose: true`', async () => {
Expand All @@ -274,25 +260,13 @@ describe('resolveTaskFn', () => {
const taskFn = resolveTaskFn({ ...defaultOpts, command: 'mock cmd', verbose: true })
const context = getInitialState()
await expect(taskFn(context)).resolves.toMatchInlineSnapshot(`undefined`)
expect(context).toMatchInlineSnapshot(`
{
"errors": Set {},
"events": EventEmitter {
"_events": {},
"_eventsCount": 0,
"_maxListeners": undefined,
Symbol(shapeMode): false,
Symbol(kCapture): false,
},
"hasPartiallyStagedFiles": null,
"output": [
"

expect(context.output).toMatchInlineSnapshot(`
[
"
→ mock cmd:
Mock success",
],
"quiet": false,
"shouldBackup": null,
}
]
`)
})

Expand All @@ -311,25 +285,11 @@ describe('resolveTaskFn', () => {
const taskFn = resolveTaskFn({ ...defaultOpts, command: 'mock cmd' })
const context = getInitialState({ quiet: true })
await expect(taskFn(context)).rejects.toThrowErrorMatchingInlineSnapshot(`"mock cmd [1]"`)
expect(context).toMatchInlineSnapshot(`
{
"errors": Set {
Symbol(TaskError),
},
"events": EventEmitter {
"_events": {},
"_eventsCount": 0,
"_maxListeners": undefined,
Symbol(shapeMode): false,
Symbol(kCapture): false,
},
"hasPartiallyStagedFiles": null,
"output": [
"stderr",
],
"quiet": true,
"shouldBackup": null,
}

expect(context.output).toMatchInlineSnapshot(`
[
"stderr",
]
`)
})

Expand All @@ -348,24 +308,8 @@ describe('resolveTaskFn', () => {
const taskFn = resolveTaskFn({ ...defaultOpts, command: 'mock cmd' })
const context = getInitialState({ quiet: true })
await expect(taskFn(context)).rejects.toThrowErrorMatchingInlineSnapshot(`"mock cmd [1]"`)
expect(context).toMatchInlineSnapshot(`
{
"errors": Set {
Symbol(TaskError),
},
"events": EventEmitter {
"_events": {},
"_eventsCount": 0,
"_maxListeners": undefined,
Symbol(shapeMode): false,
Symbol(kCapture): false,
},
"hasPartiallyStagedFiles": null,
"output": [],
"quiet": true,
"shouldBackup": null,
}
`)

expect(context.output).toEqual([])
})

it('should not kill long running tasks without errors in context', async () => {
Expand Down

0 comments on commit 934ce5c

Please sign in to comment.