Skip to content

Commit 1af07ae

Browse files
committed
test: add tests for vue and pnp typescript extension
1 parent 25e1e54 commit 1af07ae

7 files changed

+1029
-334
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
import {
2+
createSandbox,
3+
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION,
4+
Sandbox,
5+
yarnInstaller,
6+
} from './sandbox/Sandbox';
7+
import { readFixture } from './sandbox/Fixture';
8+
import { join } from 'path';
9+
import {
10+
createWebpackDevServerDriver,
11+
WEBPACK_CLI_VERSION,
12+
WEBPACK_DEV_SERVER_VERSION,
13+
} from './sandbox/WebpackDevServerDriver';
14+
15+
describe('TypeScript PnP Extension', () => {
16+
let sandbox: Sandbox;
17+
18+
beforeAll(async () => {
19+
sandbox = await createSandbox();
20+
});
21+
22+
beforeEach(async () => {
23+
await sandbox.reset();
24+
});
25+
26+
afterAll(async () => {
27+
await sandbox.cleanup();
28+
});
29+
30+
it.each([
31+
{ async: true, webpack: '^4.0.0', typescript: '2.7.1', tsloader: '^5.0.0' },
32+
{ async: false, webpack: '^4.0.0', typescript: '~3.0.0', tsloader: '^6.0.0' },
33+
{ async: true, webpack: '^4.0.0', typescript: '~3.6.0', tsloader: '^7.0.0' },
34+
{ async: false, webpack: '^4.0.0', typescript: '~3.8.0', tsloader: '^6.0.0' },
35+
])('reports semantic error for %p', async ({ async, webpack, typescript, tsloader }) => {
36+
await sandbox.load(
37+
await readFixture(join(__dirname, 'fixtures/typescript-pnp.fixture'), {
38+
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION: JSON.stringify(
39+
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION
40+
),
41+
TS_LOADER_VERSION: JSON.stringify(tsloader),
42+
TYPESCRIPT_VERSION: JSON.stringify(typescript),
43+
WEBPACK_VERSION: JSON.stringify(webpack),
44+
WEBPACK_CLI_VERSION: JSON.stringify(WEBPACK_CLI_VERSION),
45+
WEBPACK_DEV_SERVER_VERSION: JSON.stringify(WEBPACK_DEV_SERVER_VERSION),
46+
ASYNC: JSON.stringify(async),
47+
}),
48+
yarnInstaller
49+
);
50+
51+
const driver = createWebpackDevServerDriver(sandbox.spawn('yarn webpack-dev-server'), async);
52+
let errors: string[];
53+
54+
// first compilation is successful
55+
await driver.waitForNoErrors();
56+
57+
// then we introduce semantic error by removing "firstName" and "lastName" from the User model
58+
await sandbox.patch(
59+
'src/model/User.ts',
60+
[' firstName?: string;', ' lastName?: string;'].join('\n'),
61+
''
62+
);
63+
64+
// we should receive 2 semantic errors
65+
errors = await driver.waitForErrors();
66+
expect(errors).toEqual([
67+
[
68+
'ERROR in src/model/User.ts 11:16-25',
69+
"TS2339: Property 'firstName' does not exist on type 'User'.",
70+
' 9 | ',
71+
' 10 | function getUserName(user: User): string {',
72+
' > 11 | return [user.firstName, user.lastName]',
73+
' | ^^^^^^^^^',
74+
' 12 | .filter(name => name !== undefined)',
75+
" 13 | .join(' ');",
76+
' 14 | }',
77+
].join('\n'),
78+
[
79+
'ERROR in src/model/User.ts 11:32-40',
80+
"TS2339: Property 'lastName' does not exist on type 'User'.",
81+
' 9 | ',
82+
' 10 | function getUserName(user: User): string {',
83+
' > 11 | return [user.firstName, user.lastName]',
84+
' | ^^^^^^^^',
85+
' 12 | .filter(name => name !== undefined)',
86+
" 13 | .join(' ');",
87+
' 14 | }',
88+
].join('\n'),
89+
]);
90+
91+
// fix the semantic error
92+
await sandbox.patch(
93+
'src/model/User.ts',
94+
[
95+
' return [user.firstName, user.lastName]',
96+
' .filter(name => name !== undefined)',
97+
" .join(' ');",
98+
].join('\n'),
99+
` return user.email;`
100+
);
101+
102+
await driver.waitForNoErrors();
103+
104+
// delete module to trigger another error
105+
await sandbox.remove('src/authenticate.ts');
106+
107+
errors = await driver.waitForErrors();
108+
expect(errors).toEqual([
109+
[
110+
'ERROR in src/index.ts 1:23-39',
111+
"TS2307: Cannot find module './authenticate'.",
112+
" > 1 | import { login } from './authenticate';",
113+
' | ^^^^^^^^^^^^^^^^',
114+
" 2 | import { getUserName } from './model/User';",
115+
' 3 | ',
116+
" 4 | const emailInput = document.getElementById('email');",
117+
].join('\n'),
118+
]);
119+
120+
// re-create deleted module
121+
await sandbox.write(
122+
'src/authenticate.ts',
123+
[
124+
"import { User } from './model/User';",
125+
'',
126+
'async function login(email: string, password: string): Promise<void> {',
127+
' await fetch(',
128+
" '/login',",
129+
' {',
130+
" method: 'POST',",
131+
' body: JSON.stringify({ email, password })',
132+
' }',
133+
' );',
134+
'}',
135+
'',
136+
'async function logout(): Promise<any> {',
137+
' const response = await fetch(',
138+
" '/logout',",
139+
' {',
140+
" method: 'POST'",
141+
' }',
142+
' );',
143+
' return response.json();',
144+
'}',
145+
'',
146+
'export { login, logout };',
147+
].join('\n')
148+
);
149+
150+
// we should receive again 3 semantic errors
151+
errors = await driver.waitForErrors();
152+
expect(errors).toEqual([
153+
[
154+
'ERROR in src/index.ts 34:12-16',
155+
"TS2339: Property 'role' does not exist on type 'void'.",
156+
' 32 | const user = await login(email, password);',
157+
' 33 | ',
158+
" > 34 | if (user.role === 'admin') {",
159+
' | ^^^^',
160+
' 35 | console.log(`Logged in as ${getUserName(user)} [admin].`);',
161+
' 36 | } else {',
162+
' 37 | console.log(`Logged in as ${getUserName(user)}`);',
163+
].join('\n'),
164+
[
165+
'ERROR in src/index.ts 35:45-49',
166+
"TS2345: Argument of type 'void' is not assignable to parameter of type 'User'.",
167+
' 33 | ',
168+
" 34 | if (user.role === 'admin') {",
169+
' > 35 | console.log(`Logged in as ${getUserName(user)} [admin].`);',
170+
' | ^^^^',
171+
' 36 | } else {',
172+
' 37 | console.log(`Logged in as ${getUserName(user)}`);',
173+
' 38 | }',
174+
].join('\n'),
175+
[
176+
'ERROR in src/index.ts 37:45-49',
177+
"TS2345: Argument of type 'void' is not assignable to parameter of type 'User'.",
178+
' 35 | console.log(`Logged in as ${getUserName(user)} [admin].`);',
179+
' 36 | } else {',
180+
' > 37 | console.log(`Logged in as ${getUserName(user)}`);',
181+
' | ^^^^',
182+
' 38 | }',
183+
' 39 | });',
184+
' 40 | ',
185+
].join('\n'),
186+
]);
187+
});
188+
});

test/e2e/TypeScriptSolutionBuilderApi.spec.ts

Lines changed: 82 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,100 +8,98 @@ import {
88
} from './sandbox/WebpackDevServerDriver';
99

1010
describe('TypeScript SolutionBuilder API', () => {
11-
describe('semantic error', () => {
12-
let sandbox: Sandbox;
11+
let sandbox: Sandbox;
1312

14-
beforeAll(async () => {
15-
sandbox = await createSandbox();
16-
});
13+
beforeAll(async () => {
14+
sandbox = await createSandbox();
15+
});
1716

18-
beforeEach(async () => {
19-
await sandbox.reset();
20-
});
17+
beforeEach(async () => {
18+
await sandbox.reset();
19+
});
2120

22-
afterAll(async () => {
23-
await sandbox.cleanup();
24-
});
21+
afterAll(async () => {
22+
await sandbox.cleanup();
23+
});
2524

26-
it.each([{ async: false }, { async: true }])(
27-
'reports semantic error for %p',
28-
async ({ async }) => {
29-
await sandbox.load(
30-
await readFixture(join(__dirname, 'fixtures/typescript-project-references.fixture'), {
31-
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION: JSON.stringify(
32-
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION
33-
),
34-
TS_LOADER_VERSION: JSON.stringify('^7.0.1'),
35-
TYPESCRIPT_VERSION: JSON.stringify('~3.8.0'),
36-
WEBPACK_VERSION: JSON.stringify('^4.0.0'),
37-
WEBPACK_CLI_VERSION: JSON.stringify(WEBPACK_CLI_VERSION),
38-
WEBPACK_DEV_SERVER_VERSION: JSON.stringify(WEBPACK_DEV_SERVER_VERSION),
39-
ASYNC: JSON.stringify(async),
40-
})
41-
);
25+
it.each([{ async: false }, { async: true }])(
26+
'reports semantic error for %p',
27+
async ({ async }) => {
28+
await sandbox.load(
29+
await readFixture(join(__dirname, 'fixtures/typescript-project-references.fixture'), {
30+
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION: JSON.stringify(
31+
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION
32+
),
33+
TS_LOADER_VERSION: JSON.stringify('^7.0.1'),
34+
TYPESCRIPT_VERSION: JSON.stringify('~3.8.0'),
35+
WEBPACK_VERSION: JSON.stringify('^4.0.0'),
36+
WEBPACK_CLI_VERSION: JSON.stringify(WEBPACK_CLI_VERSION),
37+
WEBPACK_DEV_SERVER_VERSION: JSON.stringify(WEBPACK_DEV_SERVER_VERSION),
38+
ASYNC: JSON.stringify(async),
39+
})
40+
);
4241

43-
const driver = createWebpackDevServerDriver(
44-
sandbox.spawn('./node_modules/.bin/webpack-dev-server'),
45-
async
46-
);
47-
let errors: string[];
42+
const driver = createWebpackDevServerDriver(
43+
sandbox.spawn('./node_modules/.bin/webpack-dev-server'),
44+
async
45+
);
46+
let errors: string[];
4847

49-
// initial compilation should be successful
50-
await driver.waitForNoErrors();
48+
// initial compilation should be successful
49+
await driver.waitForNoErrors();
5150

52-
// create semantic error in shared package
53-
await sandbox.patch('packages/shared/src/intersect.ts', 'arrayB: T[] = []', 'arrayB: T');
51+
// create semantic error in shared package
52+
await sandbox.patch('packages/shared/src/intersect.ts', 'arrayB: T[] = []', 'arrayB: T');
5453

55-
// this compilation should contain semantic error in the shared project
56-
// (there is also an error in the client project but as its dependency is not built, it will not be processed)
57-
errors = await driver.waitForErrors();
58-
expect(errors).toEqual([
59-
[
60-
'ERROR in packages/shared/src/intersect.ts 2:41-49',
61-
"TS2339: Property 'includes' does not exist on type 'T'.",
62-
' 1 | function intersect<T>(arrayA: T[] = [], arrayB: T): T[] {',
63-
' > 2 | return arrayA.filter((item) => arrayB.includes(item));',
64-
' | ^^^^^^^^',
65-
' 3 | }',
66-
' 4 | ',
67-
' 5 | export default intersect;',
68-
].join('\n'),
69-
]);
54+
// this compilation should contain semantic error in the shared project
55+
// (there is also an error in the client project but as its dependency is not built, it will not be processed)
56+
errors = await driver.waitForErrors();
57+
expect(errors).toEqual([
58+
[
59+
'ERROR in packages/shared/src/intersect.ts 2:41-49',
60+
"TS2339: Property 'includes' does not exist on type 'T'.",
61+
' 1 | function intersect<T>(arrayA: T[] = [], arrayB: T): T[] {',
62+
' > 2 | return arrayA.filter((item) => arrayB.includes(item));',
63+
' | ^^^^^^^^',
64+
' 3 | }',
65+
' 4 | ',
66+
' 5 | export default intersect;',
67+
].join('\n'),
68+
]);
7069

71-
// fix semantic error in the shared package
72-
await sandbox.patch(
73-
'packages/shared/src/intersect.ts',
74-
'return arrayA.filter((item) => arrayB.includes(item));',
75-
'return arrayA.filter((item) => item && arrayB);'
76-
);
70+
// fix semantic error in the shared package
71+
await sandbox.patch(
72+
'packages/shared/src/intersect.ts',
73+
'return arrayA.filter((item) => arrayB.includes(item));',
74+
'return arrayA.filter((item) => item && arrayB);'
75+
);
7776

78-
// this compilation should contain semantic error in the client project
79-
errors = await driver.waitForErrors();
80-
expect(errors).toEqual([
81-
[
82-
'ERROR in packages/client/src/index.ts 4:42-48',
83-
"TS2345: Argument of type 'T[]' is not assignable to parameter of type 'T'.",
84-
" 'T[]' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.",
85-
' 2 | ',
86-
' 3 | function compute<T>(arrayA: T[], arrayB: T[]) {',
87-
' > 4 | const intersection = intersect(arrayA, arrayB);',
88-
' | ^^^^^^',
89-
' 5 | const difference = subtract(arrayA, arrayB);',
90-
' 6 | ',
91-
' 7 | return {',
92-
].join('\n'),
93-
]);
77+
// this compilation should contain semantic error in the client project
78+
errors = await driver.waitForErrors();
79+
expect(errors).toEqual([
80+
[
81+
'ERROR in packages/client/src/index.ts 4:42-48',
82+
"TS2345: Argument of type 'T[]' is not assignable to parameter of type 'T'.",
83+
" 'T[]' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.",
84+
' 2 | ',
85+
' 3 | function compute<T>(arrayA: T[], arrayB: T[]) {',
86+
' > 4 | const intersection = intersect(arrayA, arrayB);',
87+
' | ^^^^^^',
88+
' 5 | const difference = subtract(arrayA, arrayB);',
89+
' 6 | ',
90+
' 7 | return {',
91+
].join('\n'),
92+
]);
9493

95-
// fix semantic error in the client package
96-
await sandbox.patch(
97-
'packages/client/src/index.ts',
98-
'const intersection = intersect(arrayA, arrayB);',
99-
'const intersection = intersect(arrayA, arrayB[0]);'
100-
);
94+
// fix semantic error in the client package
95+
await sandbox.patch(
96+
'packages/client/src/index.ts',
97+
'const intersection = intersect(arrayA, arrayB);',
98+
'const intersection = intersect(arrayA, arrayB[0]);'
99+
);
101100

102-
// this compilation should be successful
103-
await driver.waitForNoErrors();
104-
}
105-
);
106-
});
101+
// this compilation should be successful
102+
await driver.waitForNoErrors();
103+
}
104+
);
107105
});

0 commit comments

Comments
 (0)