Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest tests using @angular-devkit/schematics are throwing errors for the encoded data was not valid for encoding utf-8 #1781

Open
cathey191 opened this issue May 22, 2024 · 1 comment

Comments

@cathey191
Copy link

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');
  });
});
@just-jeb
Copy link
Owner

I wonder if that's an issue with the builder or jest-preset-angular.
Could you try to reproduce it with clean jest-preset-angular setup (without the @angular-builders) in order to confirm it is indeed an issue with the builder?

Regardless, a minimal reproduction repository would speed up the investigation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants