/
runAll.unmocked.2.spec.js
138 lines (115 loc) Β· 3.91 KB
/
runAll.unmocked.2.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import makeConsoleMock from 'consolemock'
import fs from 'fs-extra'
import { nanoid } from 'nanoid'
import normalize from 'normalize-path'
import os from 'os'
import path from 'path'
jest.mock('../lib/file')
import execGitBase from '../lib/execGit'
import { readFile, writeFile } from '../lib/file'
import runAll from '../lib/runAll'
jest.unmock('execa')
jest.setTimeout(20000)
const testJsFilePretty = `module.exports = {
foo: "bar"
};
`
const isAppveyor = !!process.env.APPVEYOR
const osTmpDir = isAppveyor ? 'C:\\projects' : fs.realpathSync(os.tmpdir())
/**
* Create temporary directory and return its path
* @returns {Promise<String>}
*/
const createTempDir = async () => {
const dirname = path.resolve(osTmpDir, 'lint-staged-test', nanoid())
await fs.ensureDir(dirname)
return dirname
}
/**
* Remove temporary directory
* @param {String} dirname
* @returns {Promise<Void>}
*/
const removeTempDir = async (dirname) => {
await fs.remove(dirname)
}
let tmpDir
let cwd
// Append to file, creating if it doesn't exist
const appendFile = async (filename, content, dir = cwd) =>
fs.appendFile(path.join(dir, filename), content)
// Wrap execGit to always pass `gitOps`
const execGit = async (args) => execGitBase(args, { cwd })
// Execute runAll before git commit to emulate lint-staged
const gitCommit = async (options, args = ['-m test']) => {
await runAll({ quiet: true, ...options, cwd })
await execGit(['commit', ...args])
}
const globalConsoleTemp = console
describe('runAll', () => {
beforeAll(() => {
console = makeConsoleMock()
})
beforeEach(async () => {
tmpDir = await createTempDir()
cwd = normalize(tmpDir)
// Init repository with initial commit
await execGit('init')
await execGit(['config', 'user.name', '"test"'])
await execGit(['config', 'user.email', '"test@test.com"'])
await appendFile('README.md', '# Test\n')
await execGit(['add', 'README.md'])
await execGit(['commit', '-m initial commit'])
})
afterEach(async () => {
console.clearHistory()
if (!isAppveyor) {
await removeTempDir(tmpDir)
}
})
afterAll(() => {
console = globalConsoleTemp
})
it.only('Should throw when restoring untracked files fails', async () => {
readFile.mockImplementation(async () => Buffer.from('test'))
writeFile.mockImplementation(async () => Promise.reject('test'))
// Stage pretty file
await appendFile('test.js', testJsFilePretty)
await execGit(['add', 'test.js'])
// Create untracked file
await appendFile('test-untracked.js', testJsFilePretty)
// Run lint-staged with `prettier --list-different`
await expect(
gitCommit({ config: { '*.js': 'prettier --list-different' }, quiet: false })
).rejects.toThrowErrorMatchingInlineSnapshot(`"Something went wrong"`)
expect(console.printHistory()).toMatchInlineSnapshot(`
"
LOG Preparing... [started]
LOG Preparing... [completed]
LOG Running tasks... [started]
LOG Running tasks for *.js [started]
LOG prettier --list-different [started]
LOG prettier --list-different [failed]
LOG β
LOG Running tasks for *.js [failed]
LOG β
LOG Running tasks... [failed]
LOG Applying modifications... [started]
LOG Applying modifications... [skipped]
LOG β Skipped because of errors from tasks.
LOG Reverting to original state because of errors... [started]
LOG Reverting to original state because of errors... [failed]
LOG β Merge state could not be restored due to an error!
LOG Cleaning up... [started]
LOG Cleaning up... [skipped]
LOG β Skipped because of previous git error.
ERROR
Γ lint-staged failed due to a git error.
ERROR Any lost modifications can be restored from a git stash:
> git stash list
stash@{0}: On master: automatic lint-staged backup
> git stash apply --index stash@{0}
"
`)
})
})