Skip to content
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

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

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/parser/package.json
Expand Up @@ -51,7 +51,6 @@
},
"devDependencies": {
"@types/glob": "*",
"@typescript-eslint/experimental-utils": "5.7.0",
"glob": "*",
"typescript": "*"
},
Expand Down
28 changes: 28 additions & 0 deletions packages/parser/tests/lib/__snapshots__/tsx.ts.snap
@@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`TSX if the filename ends with '.tsx', enable jsx option automatically. filePath was not provided 1`] = `
TSError {
"column": 18,
"index": 18,
"lineNumber": 1,
"message": "'>' expected.",
}
`;

exports[`TSX if the filename ends with '.tsx', enable jsx option automatically. test.ts 1`] = `
TSError {
"column": 18,
"index": 18,
"lineNumber": 1,
"message": "'>' expected.",
}
`;

exports[`TSX if the filename ends with '.tsx', enable jsx option automatically. test.ts with 'jsx:true' option 1`] = `
TSError {
"column": 18,
"index": 18,
"lineNumber": 1,
"message": "'>' expected.",
}
`;
6 changes: 3 additions & 3 deletions packages/parser/tests/lib/parser.ts
@@ -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
112 changes: 38 additions & 74 deletions packages/parser/tests/lib/tsx.ts
@@ -1,115 +1,79 @@
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';

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

expect.addSnapshotSerializer(serializer);

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,
},
]);
try {
parseForESLint(code);
} catch (e) {
expect(e).toMatchSnapshot();
armano2 marked this conversation as resolved.
Show resolved Hide resolved
}
});

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,
},
]);
try {
parseForESLint(code, {
filePath: 'test.ts',
});
} catch (e) {
expect(e).toMatchSnapshot();
}
});

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

try {
parseForESLint(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,
},
]);
});
} catch (e) {
expect(e).toMatchSnapshot();
}
});

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
@@ -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
Member 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` +
`}`
);
},
};