Skip to content

Commit 4b6ba8d

Browse files
committedMay 15, 2024·
fix(@angular-devkit/schematics): SchematicTestRunner.runExternalSchematic fails with "The encoded data was not valid for encoding utf-8"
When using Jest instanceof does not work correctly. See: jestjs/jest#2549 Closes: #27643 (cherry picked from commit 7b52b98)
1 parent 3ada6eb commit 4b6ba8d

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed
 

‎packages/angular_devkit/schematics/src/rules/template.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ export function applyContentTemplate<T>(options: T): FileOperator {
6262
content: Buffer.from(templateImpl(decodedContent, {})(options)),
6363
};
6464
} catch (e) {
65-
if (e instanceof TypeError) {
65+
// The second part should not be needed. But Jest does not support instanceof correctly.
66+
// See: https://github.com/jestjs/jest/issues/2549
67+
if (
68+
e instanceof TypeError ||
69+
(e as NodeJS.ErrnoException).code === 'ERR_ENCODING_INVALID_ENCODED_DATA'
70+
) {
6671
return entry;
6772
}
6873

‎packages/angular_devkit/schematics/src/tree/host-tree.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,12 @@ export class HostTree implements Tree {
304304
// With the `fatal` option enabled, invalid data will throw a TypeError
305305
return decoder.decode(data);
306306
} catch (e) {
307-
if (e instanceof TypeError) {
307+
// The second part should not be needed. But Jest does not support instanceof correctly.
308+
// See: https://github.com/jestjs/jest/issues/2549
309+
if (
310+
e instanceof TypeError ||
311+
(e as NodeJS.ErrnoException).code === 'ERR_ENCODING_INVALID_ENCODED_DATA'
312+
) {
308313
throw new Error(`Failed to decode "${path}" as UTF-8 text.`);
309314
}
310315
throw e;

0 commit comments

Comments
 (0)
Please sign in to comment.