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;