Skip to content

Commit

Permalink
fix(@angular-devkit/schematics): `SchematicTestRunner.runExternalSche…
Browse files Browse the repository at this point in the history
…matic` 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
  • Loading branch information
alan-agius4 committed May 15, 2024
1 parent bc47fa1 commit 7b52b98
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/angular_devkit/schematics/src/rules/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ export function applyContentTemplate<T>(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;
}

Expand Down
7 changes: 6 additions & 1 deletion packages/angular_devkit/schematics/src/tree/host-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7b52b98

Please sign in to comment.