Skip to content

Commit

Permalink
feat(ast-spec): add fixtures
Browse files Browse the repository at this point in the history
Add fixture tests for each and every AST node.
To make things more readable I:
- separated the AST from the tokens
  - this stops the individual snapshot from being so long it's impossible to understand
  - if there are changes to tokens - then they should be more obvious now. Before you had to expand the snapshot in github to understand that the token was part of the tokens array.
- always write error snapshot, and always write AST/token snapshots
  - This is just for consistency and to make it easier whilst you're developing.
  - It will prevent us from accidentally leaving behind error snapshots if they weren't supposed to be there.
- added a custom snapshot serializer
  - Having learned how useful they are with scope manager - I created a new one to improve the look of the snapshots.
  - Instead of sorting alphabetically, I place `type` at the top, and `range`/`loc` at the end.
  - Instead of outputting `Object` ahead of every node, instead it outputs the node `type`.
  - I adjusted the output `range`/`loc` so they take up fewer lines and are more compact.

I prefixed the snapshot names with numbers just so we can control the sorting of them. No other reason.
  • Loading branch information
bradzacher committed May 30, 2021
1 parent b1d4449 commit e0572c1
Show file tree
Hide file tree
Showing 125 changed files with 4,642 additions and 21 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -91,6 +91,12 @@ jobs:
run: |
yarn build
- name: Run unit tests for ast-spec
run: yarn test
working-directory: packages/ast-spec
env:
CI: true

- name: Run unit tests for typescript-estree
run: yarn test
working-directory: packages/typescript-estree
Expand Down Expand Up @@ -301,6 +307,7 @@ jobs:
env:
CI: true

# ast-spec is internal only - so don't care about compat on other versions
# eslint-plugin-internal is internal only - so don't care about compat on other versions

publish_canary_version:
Expand Down
32 changes: 32 additions & 0 deletions .vscode/launch.json
Expand Up @@ -196,5 +196,37 @@
"${workspaceFolder}/packages/scope-manager/dist/index.js",
],
},
{
"type": "node",
"request": "launch",
"name": "Run currently opened ast-spec test",
"cwd": "${workspaceFolder}/packages/ast-spec/",
"program": "${workspaceFolder}/node_modules/jest/bin/jest.js",
"args": [
"--runInBand",
"--no-cache",
"--no-coverage",
"${fileBasename}"
],
"sourceMaps": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/parser/src/index.ts",
"${workspaceFolder}/packages/parser/dist/index.js",
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
"${workspaceFolder}/packages/typescript-estree/dist/index.js",
"${workspaceFolder}/packages/types/src/index.ts",
"${workspaceFolder}/packages/types/dist/index.js",
"${workspaceFolder}/packages/visitor-keys/src/index.ts",
"${workspaceFolder}/packages/visitor-keys/dist/index.js",
"${workspaceFolder}/packages/scope-manager/dist/index.js",
"${workspaceFolder}/packages/scope-manager/dist/index.js",
],
},
]
}
1 change: 1 addition & 0 deletions .vscode/settings.json
Expand Up @@ -31,6 +31,7 @@
".tsx.shot",
".js.shot",
".jsx.shot",
".shot",
],
"extends": "jest_snapshot"
},
Expand Down
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -17,7 +17,7 @@
"url": "https://github.com/typescript-eslint/typescript-eslint/issues"
},
"scripts": {
"build": "lerna run build --ignore ast-spec",
"build": "lerna run build --ignore @typescript-eslint/ast-spec",
"check:clean-workspace-after-install": "git diff --quiet --exit-code",
"check:configs": "lerna run check:configs",
"check:docs": "lerna run check:docs",
Expand Down Expand Up @@ -72,6 +72,7 @@
},
"devDependencies": {
"@babel/code-frame": "^7.12.13",
"@babel/eslint-parser": "^7.14.4",
"@babel/parser": "^7.14.4",
"@babel/types": "^7.14.4",
"@commitlint/cli": "^12.1.4",
Expand Down Expand Up @@ -105,6 +106,8 @@
"glob": "^7.1.7",
"husky": "^5.0.9",
"jest": "^27.0.3",
"jest-diff": "^27.0.2",
"jest-snapshot": "^27.0.2",
"jest-specific-snapshot": "^5.0.0",
"lerna": "^3.22.1",
"lint-staged": "^11.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/ast-spec/jest.config.js
Expand Up @@ -17,4 +17,5 @@ module.exports = {
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
coverageReporters: ['text-summary', 'lcov'],
setupFilesAfterEnv: ['./tests/util/setupJest.ts'],
};
14 changes: 13 additions & 1 deletion packages/ast-spec/package.json
Expand Up @@ -34,13 +34,25 @@
"postclean": "rimraf dist && rimraf _ts3.4 && rimraf .rollup.cache && rimraf coverage",
"format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore",
"lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'",
"test": "jest",
"typecheck": "tsc -p tsconfig.json --noEmit"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/typescript-eslint"
},
"devDependencies": {
"@microsoft/api-extractor": "^7.15.2"
"@babel/core": "*",
"@babel/eslint-parser": "*",
"@babel/parser": "*",
"@microsoft/api-extractor": "^7.15.2",
"@types/babel__core": "*",
"@typescript-eslint/typescript-estree": "4.25.0",
"glob": "*",
"jest-diff": "*",
"jest-snapshot": "*",
"jest-specific-snapshot": "*",
"make-dir": "*",
"pretty-format": "*"
}
}
@@ -0,0 +1 @@
abstract class Foo {}
@@ -0,0 +1,47 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration abstract TSESTree - AST 1`] = `
Program {
type: "Program",
body: Array [
ClassDeclaration {
type: "ClassDeclaration",
abstract: true,
body: ClassBody {
type: "ClassBody",
body: Array [],

range: [19, 21],
loc: {
start: { column: 19, line: 1 },
end: { column: 21, line: 1 },
},
},
id: Identifier {
type: "Identifier",
name: "Foo",

range: [15, 18],
loc: {
start: { column: 15, line: 1 },
end: { column: 18, line: 1 },
},
},
superClass: null,

range: [0, 21],
loc: {
start: { column: 0, line: 1 },
end: { column: 21, line: 1 },
},
},
],
sourceType: "script",

range: [0, 22],
loc: {
start: { column: 0, line: 1 },
end: { column: 0, line: 2 },
},
}
`;
@@ -0,0 +1,56 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration abstract TSESTree - Tokens 1`] = `
Array [
Identifier {
type: "Identifier",
value: "abstract",

range: [0, 8],
loc: {
start: { column: 0, line: 1 },
end: { column: 8, line: 1 },
},
},
Keyword {
type: "Keyword",
value: "class",

range: [9, 14],
loc: {
start: { column: 9, line: 1 },
end: { column: 14, line: 1 },
},
},
Identifier {
type: "Identifier",
value: "Foo",

range: [15, 18],
loc: {
start: { column: 15, line: 1 },
end: { column: 18, line: 1 },
},
},
Punctuator {
type: "Punctuator",
value: "{",

range: [19, 20],
loc: {
start: { column: 19, line: 1 },
end: { column: 20, line: 1 },
},
},
Punctuator {
type: "Punctuator",
value: "}",

range: [20, 21],
loc: {
start: { column: 20, line: 1 },
end: { column: 21, line: 1 },
},
},
]
`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration abstract TSESTree - Error 1`] = `"NO ERROR"`;
@@ -0,0 +1,47 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration abstract Babel - AST 1`] = `
Program {
type: "Program",
body: Array [
ClassDeclaration {
type: "ClassDeclaration",
abstract: true,
body: ClassBody {
type: "ClassBody",
body: Array [],

range: [19, 21],
loc: {
start: { column: 19, line: 1 },
end: { column: 21, line: 1 },
},
},
id: Identifier {
type: "Identifier",
name: "Foo",

range: [15, 18],
loc: {
start: { column: 15, line: 1 },
end: { column: 18, line: 1 },
},
},
superClass: null,

range: [0, 21],
loc: {
start: { column: 0, line: 1 },
end: { column: 21, line: 1 },
},
},
],
sourceType: "script",

range: [0, 22],
loc: {
start: { column: 0, line: 1 },
end: { column: 0, line: 2 },
},
}
`;
@@ -0,0 +1,56 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration abstract Babel - Tokens 1`] = `
Array [
Identifier {
type: "Identifier",
value: "abstract",

range: [0, 8],
loc: {
start: { column: 0, line: 1 },
end: { column: 8, line: 1 },
},
},
Keyword {
type: "Keyword",
value: "class",

range: [9, 14],
loc: {
start: { column: 9, line: 1 },
end: { column: 14, line: 1 },
},
},
Identifier {
type: "Identifier",
value: "Foo",

range: [15, 18],
loc: {
start: { column: 15, line: 1 },
end: { column: 18, line: 1 },
},
},
Punctuator {
type: "Punctuator",
value: "{",

range: [19, 20],
loc: {
start: { column: 19, line: 1 },
end: { column: 20, line: 1 },
},
},
Punctuator {
type: "Punctuator",
value: "}",

range: [20, 21],
loc: {
start: { column: 20, line: 1 },
end: { column: 21, line: 1 },
},
},
]
`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration abstract Babel - Error 1`] = `"NO ERROR"`;
@@ -0,0 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration abstract AST Alignment - AST 1`] = `
"Snapshot Diff:
Compared values have no visual difference."
`;
@@ -0,0 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration abstract AST Alignment - Token 1`] = `
"Snapshot Diff:
Compared values have no visual difference."
`;
@@ -0,0 +1 @@
declare class Foo {}

0 comments on commit e0572c1

Please sign in to comment.