Skip to content

Commit

Permalink
test(parser): update parser tsx tests to not use eslint (#4323)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Dec 20, 2021
1 parent 234be00 commit b57aa90
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 77 deletions.
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
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
133 changes: 60 additions & 73 deletions packages/parser/tests/lib/tsx.ts
@@ -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
@@ -0,0 +1,18 @@
import type { Plugin } from 'pretty-format';
import { TSError } from '@typescript-eslint/typescript-estree/dist/node-utils';

export const serializer: Plugin = {
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` +
`}`
);
},
};

0 comments on commit b57aa90

Please sign in to comment.