|
| 1 | +import makeConsoleMock from 'consolemock' |
| 2 | + |
| 3 | +import lintStaged from '../lib/index' |
| 4 | +import { runAll } from '../lib/runAll' |
| 5 | +import { getInitialState } from '../lib/state' |
| 6 | +import { ApplyEmptyCommitError, ConfigNotFoundError, GitError } from '../lib/symbols' |
| 7 | + |
| 8 | +jest.mock('../lib/validateOptions.js', () => ({ |
| 9 | + validateOptions: jest.fn(async () => {}), |
| 10 | +})) |
| 11 | + |
| 12 | +jest.mock('../lib/runAll.js', () => ({ |
| 13 | + runAll: jest.fn(async () => {}), |
| 14 | +})) |
| 15 | + |
| 16 | +describe('lintStaged', () => { |
| 17 | + it('should log error when configuration not found', async () => { |
| 18 | + const ctx = getInitialState() |
| 19 | + ctx.errors.add(ConfigNotFoundError) |
| 20 | + runAll.mockImplementationOnce(async () => { |
| 21 | + throw { ctx } |
| 22 | + }) |
| 23 | + |
| 24 | + const logger = makeConsoleMock() |
| 25 | + |
| 26 | + await expect(lintStaged({}, logger)).resolves.toEqual(false) |
| 27 | + |
| 28 | + expect(logger.printHistory()).toMatchInlineSnapshot(` |
| 29 | + " |
| 30 | + ERROR ✖ No valid configuration found." |
| 31 | + `) |
| 32 | + }) |
| 33 | + |
| 34 | + it('should log error when preventing empty commit', async () => { |
| 35 | + const ctx = getInitialState() |
| 36 | + ctx.errors.add(ApplyEmptyCommitError) |
| 37 | + runAll.mockImplementationOnce(async () => { |
| 38 | + throw { ctx } |
| 39 | + }) |
| 40 | + |
| 41 | + const logger = makeConsoleMock() |
| 42 | + |
| 43 | + await expect(lintStaged({}, logger)).resolves.toEqual(false) |
| 44 | + |
| 45 | + expect(logger.printHistory()).toMatchInlineSnapshot(` |
| 46 | + " |
| 47 | + WARN |
| 48 | + ⚠ lint-staged prevented an empty git commit. |
| 49 | + Use the --allow-empty option to continue, or check your task configuration |
| 50 | + " |
| 51 | + `) |
| 52 | + }) |
| 53 | + |
| 54 | + it('should log error when preventing empty commit', async () => { |
| 55 | + const ctx = getInitialState() |
| 56 | + ctx.errors.add(ApplyEmptyCommitError) |
| 57 | + runAll.mockImplementationOnce(async () => { |
| 58 | + throw { ctx } |
| 59 | + }) |
| 60 | + |
| 61 | + const logger = makeConsoleMock() |
| 62 | + |
| 63 | + await expect(lintStaged({}, logger)).resolves.toEqual(false) |
| 64 | + |
| 65 | + expect(logger.printHistory()).toMatchInlineSnapshot(` |
| 66 | + " |
| 67 | + WARN |
| 68 | + ⚠ lint-staged prevented an empty git commit. |
| 69 | + Use the --allow-empty option to continue, or check your task configuration |
| 70 | + " |
| 71 | + `) |
| 72 | + }) |
| 73 | + |
| 74 | + it('should log error when a git operation failed', async () => { |
| 75 | + const ctx = getInitialState() |
| 76 | + ctx.shouldBackup = true |
| 77 | + ctx.errors.add(GitError) |
| 78 | + runAll.mockImplementationOnce(async () => { |
| 79 | + throw { ctx } |
| 80 | + }) |
| 81 | + |
| 82 | + const logger = makeConsoleMock() |
| 83 | + |
| 84 | + await expect(lintStaged({}, logger)).resolves.toEqual(false) |
| 85 | + |
| 86 | + expect(logger.printHistory()).toMatchInlineSnapshot(` |
| 87 | + " |
| 88 | + ERROR |
| 89 | + ✖ lint-staged failed due to a git error. |
| 90 | + ERROR Any lost modifications can be restored from a git stash: |
| 91 | +
|
| 92 | + > git stash list |
| 93 | + stash@{0}: automatic lint-staged backup |
| 94 | + > git stash apply --index stash@{0} |
| 95 | + " |
| 96 | + `) |
| 97 | + }) |
| 98 | + |
| 99 | + it('should throw when context is malformed', async () => { |
| 100 | + expect.assertions(2) |
| 101 | + |
| 102 | + const testError = Symbol() |
| 103 | + |
| 104 | + runAll.mockImplementationOnce(async () => { |
| 105 | + throw testError |
| 106 | + }) |
| 107 | + |
| 108 | + const logger = makeConsoleMock() |
| 109 | + |
| 110 | + await lintStaged({}, logger).catch((error) => { |
| 111 | + expect(error).toEqual(testError) |
| 112 | + }) |
| 113 | + |
| 114 | + expect(logger.printHistory()).toMatchInlineSnapshot(`""`) |
| 115 | + }) |
| 116 | + |
| 117 | + it.each` |
| 118 | + platform | maxArgLength |
| 119 | + ${'darwin'} | ${262144 / 2} |
| 120 | + ${'win32'} | ${8191 / 2} |
| 121 | + ${'others'} | ${131072 / 2} |
| 122 | + `( |
| 123 | + 'should use default max arg length of $maxArgLength on $platform', |
| 124 | + async ({ platform, maxArgLength }) => { |
| 125 | + const realPlatform = process.platform |
| 126 | + Object.defineProperty(process, 'platform', { |
| 127 | + value: platform, |
| 128 | + }) |
| 129 | + |
| 130 | + await lintStaged({}, makeConsoleMock()) |
| 131 | + |
| 132 | + expect(runAll).toHaveBeenLastCalledWith( |
| 133 | + expect.objectContaining({ maxArgLength }), |
| 134 | + expect.objectContaining({}) |
| 135 | + ) |
| 136 | + |
| 137 | + Object.defineProperty(process, 'platform', { |
| 138 | + value: realPlatform, |
| 139 | + }) |
| 140 | + } |
| 141 | + ) |
| 142 | +}) |
0 commit comments