Skip to content

Commit 7e35e29

Browse files
alan-agius4clydin
authored andcommitted
test: account for module resolution differences
In some cases due to module resolution '@ngtools' might already been under `@angular-devkit/build-angular`.
1 parent 2925f06 commit 7e35e29

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
11
import { createDir, moveFile } from '../../utils/fs';
22
import { ng } from '../../utils/process';
3+
import { assertIsError } from '../../utils/utils';
34

45
export default async function () {
56
await createDir('node_modules/@angular-devkit/build-angular/node_modules');
6-
await moveFile(
7-
'node_modules/@ngtools',
8-
'node_modules/@angular-devkit/build-angular/node_modules/@ngtools',
9-
);
7+
let originalInRootNodeModules = true;
8+
9+
try {
10+
await moveFile(
11+
'node_modules/@ngtools',
12+
'node_modules/@angular-devkit/build-angular/node_modules/@ngtools',
13+
);
14+
} catch (e) {
15+
assertIsError(e);
16+
17+
if (e.code !== 'ENOENT') {
18+
throw e;
19+
}
20+
21+
// In some cases due to module resolution '@ngtools' might already been under `@angular-devkit/build-angular`.
22+
originalInRootNodeModules = false;
23+
await moveFile(
24+
'node_modules/@angular-devkit/build-angular/node_modules/@ngtools',
25+
'node_modules/@ngtools',
26+
);
27+
}
1028

1129
await ng('build', '--configuration=development');
1230

1331
// Move it back.
14-
await moveFile(
15-
'node_modules/@angular-devkit/build-angular/node_modules/@ngtools',
16-
'node_modules/@ngtools',
17-
);
32+
await moveBack(originalInRootNodeModules);
33+
}
34+
35+
function moveBack(originalInRootNodeModules: Boolean): Promise<void> {
36+
return originalInRootNodeModules
37+
? moveFile(
38+
'node_modules/@angular-devkit/build-angular/node_modules/@ngtools',
39+
'node_modules/@ngtools',
40+
)
41+
: moveFile(
42+
'node_modules/@ngtools',
43+
'node_modules/@angular-devkit/build-angular/node_modules/@ngtools',
44+
);
1845
}

tests/legacy-cli/e2e/utils/utils.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'assert';
12
import { mkdtemp, realpath, rm } from 'fs/promises';
23
import { tmpdir } from 'os';
34
import path from 'path';
@@ -41,3 +42,11 @@ export async function mockHome(cb: (home: string) => Promise<void>): Promise<voi
4142
await rm(tempHome, { recursive: true, force: true });
4243
}
4344
}
45+
46+
export function assertIsError(value: unknown): asserts value is Error & { code?: string } {
47+
const isError =
48+
value instanceof Error ||
49+
// The following is needing to identify errors coming from RxJs.
50+
(typeof value === 'object' && value && 'name' in value && 'message' in value);
51+
assert(isError, 'catch clause variable is not an Error instance');
52+
}

0 commit comments

Comments
 (0)