From 4abb18b140f6c43f281228906cb09b48a4ee29ba Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Wed, 15 May 2024 15:07:05 +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/tree/host-tree.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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;