-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
TypeScriptVueExtension.spec.ts
220 lines (201 loc) · 7.15 KB
/
TypeScriptVueExtension.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { join } from 'path';
import { createSandbox, Sandbox } from './sandbox/Sandbox';
import { readFixture } from './sandbox/Fixture';
import {
createWebpackDevServerDriver,
WEBPACK_CLI_VERSION,
WEBPACK_DEV_SERVER_VERSION,
} from './sandbox/WebpackDevServerDriver';
import { FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION } from './sandbox/Plugin';
import semver from 'semver/preload';
describe('TypeScript Vue Extension', () => {
let sandbox: Sandbox;
beforeAll(async () => {
sandbox = await createSandbox();
});
beforeEach(async () => {
await sandbox.reset();
});
afterAll(async () => {
await sandbox.cleanup();
});
it.each([
{
async: false,
typescript: '^3.8.0',
tsloader: '^7.0.0',
vueloader: '^15.8.3',
vue: '^2.0.0',
compiler: 'vue-template-compiler',
qrcodevue: '^1.7.0',
},
{
async: true,
typescript: '^3.8.0',
tsloader: '^7.0.0',
vueloader: 'v16.8.3',
vue: '^3.0.0',
compiler: '@vue/compiler-sfc',
qrcodevue: '^3.0.0',
},
])(
'reports semantic error for %p',
async ({ async, typescript, tsloader, vueloader, vue, compiler, qrcodevue }) => {
const fixtures = [
await readFixture(join(__dirname, 'fixtures/environment/typescript-vue.fixture'), {
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION: JSON.stringify(
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION
),
TS_LOADER_VERSION: JSON.stringify(tsloader),
TYPESCRIPT_VERSION: JSON.stringify(typescript),
WEBPACK_VERSION: JSON.stringify('^4.0.0'),
WEBPACK_CLI_VERSION: JSON.stringify(WEBPACK_CLI_VERSION),
WEBPACK_DEV_SERVER_VERSION: JSON.stringify(WEBPACK_DEV_SERVER_VERSION),
VUE_LOADER_VERSION: JSON.stringify(vueloader),
VUE_VERSION: JSON.stringify(vue),
VUE_COMPILER: JSON.stringify(compiler),
QRCODE_VUE_VERSION: JSON.stringify(qrcodevue),
ASYNC: JSON.stringify(async),
}),
await readFixture(join(__dirname, 'fixtures/implementation/typescript-vue-shared.fixture')),
];
if (semver.satisfies('2.0.0', vue)) {
fixtures.push(
await readFixture(join(__dirname, 'fixtures/implementation/typescript-vue2.fixture'))
);
} else if (semver.satisfies('3.0.0', vue)) {
fixtures.push(
await readFixture(join(__dirname, 'fixtures/implementation/typescript-vue3.fixture'))
);
}
await sandbox.load(fixtures);
if (semver.satisfies('2.0.0', vue)) {
await sandbox.write(
'src/vue-shim.d.ts',
[
'declare module "*.vue" {',
' import Vue from "vue";',
' export default Vue;',
'}',
].join('\n')
);
} else if (semver.satisfies('3.0.0', vue)) {
await sandbox.write('src/vue-shim.d.ts', 'declare module "*.vue";');
}
const driver = createWebpackDevServerDriver(
sandbox.spawn('npm run webpack-dev-server'),
async
);
let errors: string[] = [];
// first compilation is successful
await driver.waitForNoErrors();
// modify user model file
await sandbox.patch(
'src/component/LoggedIn.vue',
"import User, { getUserName } from '@/model/User';",
"import User from '@/model/User';"
);
// next compilation should have missing function error
errors = await driver.waitForErrors();
expect(errors).toEqual([
[
'ERROR in src/component/LoggedIn.vue:27:21',
"TS2304: Cannot find name 'getUserName'.",
' 25 | const user: User = this.user;',
' 26 |',
" > 27 | return user ? getUserName(user) : '';",
' | ^^^^^^^^^^^',
' 28 | }',
' 29 | },',
' 30 | async logout() {',
].join('\n'),
]);
// fix it
await sandbox.patch(
'src/component/LoggedIn.vue',
"return user ? getUserName(user) : '';",
"return user ? `${user.firstName} ${user.lastName}` : '';"
);
await driver.waitForNoErrors();
// modify user model file again
await sandbox.patch('src/model/User.ts', ' firstName?: string;\n', '');
// not we should have an error about missing firstName property
errors = await driver.waitForErrors();
expect(errors).toEqual([
[
'ERROR in src/component/LoggedIn.vue:27:29',
"TS2339: Property 'firstName' does not exist on type 'User'.",
' 25 | const user: User = this.user;',
' 26 |',
" > 27 | return user ? `${user.firstName} ${user.lastName}` : '';",
' | ^^^^^^^^^',
' 28 | }',
' 29 | },',
' 30 | async logout() {',
].join('\n'),
[
'ERROR in src/model/User.ts:11:16',
"TS2339: Property 'firstName' does not exist on type 'User'.",
' 9 |',
' 10 | function getUserName(user: User): string {',
' > 11 | return [user.firstName, user.lastName]',
' | ^^^^^^^^^',
' 12 | .filter(name => name !== undefined)',
" 13 | .join(' ');",
' 14 | }',
].join('\n'),
]);
// fix the error
await sandbox.patch(
'src/model/User.ts',
' lastName?: string;',
[' firstName?: string;', ' lastName?: string;'].join('\n')
);
await driver.waitForNoErrors();
if (semver.satisfies('3.0.0', vue)) {
await sandbox.patch(
'src/component/Header.vue',
'defineProps({',
['let x: number = "1"', 'defineProps({'].join('\n')
);
errors = await driver.waitForErrors();
expect(errors).toEqual([
[
'ERROR in src/component/Header.vue:6:5',
"TS2322: Type '\"1\"' is not assignable to type 'number'.",
' 4 |',
' 5 | <script setup lang="ts">',
' > 6 | let x: number = "1"',
' | ^',
' 7 | defineProps({',
' 8 | title: String,',
' 9 | });',
].join('\n'),
]);
// fix the issue
await sandbox.patch('src/component/Header.vue', 'let x: number = "1"', '');
await driver.waitForNoErrors();
// introduce error in second <script>
await sandbox.patch(
'src/component/Logo.vue',
'export default {',
['let x: number = "1";', 'export default {'].join('\n')
);
errors = await driver.waitForErrors();
expect(errors).toEqual([
[
'ERROR in src/component/Logo.vue:15:5',
"TS2322: Type '\"1\"' is not assignable to type 'number'.",
' 13 |',
' 14 | <script lang="ts">',
' > 15 | let x: number = "1";',
' | ^',
' 16 | export default {',
' 17 | inheritAttrs: false,',
' 18 | customOptions: {}',
].join('\n'),
]);
}
}
);
});