Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(parser): update parser tsx tests to not use eslint (#4323)
- Loading branch information
Showing
4 changed files
with
81 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = { | ||
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` + | ||
`}` | ||
); | ||
}, | ||
}; |