|
| 1 | +import '../integration/__mocks__/resolveConfig.js' |
| 2 | + |
| 3 | +import { jest } from '@jest/globals' |
| 4 | + |
| 5 | +import { withGitIntegration } from '../integration/__utils__/withGitIntegration.js' |
| 6 | +import * as fileFixtures from '../integration/__fixtures__/files.js' |
| 7 | +import * as configFixtures from '../integration/__fixtures__/configs.js' |
| 8 | + |
| 9 | +import { getLintStagedExecutor } from './__utils__/getLintStagedExecutor.js' |
| 10 | + |
| 11 | +jest.setTimeout(20000) |
| 12 | +jest.retryTimes(2) |
| 13 | + |
| 14 | +describe('lint-staged', () => { |
| 15 | + test( |
| 16 | + 'reads config from stdin', |
| 17 | + withGitIntegration(async ({ cwd, execGit, readFile, writeFile }) => { |
| 18 | + const lintStaged = getLintStagedExecutor(cwd) |
| 19 | + |
| 20 | + // Stage ugly file |
| 21 | + await writeFile('test file.js', fileFixtures.uglyJS) |
| 22 | + await execGit(['add', 'test file.js']) |
| 23 | + |
| 24 | + // Run lint-staged with config from stdin |
| 25 | + await lintStaged('-c -', { |
| 26 | + input: JSON.stringify(configFixtures.prettierWrite), |
| 27 | + }) |
| 28 | + |
| 29 | + // Nothing was wrong so file was prettified |
| 30 | + expect(await readFile('test file.js')).toEqual(fileFixtures.prettyJS) |
| 31 | + }) |
| 32 | + ) |
| 33 | + |
| 34 | + test( |
| 35 | + 'fails when stdin config is not valid', |
| 36 | + withGitIntegration(async ({ cwd, execGit, readFile, writeFile }) => { |
| 37 | + const lintStaged = getLintStagedExecutor(cwd) |
| 38 | + |
| 39 | + // Stage ugly file |
| 40 | + await writeFile('test file.js', fileFixtures.uglyJS) |
| 41 | + await execGit(['add', 'test file.js']) |
| 42 | + |
| 43 | + // Break JSON by removing } from the end |
| 44 | + const brokenJSONConfig = JSON.stringify(configFixtures.prettierWrite).replace('"}', '"') |
| 45 | + |
| 46 | + // Run lint-staged with broken config from stdin |
| 47 | + await expect(lintStaged('-c -', { input: brokenJSONConfig })).rejects.toThrowError( |
| 48 | + 'Failed to read config from stdin' |
| 49 | + ) |
| 50 | + |
| 51 | + // File was not edited |
| 52 | + expect(await readFile('test file.js')).toEqual(fileFixtures.uglyJS) |
| 53 | + }) |
| 54 | + ) |
| 55 | +}) |
0 commit comments