-
Notifications
You must be signed in to change notification settings - Fork 59
/
runESLint.test.js
151 lines (132 loc) · 3.36 KB
/
runESLint.test.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
/* eslint-disable class-methods-use-this, global-require */
const path = require('path');
const runESLintRunnerWithMockedEngine = options => {
jest.resetModules();
jest.doMock('eslint', () => ({
CLIEngine: class {
isPathIgnored(file) {
return options.cliEngine.ignoredFiles.includes(file);
}
executeOnFiles() {
return {
results:
options.cliEngine.errorCount > 0
? [{ errorCount: options.cliEngine.errorCount, warningCount: 0 }]
: [],
errorCount: options.cliEngine.errorCount,
warningCount: 0,
};
}
getFormatter() {
return () => {};
}
},
}));
const runESLint = require('../runESLint');
return runESLint({ extraOptions: {}, ...options.runESLint });
};
it('Requires the config setupTestFrameworkScriptFile when specified', async () => {
const setupFile = path.join(__dirname, './path/to/setupFile.js');
let setupFileWasLoaded = false;
jest.doMock(
setupFile,
() => {
setupFileWasLoaded = true;
},
{ virtual: true },
);
await runESLintRunnerWithMockedEngine({
cliEngine: {
ignoredFiles: ['/path/to/file.test.js'],
errorCount: 0,
},
runESLint: {
testPath: 'path/to/file.test.js',
config: {
setupTestFrameworkScriptFile: setupFile,
},
},
});
expect(setupFileWasLoaded).toBeTruthy();
});
it('Requires the config setupFilesAfterEnv when specified', async () => {
const setupFiles = [
path.join(__dirname, './path/to/setupFileFoo.js'),
path.join(__dirname, './path/to/setupFileBar.js'),
];
const setupFilesWereLoaded = setupFiles.map(() => false);
setupFiles.forEach((setupFile, index) => {
jest.doMock(
setupFile,
() => {
setupFilesWereLoaded[index] = true;
},
{ virtual: true },
);
});
await runESLintRunnerWithMockedEngine({
cliEngine: {
ignoredFiles: ['/path/to/file.test.js'],
errorCount: 0,
},
runESLint: {
testPath: 'path/to/file.test.js',
config: {
setupFilesAfterEnv: setupFiles,
},
},
});
expect(setupFilesWereLoaded).toEqual([true, true]);
});
it('Returns "skipped" when the test path is ignored', async () => {
const result = await runESLintRunnerWithMockedEngine({
cliEngine: {
ignoredFiles: ['/path/to/file.test.js'],
errorCount: 0,
},
runESLint: {
testPath: '/path/to/file.test.js',
config: {},
},
});
expect(result).toMatchObject({
numFailingTests: 0,
numPassingTests: 0,
numPendingTests: 1,
skipped: true,
});
});
it('Returns "passed" when the test passed', async () => {
const result = await runESLintRunnerWithMockedEngine({
cliEngine: {
ignoredFiles: [],
errorCount: 0,
},
runESLint: {
testPath: '/path/to/file.test.js',
config: {},
},
});
expect(result).toMatchObject({
numFailingTests: 0,
numPassingTests: 1,
numPendingTests: 0,
});
});
it('Returns "fail" when the test failed', async () => {
const result = await runESLintRunnerWithMockedEngine({
cliEngine: {
ignoredFiles: [],
errorCount: 1,
},
runESLint: {
testPath: '/path/to/file.test.js',
config: {},
},
});
expect(result).toMatchObject({
numFailingTests: 1,
numPassingTests: 0,
numPendingTests: 0,
});
});