From 7b52b98bfdededa86633d673866350723660d681 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Wed, 15 May 2024 15:10:21 +0000 Subject: [PATCH] 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: https://github.com/jestjs/jest/issues/2549 Closes: #27643 --- packages/angular_devkit/schematics/src/rules/template.ts | 7 ++++++- packages/angular_devkit/schematics/src/tree/host-tree.ts | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/angular_devkit/schematics/src/rules/template.ts b/packages/angular_devkit/schematics/src/rules/template.ts index c2105477dcfa..617c5539e026 100644 --- a/packages/angular_devkit/schematics/src/rules/template.ts +++ b/packages/angular_devkit/schematics/src/rules/template.ts @@ -62,7 +62,12 @@ export function applyContentTemplate(options: T): FileOperator { content: Buffer.from(templateImpl(decodedContent, {})(options)), }; } catch (e) { - if (e instanceof TypeError) { + // The second part should not be needed. But Jest does not support instanceof correctly. + // See: https://github.com/jestjs/jest/issues/2549 + if ( + e instanceof TypeError || + (e as NodeJS.ErrnoException).code === 'ERR_ENCODING_INVALID_ENCODED_DATA' + ) { return entry; } diff --git a/packages/angular_devkit/schematics/src/tree/host-tree.ts b/packages/angular_devkit/schematics/src/tree/host-tree.ts index c37d9d11027d..8c4b012d401d 100644 --- a/packages/angular_devkit/schematics/src/tree/host-tree.ts +++ b/packages/angular_devkit/schematics/src/tree/host-tree.ts @@ -304,7 +304,12 @@ export class HostTree implements Tree { // With the `fatal` option enabled, invalid data will throw a TypeError return decoder.decode(data); } catch (e) { - if (e instanceof TypeError) { + // The second part should not be needed. But Jest does not support instanceof correctly. + // See: https://github.com/jestjs/jest/issues/2549 + if ( + e instanceof TypeError || + (e as NodeJS.ErrnoException).code === 'ERR_ENCODING_INVALID_ENCODED_DATA' + ) { throw new Error(`Failed to decode "${path}" as UTF-8 text.`); } throw e;