/
index.spec.ts
210 lines (199 loc) · 5.4 KB
/
index.spec.ts
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
import { TSESLint } from '@typescript-eslint/experimental-utils';
import * as parser from '@typescript-eslint/parser';
import { readFileSync } from 'fs';
import rule, { Options } from '../src/rules/config';
const ruleTester = new TSESLint.RuleTester({
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
ecmaFeatures: {},
/**
* Project is needed to generate the parserServices
* within @typescript-eslint/parser
*/
project: './tests/fixture-project/tsconfig.json',
},
parser: require.resolve('@typescript-eslint/parser'),
});
/**
* Inline rules should be supported
*/
const tslintRulesConfig: Options = [
{
rules: {
semicolon: [true, 'always'],
},
},
];
/**
* Custom rules directories should be supported
*/
const tslintRulesDirectoryConfig: Options = [
{
rulesDirectory: ['./tests/test-tslint-rules-directory'],
rules: {
'always-fail': {
severity: 'error',
},
},
},
];
ruleTester.run('tslint/config', rule, {
valid: [
{
code: 'var foo = true;',
options: tslintRulesConfig,
filename: './tests/fixture-project/1.ts',
},
{
filename: './tests/test-project/file-spec.ts',
code: readFileSync('./tests/test-project/file-spec.ts', 'utf8').replace(
/\n/g,
' ',
),
parserOptions: {
project: `${__dirname}/test-project/tsconfig.json`,
},
options: tslintRulesConfig,
},
{
code: 'throw "should be ok because rule is not loaded";',
options: tslintRulesConfig,
filename: './tests/fixture-project/2.ts',
},
],
invalid: [
{
options: [{ lintFile: './tests/test-project/tslint.json' }],
code: 'throw "err" // no-string-throw',
filename: './tests/fixture-project/3.ts',
errors: [
{
messageId: 'failure',
data: {
message:
'Throwing plain strings (not instances of Error) gives no stack traces',
ruleName: 'no-string-throw',
},
},
],
},
{
code: 'var foo = true // semicolon',
options: tslintRulesConfig,
output: 'var foo = true; // semicolon',
filename: './tests/fixture-project/4.ts',
errors: [
{
messageId: 'failure',
data: {
message: 'Missing semicolon',
ruleName: 'semicolon',
},
line: 1,
column: 15,
},
],
},
{
code: 'var foo = true // fail',
options: tslintRulesDirectoryConfig,
output: 'var foo = true // fail',
filename: './tests/fixture-project/5.ts',
errors: [
{
messageId: 'failure',
data: {
message: 'failure',
ruleName: 'always-fail',
},
line: 1,
column: 1,
},
],
},
{
filename: './tests/test-project/source.ts',
code: readFileSync('./tests/test-project/source.ts', 'utf8').replace(
/\n/g,
' ',
),
parserOptions: {
project: `${__dirname}/test-project/tsconfig.json`,
},
options: [
{
rulesDirectory: [
`${__dirname}/../../../node_modules/tslint/lib/rules`,
],
rules: { 'restrict-plus-operands': true },
},
],
errors: [
{
messageId: 'failure',
data: {
message:
'Operands of \'+\' operation must either be both strings or both numbers or both bigints, but found 1 + "2". Consider using template literals.',
ruleName: 'restrict-plus-operands',
},
},
],
},
],
});
describe('tslint/error', () => {
function testOutput(code: string, config: TSESLint.Linter.Config): void {
const linter = new TSESLint.Linter();
linter.defineRule('tslint/config', rule);
linter.defineParser('@typescript-eslint/parser', parser);
expect(() => linter.verify(code, config)).toThrow(
`You must provide a value for the "parserOptions.project" property for @typescript-eslint/parser`,
);
}
it('should error on missing project', () => {
testOutput('foo;', {
rules: {
'tslint/config': [2, tslintRulesConfig],
},
parser: '@typescript-eslint/parser',
});
});
it('should error on default parser', () => {
testOutput('foo;', {
parserOptions: {
project: `${__dirname}/test-project/tsconfig.json`,
},
rules: {
'tslint/config': [2, tslintRulesConfig],
},
});
});
it('should not crash if there are no tslint rules specified', () => {
const linter = new TSESLint.Linter();
jest.spyOn(console, 'warn').mockImplementation();
linter.defineRule('tslint/config', rule);
linter.defineParser('@typescript-eslint/parser', parser);
expect(() =>
linter.verify(
'foo;',
{
parserOptions: {
project: `${__dirname}/test-project/tsconfig.json`,
},
rules: {
'tslint/config': [2, {}],
},
parser: '@typescript-eslint/parser',
},
`${__dirname}/test-project/extra.ts`,
),
).not.toThrow();
expect(console.warn).toHaveBeenCalledWith(
expect.stringContaining(
`Tried to lint ${__dirname}/test-project/extra.ts but found no valid, enabled rules for this file type and file path in the resolved configuration.`,
),
);
jest.resetAllMocks();
});
});