angular-cli: Tests don't run in Angular 16 using Jasmine/Karma

Command

test

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

15

Description

The project compiles well, and the tests worked in version 15 of Angular. After running ng test I am receiving the following error:

ng-virtual-main.js!=!data:text/javascript;base64 ...Unknown string like token - Error: Module build failed (from ./node_modules/@ngtools/webpack/src/ivy/index.js): ...Unknown string like token s missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.

Minimal Reproduction

After running ng test I am receiving the following error:

ng-virtual-main.js!=!data:text/javascript;base64 ...Unknown string like token - Error: Module build failed (from ./node_modules/@ngtools/webpack/src/ivy/index.js): ...Unknown string like token s missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.

Exception or Error

`ng-virtual-main.js!=!data:text/javascript;base64 ...Unkown string like token - Error: Module build failed (from ./node_modules/@ngtools/webpack/src/ivy/index.js):  ...Unkown string like token s missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.`

Your Environment

npm ls @ngtools/webpack

`└─┬ @angular-devkit/build-angular@16.2.4
  └── @ngtools/webpack@16.2.4`

ng version 

`Angular CLI: 16.2.4
Node: 18.15.0
Package Manager: npm 9.5.0
OS: linux x64

Angular: 16.2.7
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                              Version
--------------------------------------------------------------
@angular-devkit/architect            0.1602.4
@angular-devkit/build-angular        16.2.4
@angular-devkit/core                 16.2.4
@angular-devkit/schematics           16.2.4
@angular/cdk                         16.2.6
@angular/cli                         16.2.4
@angular/material                    16.2.6
@angular/material-date-fns-adapter   16.2.6
@schematics/angular                  16.2.4
rxjs                                 7.5.7
typescript                           4.9.3
zone.js                              0.13.3
`

Anything else relevant?

No response

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 2
  • Comments: 15

Commits related to this issue

Most upvoted comments

This seems to be caused by the allowJs tsconfig option.

It is necessary to remove the allowJs option until a fix is provided.

Same problem with Angular 18.

A found a simple workaround.

If you look at https://github.com/angular/angular-cli/blob/073e81ecfbde768c1c4e9fa243bf4a0af08eaf8d/packages/angular_devkit/build_angular/src/builders/karma/browser_builder.ts#L47 you can see that the missing Javascript is added only if !options.main.

Solution is to decode javascript:

data:text/javascript;base64,CiAgaW1wb3J0IHsgZ2V0VGVzdEJlZCB9IGZyb20gJ0Bhbmd1bGFyL2NvcmUvdGVzdGluZyc7CiAgaW1wb3J0IHsKICAgIEJyb3dzZXJEeW5hbWljVGVzdGluZ01vZHVsZSwKICAgIHBsYXRmb3JtQnJvd3NlckR5bmFtaWNUZXN0aW5nLAogICB9IGZyb20gJ0Bhbmd1bGFyL3BsYXRmb3JtLWJyb3dzZXItZHluYW1pYy90ZXN0aW5nJzsKCiAgLy8gSW5pdGlhbGl6ZSB0aGUgQW5ndWxhciB0ZXN0aW5nIGVudmlyb25tZW50LgogIGdldFRlc3RCZWQoKS5pbml0VGVzdEVudmlyb25tZW50KEJyb3dzZXJEeW5hbWljVGVzdGluZ01vZHVsZSwgcGxhdGZvcm1Ccm93c2VyRHluYW1pY1Rlc3RpbmcoKSwgewogICAgZXJyb3JPblVua25vd25FbGVtZW50czogdHJ1ZSwKICAgIGVycm9yT25Vbmtub3duUHJvcGVydGllczogdHJ1ZQogIH0pOwo=

You will have

  import { getTestBed } from '@angular/core/testing';
  import {
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting,
   } from '@angular/platform-browser-dynamic/testing';

  // Initialize the Angular testing environment.
  getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), {
    errorOnUnknownElements: true,
    errorOnUnknownProperties: true
  });

In src folder, add a test.ts file with the previous content.

in tsconfig.spec.json, add

  "files": [
    "src/test.ts"
  ],

In angular.json file, in field architect > test > options, add:

            "main": "src/test.ts",

The problem should disappear.