-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathparsers.it.spec.ts
115 lines (99 loc) · 4.45 KB
/
parsers.it.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { promises as fsPromises } from 'fs';
import { expect } from 'chai';
import { createParser, ParserOptions } from '../../src/parsers';
import { AstFormat, HtmlAst, TSAst, JSAst, Ast } from '../../src/syntax';
import { createParserOptions } from '../helpers/factories';
import { resolveTestResource } from '../helpers/resolve-test-resource';
const resolveParserTestResource = resolveTestResource.bind(null, 'parser');
describe('parsers integration', () => {
it('should allow to parse html with script tags', async () => {
const actual = await actAssertHtml('index.html');
expect(actual.root.scripts).lengthOf(2);
expect(actual).to.matchSnapshot();
});
it('should allow to parse a *.vue file', async () => {
const actual = await actAssertHtml('App.vue');
expect(actual.format).eq(AstFormat.Html);
expect(actual.root.scripts).lengthOf(1);
expect(actual).to.matchSnapshot();
});
it('should allow to parse a an angular file', async () => {
const actual = await actAssertTS('app.component.ts');
expect(actual).to.matchSnapshot();
});
it('should allow to parse a tsx file', async () => {
const actual = await actAssertTsx('App.tsx');
expect(actual).to.matchSnapshot();
});
it('should allow to parse a react file with custom babelrc file', async () => {
const actual = await actAssertJS('jsx-with-babelrc/Badge.js');
expect(actual).to.matchSnapshot();
});
it('should allow to parse a react file with project-wide configuration file', async () => {
process.chdir(resolveParserTestResource('jsx-with-project-wide-config'));
const actual = await actAssertJS('jsx-with-project-wide-config/src/Badge.js');
expect(actual).to.matchSnapshot();
});
it('should ignore configuration when parsing ts files', async () => {
process.chdir(resolveParserTestResource('ts-in-babel-project'));
const actual = await actAssertTS('ts-in-babel-project/src/app.ts');
expect(actual).to.matchSnapshot();
});
it('should be able to parse a ts file with more recent TS features', async () => {
const actual = await actAssertTS('new-ts-features.ts');
expect(actual).to.matchSnapshot();
});
it('should allow a plugin that conflicts with the default plugins as long as plugins are emptied out', async () => {
process.chdir(resolveParserTestResource('js-in-babel-project'));
const actual = await actAssertJS('js-in-babel-project/src/app.js', createParserOptions({ plugins: [] }));
expect(actual).to.matchSnapshot();
});
it('should allow to parse a mjs file', async () => {
const actual = await actAssertJS('app.mjs');
expect(actual).to.matchSnapshot();
});
it('should allow to parse a cjs file', async () => {
const actual = await actAssertJS('app.cjs');
expect(actual).to.matchSnapshot();
});
async function act(testResourceFileName: string, options: ParserOptions) {
const fileName = resolveParserTestResource(testResourceFileName);
const input = await fsPromises.readFile(fileName, 'utf8');
const actual = await createParser(options)(input, fileName);
cleanFileName(actual, testResourceFileName);
return actual;
}
async function actAssertHtml(testResourceFileName: string, options = createParserOptions()): Promise<HtmlAst> {
const actual = await act(testResourceFileName, options);
expect(actual.format).eq(AstFormat.Html);
return actual as HtmlAst;
}
async function actAssertTS(testResourceFileName: string, options = createParserOptions()): Promise<TSAst> {
const actual = await act(testResourceFileName, options);
expect(actual.format).eq(AstFormat.TS);
return actual as TSAst;
}
async function actAssertTsx(testResourceFileName: string, options = createParserOptions()): Promise<TSAst> {
const actual = await act(testResourceFileName, options);
expect(actual.format).eq(AstFormat.Tsx);
return actual as TSAst;
}
async function actAssertJS(testResourceFileName: string, options = createParserOptions()): Promise<JSAst> {
const actual = await act(testResourceFileName, options);
expect(actual.format).eq(AstFormat.JS);
return actual as JSAst;
}
/**
* Reset the file name, so snapshots are the same locally as in ci/cd
*/
function cleanFileName(ast: Ast, fileNameOverride: string) {
ast.originFileName = fileNameOverride;
switch (ast.format) {
case AstFormat.Html:
ast.root.scripts.forEach((script) => {
script.originFileName = fileNameOverride;
});
default:
}
}
});