Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b57aa90

Browse files
authored
test(parser): update parser tsx tests to not use eslint (typescript-eslint#4323)
1 parent 234be00 commit b57aa90

File tree

4 files changed

+81
-77
lines changed

4 files changed

+81
-77
lines changed

packages/parser/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
},
5252
"devDependencies": {
5353
"@types/glob": "*",
54-
"@typescript-eslint/experimental-utils": "5.7.0",
5554
"glob": "*",
5655
"typescript": "*"
5756
},

packages/parser/tests/lib/parser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TSESLint } from '@typescript-eslint/experimental-utils';
1+
import { ParserOptions } from '@typescript-eslint/types';
22
import * as typescriptESTree from '@typescript-eslint/typescript-estree/dist/parser';
33
import * as scopeManager from '@typescript-eslint/scope-manager/dist/analyze';
44
import { parse, parseForESLint } from '../../src/parser';
@@ -26,7 +26,7 @@ describe('parser', () => {
2626
it('parseAndGenerateServices() should be called with options', () => {
2727
const code = 'const valid = true;';
2828
const spy = jest.spyOn(typescriptESTree, 'parseAndGenerateServices');
29-
const config: TSESLint.ParserOptions = {
29+
const config: ParserOptions = {
3030
loc: false,
3131
comment: false,
3232
range: false,
@@ -80,7 +80,7 @@ describe('parser', () => {
8080
it('analyze() should be called with options', () => {
8181
const code = 'const valid = true;';
8282
const spy = jest.spyOn(scopeManager, 'analyze');
83-
const config: TSESLint.ParserOptions = {
83+
const config: ParserOptions = {
8484
loc: false,
8585
comment: false,
8686
range: false,

packages/parser/tests/lib/tsx.ts

Lines changed: 60 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,102 @@
1-
import { TSESLint } from '@typescript-eslint/experimental-utils';
2-
import * as parser from '../../src/parser';
1+
import { parseForESLint } from '../../src/parser';
2+
import { serializer } from '../tools/ts-error-serializer';
3+
import type { ParserOptions } from '@typescript-eslint/types';
34

45
//------------------------------------------------------------------------------
56
// Tests
67
//------------------------------------------------------------------------------
78

9+
expect.addSnapshotSerializer(serializer);
10+
11+
function parseWithError(code: string, options?: ParserOptions | null): unknown {
12+
try {
13+
return parseForESLint(code, options);
14+
} catch (e) {
15+
return e;
16+
}
17+
}
18+
819
describe('TSX', () => {
920
describe("if the filename ends with '.tsx', enable jsx option automatically.", () => {
10-
const linter = new TSESLint.Linter();
11-
linter.defineParser('@typescript-eslint/parser', parser);
12-
1321
it('filePath was not provided', () => {
1422
const code = 'const element = <T/>';
15-
const config = {
16-
parser: '@typescript-eslint/parser',
17-
};
18-
const messages = linter.verify(code, config);
1923

20-
expect(messages).toStrictEqual([
21-
{
22-
column: 18,
23-
fatal: true,
24-
line: 1,
25-
message: "Parsing error: '>' expected.",
26-
ruleId: null,
27-
severity: 2,
28-
},
29-
]);
24+
expect(parseWithError(code)).toMatchInlineSnapshot(`
25+
TSError {
26+
"column": 18,
27+
"index": 18,
28+
"lineNumber": 1,
29+
"message": "'>' expected.",
30+
}
31+
`);
3032
});
3133

3234
it("filePath was not provided and 'jsx:true' option", () => {
3335
const code = 'const element = <T/>';
34-
const config = {
35-
parser: '@typescript-eslint/parser',
36-
parserOptions: {
36+
expect(() =>
37+
parseForESLint(code, {
3738
ecmaFeatures: {
3839
jsx: true,
3940
},
40-
},
41-
};
42-
const messages = linter.verify(code, config);
43-
44-
expect(messages).toStrictEqual([]);
41+
}),
42+
).not.toThrow();
4543
});
4644

4745
it('test.ts', () => {
4846
const code = 'const element = <T/>';
49-
const config = {
50-
parser: '@typescript-eslint/parser',
51-
};
52-
const messages = linter.verify(code, config, { filename: 'test.ts' });
53-
54-
expect(messages).toStrictEqual([
55-
{
56-
column: 18,
57-
fatal: true,
58-
line: 1,
59-
message: "Parsing error: '>' expected.",
60-
ruleId: null,
61-
severity: 2,
62-
},
63-
]);
47+
expect(
48+
parseWithError(code, {
49+
filePath: 'test.ts',
50+
}),
51+
).toMatchInlineSnapshot(`
52+
TSError {
53+
"column": 18,
54+
"index": 18,
55+
"lineNumber": 1,
56+
"message": "'>' expected.",
57+
}
58+
`);
6459
});
6560

6661
it("test.ts with 'jsx:true' option", () => {
6762
const code = 'const element = <T/>';
68-
const config = {
69-
parser: '@typescript-eslint/parser',
70-
parserOptions: {
63+
64+
expect(
65+
parseWithError(code, {
66+
filePath: 'test.ts',
7167
ecmaFeatures: {
7268
jsx: true,
7369
},
74-
},
75-
};
76-
const messages = linter.verify(code, config, { filename: 'test.ts' });
77-
78-
expect(messages).toStrictEqual([
79-
{
80-
column: 18,
81-
fatal: true,
82-
line: 1,
83-
message: "Parsing error: '>' expected.",
84-
ruleId: null,
85-
severity: 2,
86-
},
87-
]);
70+
}),
71+
).toMatchInlineSnapshot(`
72+
TSError {
73+
"column": 18,
74+
"index": 18,
75+
"lineNumber": 1,
76+
"message": "'>' expected.",
77+
}
78+
`);
8879
});
8980

9081
it('test.tsx', () => {
9182
const code = 'const element = <T/>';
92-
const config = {
93-
parser: '@typescript-eslint/parser',
94-
};
95-
const messages = linter.verify(code, config, { filename: 'test.tsx' });
96-
97-
expect(messages).toStrictEqual([]);
83+
expect(() =>
84+
parseForESLint(code, {
85+
filePath: 'test.tsx',
86+
}),
87+
).not.toThrow();
9888
});
9989

10090
it("test.tsx with 'jsx:false' option", () => {
10191
const code = 'const element = <T/>';
102-
const config = {
103-
parser: '@typescript-eslint/parser',
104-
parserOptions: {
92+
expect(() =>
93+
parseForESLint(code, {
10594
ecmaFeatures: {
10695
jsx: false,
10796
},
108-
},
109-
};
110-
const messages = linter.verify(code, config, { filename: 'test.tsx' });
111-
112-
expect(messages).toStrictEqual([]);
97+
filePath: 'test.tsx',
98+
}),
99+
).not.toThrow();
113100
});
114101
});
115102
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { Plugin } from 'pretty-format';
2+
import { TSError } from '@typescript-eslint/typescript-estree/dist/node-utils';
3+
4+
export const serializer: Plugin = {
5+
test: (val: unknown): val is TSError => val instanceof TSError,
6+
serialize(val: TSError, config, indentation, depth, refs, printer) {
7+
const format = (value: unknown): string =>
8+
printer(value, config, indentation, depth + 1, refs);
9+
return (
10+
`${val.name} {\n` +
11+
`${config.indent}"column": ${format(val.column)},\n` +
12+
`${config.indent}"index": ${format(val.index)},\n` +
13+
`${config.indent}"lineNumber": ${format(val.lineNumber)},\n` +
14+
`${config.indent}"message": ${format(val.message)},\n` +
15+
`}`
16+
);
17+
},
18+
};

0 commit comments

Comments
 (0)