angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Tests don't run in Angular 16 using Jasmine/Karma

Open apangr opened this issue 2 years ago • 14 comments

Command

test

Is this a regression?

  • [X] 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/[email protected]
  └── @ngtools/[email protected]`

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

apangr avatar Oct 02 '23 23:10 apangr

This seems like a bug but we'll need to look at a reproduction to find and fix the problem. Can you setup a minimal repro please?

You can read here why this is needed. A good way to make a minimal repro is to create a new app via ng new repro-app and adding the minimum possible code to show the problem. Then you can push this repository to github and link it here.

This might be related to your directory structure so its really important to get an accurate repro to diagnose this.

alan-agius4 avatar Oct 03 '23 05:10 alan-agius4

I haven't managed to reproduce the error in another project yet, but when I decoded the 'unknown string' I received something like this in case it helps:

  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
  });

apangr avatar Oct 03 '23 14:10 apangr

Hello @alan-agius4 here is the small app where you can get the error when running ng test . I hope it is of great help. https://github.com/apangr/repro-app

apangr avatar Oct 06 '23 16:10 apangr

This seems to be caused by the allowJs tsconfig option.

alan-agius4 avatar Oct 09 '23 11:10 alan-agius4

well, in my project I have some .js files and I will have them until I have time to create an npm package, if I exclude them from the test can it work? or is it necessary to remove the allowJs option and files?

apangr avatar Oct 09 '23 12:10 apangr

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

alan-agius4 avatar Oct 09 '23 12:10 alan-agius4

ok thanks for your help

apangr avatar Oct 09 '23 13:10 apangr

I have also come across this issue. Will it be fixed anytime soon?

mtzea avatar Nov 19 '23 12:11 mtzea

+1

Kaspis77 avatar Nov 22 '23 21:11 Kaspis77

Same issue....

chaukhaphan avatar Feb 15 '24 23:02 chaukhaphan

FYI, changing allowJs from true to false fixed it for me. Fortunately, I didn't have any JS files and don't remember why it was there to begin with.

walkingriver avatar Feb 22 '24 22:02 walkingriver

Luckily I already migrated away from Karma several years ago to Jest. Which works much better now and also works well with Angular. Angular also has a builder for that: @angular-builders/jest:run.

melroy89 avatar Mar 09 '24 02:03 melroy89