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
Example of the test file that is failing on the new application:
import { TextEncoder, TextDecoder } from 'util'
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');
});
});
package.json:
{
"name": "test-jest",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test",
"test:watch": "ng test --watch",
"serve:ssr:test-jest": "node dist/test-jest/server/server.mjs"
},
"private": true,
"dependencies": {
"@angular/animations": "^17.3.0",
"@angular/common": "^17.3.0",
"@angular/compiler": "^17.3.0",
"@angular/core": "^17.3.0",
"@angular/forms": "^17.3.0",
"@angular/platform-browser": "^17.3.0",
"@angular/platform-browser-dynamic": "^17.3.0",
"@angular/platform-server": "^17.3.0",
"@angular/router": "^17.3.0",
"@angular/ssr": "^17.3.3",
"express": "^4.18.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
},
"devDependencies": {
"@angular-builders/jest": "^17.0.3",
"@angular-devkit/build-angular": "^17.3.3",
"@angular/cli": "^17.3.3",
"@angular/compiler-cli": "^17.3.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.12",
"@types/node": "^18.19.33",
"jasmine-core": "~5.1.0",
"jest": "^29.7.0",
"typescript": "~5.4.2"
}
}
What I have tried is testing on different applications, still getting the same result. Created a new application to be extra sure it wasn't config related.
Expecting all unit tests to pass like they did on the earlier versions of Angular. Or for there to be a way to fix broken tests.
This has been fixed in the recent Angular release, in this issue: