Skip to content

test(parser): update parser tsx tests to not use eslint #4323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
},
"devDependencies": {
"@types/glob": "*",
"@typescript-eslint/experimental-utils": "5.7.0",
"glob": "*",
"typescript": "*"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/parser/tests/lib/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TSESLint } from '@typescript-eslint/experimental-utils';
import { ParserOptions } from '@typescript-eslint/types';
import * as typescriptESTree from '@typescript-eslint/typescript-estree/dist/parser';
import * as scopeManager from '@typescript-eslint/scope-manager/dist/analyze';
import { parse, parseForESLint } from '../../src/parser';
Expand Down Expand Up @@ -26,7 +26,7 @@ describe('parser', () => {
it('parseAndGenerateServices() should be called with options', () => {
const code = 'const valid = true;';
const spy = jest.spyOn(typescriptESTree, 'parseAndGenerateServices');
const config: TSESLint.ParserOptions = {
const config: ParserOptions = {
loc: false,
comment: false,
range: false,
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('parser', () => {
it('analyze() should be called with options', () => {
const code = 'const valid = true;';
const spy = jest.spyOn(scopeManager, 'analyze');
const config: TSESLint.ParserOptions = {
const config: ParserOptions = {
loc: false,
comment: false,
range: false,
Expand Down
133 changes: 60 additions & 73 deletions packages/parser/tests/lib/tsx.ts
Original file line number Diff line number Diff line change
@@ -1,115 +1,102 @@
import { TSESLint } from '@typescript-eslint/experimental-utils';
import * as parser from '../../src/parser';
import { parseForESLint } from '../../src/parser';
import { serializer } from '../tools/ts-error-serializer';
import type { ParserOptions } from '@typescript-eslint/types';

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

expect.addSnapshotSerializer(serializer);

function parseWithError(code: string, options?: ParserOptions | null): unknown {
try {
return parseForESLint(code, options);
} catch (e) {
return e;
}
}

describe('TSX', () => {
describe("if the filename ends with '.tsx', enable jsx option automatically.", () => {
const linter = new TSESLint.Linter();
linter.defineParser('@typescript-eslint/parser', parser);

it('filePath was not provided', () => {
const code = 'const element = <T/>';
const config = {
parser: '@typescript-eslint/parser',
};
const messages = linter.verify(code, config);

expect(messages).toStrictEqual([
{
column: 18,
fatal: true,
line: 1,
message: "Parsing error: '>' expected.",
ruleId: null,
severity: 2,
},
]);
expect(parseWithError(code)).toMatchInlineSnapshot(`
TSError {
"column": 18,
"index": 18,
"lineNumber": 1,
"message": "'>' expected.",
}
`);
});

it("filePath was not provided and 'jsx:true' option", () => {
const code = 'const element = <T/>';
const config = {
parser: '@typescript-eslint/parser',
parserOptions: {
expect(() =>
parseForESLint(code, {
ecmaFeatures: {
jsx: true,
},
},
};
const messages = linter.verify(code, config);

expect(messages).toStrictEqual([]);
}),
).not.toThrow();
});

it('test.ts', () => {
const code = 'const element = <T/>';
const config = {
parser: '@typescript-eslint/parser',
};
const messages = linter.verify(code, config, { filename: 'test.ts' });

expect(messages).toStrictEqual([
{
column: 18,
fatal: true,
line: 1,
message: "Parsing error: '>' expected.",
ruleId: null,
severity: 2,
},
]);
expect(
parseWithError(code, {
filePath: 'test.ts',
}),
).toMatchInlineSnapshot(`
TSError {
"column": 18,
"index": 18,
"lineNumber": 1,
"message": "'>' expected.",
}
`);
});

it("test.ts with 'jsx:true' option", () => {
const code = 'const element = <T/>';
const config = {
parser: '@typescript-eslint/parser',
parserOptions: {

expect(
parseWithError(code, {
filePath: 'test.ts',
ecmaFeatures: {
jsx: true,
},
},
};
const messages = linter.verify(code, config, { filename: 'test.ts' });

expect(messages).toStrictEqual([
{
column: 18,
fatal: true,
line: 1,
message: "Parsing error: '>' expected.",
ruleId: null,
severity: 2,
},
]);
}),
).toMatchInlineSnapshot(`
TSError {
"column": 18,
"index": 18,
"lineNumber": 1,
"message": "'>' expected.",
}
`);
});

it('test.tsx', () => {
const code = 'const element = <T/>';
const config = {
parser: '@typescript-eslint/parser',
};
const messages = linter.verify(code, config, { filename: 'test.tsx' });

expect(messages).toStrictEqual([]);
expect(() =>
parseForESLint(code, {
filePath: 'test.tsx',
}),
).not.toThrow();
});

it("test.tsx with 'jsx:false' option", () => {
const code = 'const element = <T/>';
const config = {
parser: '@typescript-eslint/parser',
parserOptions: {
expect(() =>
parseForESLint(code, {
ecmaFeatures: {
jsx: false,
},
},
};
const messages = linter.verify(code, config, { filename: 'test.tsx' });

expect(messages).toStrictEqual([]);
filePath: 'test.tsx',
}),
).not.toThrow();
});
});
});
18 changes: 18 additions & 0 deletions packages/parser/tests/tools/ts-error-serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Plugin } from 'pretty-format';
import { TSError } from '@typescript-eslint/typescript-estree/dist/node-utils';

export const serializer: Plugin = {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is copy of serializer from typescript-estree, we can't just inport it as sertializers are not compiled/exposed

test: (val: unknown): val is TSError => val instanceof TSError,
serialize(val: TSError, config, indentation, depth, refs, printer) {
const format = (value: unknown): string =>
printer(value, config, indentation, depth + 1, refs);
return (
`${val.name} {\n` +
`${config.indent}"column": ${format(val.column)},\n` +
`${config.indent}"index": ${format(val.index)},\n` +
`${config.indent}"lineNumber": ${format(val.lineNumber)},\n` +
`${config.indent}"message": ${format(val.message)},\n` +
`}`
);
},
};