angular-builders
angular-builders copied to clipboard
Jest tests using @angular-devkit/schematics are throwing errors for the encoded data was not valid for encoding utf-8
Describe the Bug
On three different applications Angular are failing all test that use @angular-devkit/schematics. Two of the applications were passing fine on Angular 17.2.0 but failed after upgrading to 17.3.7. The third application was a new one that was created on 17.3.7 to just for testing purses.
Standard jest tests, eg testing 'Hello world' are passing. But anything using schematics is throwing with the error The encoded data was not valid for encoding utf-8.
Minimal Reproduction
Create a new Angular application on 17.3.7 (latest version). Change to using Jest. Create a unit test that uses any schematics, and it throws with an encoding utf-8 error.
Expected Behavior
Expect test to be able to run against anything with schematics, not to straight away fail.
Environment
Test file example:
global.TextEncoder = TextEncoder
// @ts-expect-error
global.TextDecoder = TextDecoder
import { Tree } from '@angular-devkit/schematics';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import * as path from 'path';
const collectionPath = path.join(__dirname, '../collection.json');
describe('hello-world', () => {
it('works', async () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = await runner.runSchematic('hello-world', {}, Tree.empty());
expect(tree.files).toEqual([]);
});
it('works', async () => {
const workspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
version: '7.0.0',
};
const defaultAppOptions = {
name: 'bar',
inlineStyle: false,
inlineTemplate: false,
viewEncapsulation: 'Emulated',
routing: false,
style: 'css',
skipTests: false
};
const runner = new SchematicTestRunner('schematics', collectionPath);
const workspaceTree = await runner.runExternalSchematic(
'@schematics/angular',
'workspace',
workspaceOptions
);
const appTree = await runner.runExternalSchematic('@schematics/angular', 'application', defaultAppOptions, workspaceTree);
const tree = await runner.runSchematic('hello-world', {}, appTree);
expect(tree.files[0]).toEqual('/README.md');
});
});