forked from vuejs/vue-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheslintPlugin.spec.js
227 lines (199 loc) · 5.93 KB
/
eslintPlugin.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
jest.setTimeout(35000)
const path = require('path')
const { linkBin } = require('@vue/cli/lib/util/linkBin')
const create = require('@vue/cli-test-utils/createTestProject')
const runSilently = fn => {
const log = console.log
console.log = () => {}
const res = fn()
console.log = log
return res
}
test('should work', async () => {
const project = await create('eslint', {
plugins: {
'@vue/cli-plugin-babel': {},
'@vue/cli-plugin-eslint': {
config: 'airbnb',
lintOn: 'commit'
}
}
}, null, true /* initGit */)
const { read, write, run } = project
// should've applied airbnb autofix
const main = await read('src/main.js')
expect(main).toMatch(';')
// remove semicolons
const updatedMain = main.replace(/;/g, '')
await write('src/main.js', updatedMain)
// lint
await run('vue-cli-service lint')
expect(await read('src/main.js')).toMatch(';')
// lint-on-commit
await runSilently(() => {
require('yorkie/src/install')(path.join(project.dir, 'node_modules'))
// since yorkie isn't actually installed in the test project, we need to
// symlink it
return linkBin(
path.resolve(require.resolve('yorkie/src/install'), '../../'),
path.join(project.dir, 'node_modules', 'yorkie')
)
})
const hook = await read('.git/hooks/pre-commit')
expect(hook).toMatch('#yorkie')
await write('src/main.js', updatedMain)
// nvm doesn't like PREFIX env
if (process.platform === 'darwin') {
delete process.env.PREFIX
}
await run('git add -A')
await run('git commit -m save')
// should be linted on commit
expect(await read('src/main.js')).toMatch(';')
// lint-on-save needs to be tested in a callback
let done
const donePromise = new Promise(resolve => {
done = resolve
})
// enable lintOnSave
await write('vue.config.js', "module.exports = { lintOnSave: 'default' }")
// write invalid file
const app = await read('src/App.vue')
const updatedApp = app.replace(/;/, '')
await write('src/App.vue', updatedApp)
const server = run('vue-cli-service serve')
let isFirstMsg = true
server.stdout.on('data', data => {
data = data.toString()
if (isFirstMsg) {
// should fail on start
expect(data).toMatch(/Failed to compile with \d error/)
isFirstMsg = false
// fix it
write('src/App.vue', app)
} else if (data.match(/Compiled successfully/)) {
// should compile on the subsequent update
// (note: in CI environment this may not be the exact 2nd update,
// so we use data.match as a termination condition rather than a test case)
server.stdin.write('close')
done()
}
})
await donePromise
})
test('should not fix with --no-fix option', async () => {
const project = await create('eslint-nofix', {
plugins: {
'@vue/cli-plugin-babel': {},
'@vue/cli-plugin-eslint': {
config: 'airbnb',
lintOn: 'commit'
}
}
})
const { read, write, run } = project
// should've applied airbnb autofix
const main = await read('src/main.js')
expect(main).toMatch(';')
// remove semicolons
const updatedMain = main.replace(/;/g, '')
await write('src/main.js', updatedMain)
// lint with no fix should fail
try {
await run('vue-cli-service lint --no-fix')
} catch (e) {
expect(e.code).toBe(1)
expect(e.failed).toBeTruthy()
}
// files should not have been fixed
expect(await read('src/main.js')).not.toMatch(';')
})
// #3167, #3243
test('should not throw when src folder is ignored by .eslintignore', async () => {
const project = await create('eslint-ignore', {
plugins: {
'@vue/cli-plugin-babel': {},
'@vue/cli-plugin-eslint': {
config: 'airbnb',
lintOn: 'commit'
}
},
useConfigFiles: true
})
const { write, run } = project
await write('.eslintignore', 'src\n.eslintrc.js')
// should not throw
await run('vue-cli-service lint')
})
test('should save report results to file with --output-file option', async () => {
const project = await create('eslint-output-file', {
plugins: {
'@vue/cli-plugin-babel': {},
'@vue/cli-plugin-eslint': {
config: 'airbnb',
lintOn: 'commit'
}
}
})
const { read, write, run } = project
// should've applied airbnb autofix
const main = await read('src/main.js')
expect(main).toMatch(';')
// remove semicolons
const updatedMain = main.replace(/;/g, '')
await write('src/main.js', updatedMain)
// result file name
const resultsFile = 'lint_results.json'
try {
// lint in JSON format to output-file
await run(`vue-cli-service lint --format json --output-file ${resultsFile} --no-fix`)
} catch (e) {
// lint with no fix should fail
expect(e.code).toBe(1)
expect(e.failed).toBeTruthy()
}
let resultsFileContents = ''
// results file should exist
try {
resultsFileContents = await read(resultsFile)
} catch (e) {
expect(e.code).toBe(0)
expect(e.failed).toBeFalsy()
}
// results file should not be empty
expect(resultsFileContents.length).toBeGreaterThan(0)
// results file is valid JSON
try {
JSON.parse(resultsFileContents)
} catch (e) {
expect(e.code).toBe(0)
expect(e.failed).toBeFalsy()
}
// results file should show "Missing semicolon" errors
expect(resultsFileContents).toEqual(expect.stringContaining('Missing semicolon'))
})
test('should persist cache', async () => {
const project = await create('eslint-cache', {
plugins: {
'@vue/cli-plugin-eslint': {
config: 'airbnb',
lintOn: 'save'
}
}
})
let done
const donePromise = new Promise(resolve => {
done = resolve
})
const { has, run } = project
const server = run('vue-cli-service serve')
server.stdout.on('data', data => {
data = data.toString()
if (data.match(/Compiled successfully/)) {
server.stdin.write('close')
done()
}
})
await donePromise
expect(has('node_modules/.cache/eslint/cache.json')).toBe(true)
})