Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2eb3ca4

Browse files
authored
test(typescript-estree): separate snapshots for ast tests (typescript-eslint#2290)
1 parent 9458130 commit 2eb3ca4

File tree

859 files changed

+341926
-365931
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

859 files changed

+341926
-365931
lines changed

.eslintrc.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,9 @@ module.exports = {
161161
// all test files
162162
{
163163
files: [
164-
'packages/eslint-plugin-internal/tests/**/*.test.ts',
165-
'packages/eslint-plugin-tslint/tests/**/*.ts',
166-
'packages/eslint-plugin/tests/**/*.test.ts',
164+
'packages/*/tests/**/*.test.ts',
165+
'packages/*/tests/**/*.spec.ts',
167166
'packages/parser/tests/**/*.ts',
168-
'packages/typescript-estree/tests/**/*.ts',
169167
],
170168
env: {
171169
'jest/globals': true,

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
"@commitlint/cli": "^8.3.5",
7070
"@commitlint/config-conventional": "^8.3.4",
7171
"@commitlint/config-lerna-scopes": "^8.3.4",
72+
"@types/glob": "^7.1.1",
73+
"@types/jest-specific-snapshot": "^0.5.4",
7274
"@types/jest": "^25.2.1",
7375
"@types/node": "^13.13.5",
7476
"@types/prettier": "^2.0.0",
@@ -82,11 +84,14 @@
8284
"eslint-plugin-eslint-plugin": "^2.2.1",
8385
"eslint-plugin-import": "^2.20.2",
8486
"eslint-plugin-jest": "^23.10.0",
87+
"glob": "^7.1.6",
8588
"husky": "^4.2.5",
8689
"isomorphic-fetch": "^2.2.1",
8790
"jest": "^25.5.4",
91+
"jest-specific-snapshot": "^3.0.0",
8892
"lerna": "^3.20.2",
8993
"lint-staged": "^10.2.2",
94+
"make-dir": "^3.1.0",
9095
"markdownlint-cli": "^0.23.0",
9196
"prettier": "^2.0.5",
9297
"rimraf": "^3.0.2",

packages/scope-manager/package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,10 @@
4242
"@typescript-eslint/visitor-keys": "3.6.1"
4343
},
4444
"devDependencies": {
45-
"@types/jest-specific-snapshot": "^0.5.4",
46-
"@types/mkdirp": "^1.0.0",
4745
"@typescript-eslint/typescript-estree": "3.6.1",
48-
"glob": "^7.1.6",
49-
"jest-specific-snapshot": "^3.0.0",
50-
"make-dir": "^3.1.0",
46+
"glob": "*",
47+
"jest-specific-snapshot": "*",
48+
"make-dir": "*",
5149
"prettier": "*",
5250
"pretty-format": "^25.5.0",
5351
"rimraf": "*",

packages/scope-manager/tests/eslint-scope/label.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('label', () => {
4747
expectToBeGlobalScope(scope);
4848
expect(scope.variables).toHaveLength(1);
4949
expect(scope.variables[0].name).toBe('foo');
50-
expect(scope.through.length).toBe(3);
50+
expect(scope.through).toHaveLength(3);
5151
expect(scope.through[2].identifier.name).toBe('foo');
5252
expect(scope.through[2].isRead()).toBeTruthy();
5353

packages/scope-manager/tests/fixtures.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ function nestDescribe(
145145
};
146146

147147
if ([...fixture.segments, fixture.name].join(path.sep) === ONLY) {
148+
// eslint-disable-next-line jest/no-focused-tests
148149
it.only(fixture.name, test);
149150
} else {
150151
it(fixture.name, test);
@@ -153,3 +154,23 @@ function nestDescribe(
153154
}
154155

155156
fixtures.forEach(f => nestDescribe(f));
157+
158+
if (ONLY === '') {
159+
// ensure that the snapshots are cleaned up, because jest-specific-snapshot won't do this check
160+
const snapshots = glob.sync(`${FIXTURES_DIR}/**/*.shot`).map(absolute => {
161+
const relative = path.relative(FIXTURES_DIR, absolute);
162+
const { name, dir } = path.parse(relative);
163+
return {
164+
relative,
165+
fixturePath: path.join(FIXTURES_DIR, dir, name),
166+
};
167+
});
168+
169+
describe('ast snapshots should have an associated test', () => {
170+
for (const snap of snapshots) {
171+
it(snap.relative, () => {
172+
fs.existsSync(snap.fixturePath);
173+
});
174+
}
175+
});
176+
}

packages/scope-manager/tests/lib.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('implicit lib definitions', () => {
88
});
99

1010
const variables = scopeManager.variables;
11-
expect(variables.length).toBe(0);
11+
expect(variables).toHaveLength(0);
1212
});
1313

1414
it('should define implicit variables', () => {

packages/typescript-estree/jest.config.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
module.exports = {
44
testEnvironment: 'node',
55
transform: {
6-
'^.+\\.tsx?$': 'ts-jest',
6+
[/^.+\.tsx?$/.source]: 'ts-jest',
77
},
8-
testRegex: './tests/(lib/.*\\.(jsx?|tsx?)|ast-alignment/spec\\.ts)$',
8+
testRegex: [
9+
/.\/tests\/lib\/.*\.ts$/.source,
10+
/.\/tests\/ast-alignment\/spec\.ts$/.source,
11+
/.\/tests\/[^\/]+\.test\.ts$/.source,
12+
],
913
collectCoverage: false,
1014
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}'],
1115
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],

packages/typescript-estree/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@
5555
"@babel/types": "^7.8.3",
5656
"@types/babel__code-frame": "^7.0.1",
5757
"@types/debug": "^4.1.5",
58-
"@types/glob": "^7.1.1",
5958
"@types/is-glob": "^4.0.1",
6059
"@types/lodash": "^4.14.149",
6160
"@types/semver": "^7.1.0",
6261
"@types/tmp": "^0.2.0",
6362
"@typescript-eslint/shared-fixtures": "3.6.1",
63+
"glob": "*",
64+
"jest-specific-snapshot": "*",
65+
"make-dir": "*",
6466
"tmp": "^0.2.1",
6567
"typescript": "*"
6668
},
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import fs from 'fs';
2+
import glob from 'glob';
3+
import 'jest-specific-snapshot';
4+
import makeDir from 'make-dir';
5+
import path from 'path';
6+
import { parseAndGenerateServices } from '../src/parser';
7+
8+
// Assign a segment set to this variable to limit the test to only this segment
9+
// This is super helpful if you need to debug why a specific fixture isn't producing the correct output
10+
// eg. ['type-declaration', 'signatures', 'method-generic'] will only test /type-declaration/signatures/method-generic.ts
11+
// prettier-ignore
12+
const ONLY = [].join(path.sep);
13+
14+
const FIXTURES_DIR = path.resolve(
15+
__dirname,
16+
'..',
17+
'..',
18+
'..',
19+
'node_modules',
20+
'@typescript-eslint',
21+
'shared-fixtures',
22+
'fixtures',
23+
);
24+
const SNAPSHOTS_DIR = path.resolve(__dirname, 'snapshots');
25+
26+
const JSX_REGEX = /\.[jt]sx$/;
27+
const fixtures = glob
28+
.sync(`${FIXTURES_DIR}/**/*.src.{js,ts,jsx,tsx}`)
29+
.map(absolute => {
30+
const relative = path.relative(FIXTURES_DIR, absolute);
31+
const { name, dir, ext } = path.parse(relative);
32+
const segments = dir.split(path.sep);
33+
const snapshotPath = path.join(SNAPSHOTS_DIR, dir);
34+
return {
35+
absolute,
36+
isJsx: JSX_REGEX.test(name),
37+
name,
38+
segments,
39+
snapshotPath,
40+
snapshotFile: path.join(snapshotPath, `${name}${ext}.shot`),
41+
};
42+
});
43+
44+
function nestDescribe(
45+
fixture: typeof fixtures[number],
46+
segments = fixture.segments,
47+
): void {
48+
if (segments.length > 0) {
49+
describe(segments[0], () => {
50+
nestDescribe(fixture, segments.slice(1));
51+
});
52+
} else {
53+
const test = (): void => {
54+
const contents = fs.readFileSync(fixture.absolute, 'utf8');
55+
56+
try {
57+
makeDir.sync(fixture.snapshotPath);
58+
} catch (e) {
59+
if ('code' in e && e.code === 'EEXIST') {
60+
// already exists - ignored
61+
} else {
62+
throw e;
63+
}
64+
}
65+
66+
try {
67+
const { ast } = parseAndGenerateServices(contents, {
68+
comment: true,
69+
errorOnUnknownASTType: true,
70+
jsx: fixture.isJsx,
71+
loc: true,
72+
range: true,
73+
tokens: true,
74+
});
75+
expect(ast).toMatchSpecificSnapshot(fixture.snapshotFile);
76+
} catch (e) {
77+
expect(e).toMatchSpecificSnapshot(fixture.snapshotFile);
78+
}
79+
};
80+
81+
if ([...fixture.segments, fixture.name].join(path.sep) === ONLY) {
82+
// eslint-disable-next-line jest/no-focused-tests
83+
it.only(fixture.name, test);
84+
} else {
85+
it(fixture.name, test);
86+
}
87+
}
88+
}
89+
90+
fixtures.forEach(f => nestDescribe(f));
91+
92+
if (ONLY === '') {
93+
// ensure that the snapshots are cleaned up, because jest-specific-snapshot won't do this check
94+
const snapshots = glob.sync(`${SNAPSHOTS_DIR}/**/*.shot`).map(absolute => {
95+
const relative = path.relative(SNAPSHOTS_DIR, absolute);
96+
const { name, dir } = path.parse(relative);
97+
return {
98+
relative,
99+
fixturePath: path.join(FIXTURES_DIR, dir, name),
100+
};
101+
});
102+
103+
describe('ast snapshots should have an associated test', () => {
104+
for (const snap of snapshots) {
105+
it(snap.relative, () => {
106+
fs.existsSync(snap.fixturePath);
107+
});
108+
}
109+
});
110+
}

0 commit comments

Comments
 (0)