Skip to content

Karma tests with non-standard suffix no longer run after migration to ng 15 #24471

Closed

Description

@johncrim

Which @angular/* package(s) are the source of the bug?

@angular/cli I believe

Is this a regression?

Yes

Description

Our angular projects use both jest tests and karma tests. To distinguish them while running, we use *.spec.ts for jest tests, and *.karma-spec.ts for karma tests. This was fine up until we upgraded to angular 15 - now there's no longer an obvious way to only load and run files with the *.karma-spec.ts suffix.

Previously we used code like the following in our test.ts file:

declare const require: {
  context(path: string, deep?: boolean, filter?: RegExp): {
    keys(): string[];
    <T>(id: string): T;
  };
};

...
// Find all the tests.
const context = require.context('../../', true, /\.karma-spec\.ts$/);
// And load the modules.
context.keys().map(context);

This code was removed by the migration, but I don't see a documented config option for the suffix. Any suggestions?

Please provide a link to a minimal reproduction of the bug

[email protected]:johncrim/ng-jest-starter.git

Please provide the exception or error you saw

To reproduce:

git clone [email protected]:johncrim/ng-jest-starter.git
yarn # Installs ng 14 dependencies
yarn test # Runs jest tests
yarn karma:headless # Runs karma tests

At this point, things work as expected. Then update to ng 15*, and try running the tests:

yarn ng update @angular/core@15 @angular/cli@15
yarn test # Still works
yarn karma:headless # Fails with error below

...
./libs/ui-lib/src/lib/ui-lib.component.spec.ts - Error: Module build failed (from ./node_modules/@ngtools/webpack/src/ivy/index.js):
Error: ng-jest-starter\libs\ui-lib\src\lib\ui-lib.component.spec.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
...

*Note that the ng update fails on the Update TypeScript compiler 'target' and set 'useDefineForClassFields'. migration. This is irrelevant, but you can add "useDefineForClassFields": false to the tsconfig.base.json file.

Please provide the environment you discovered this bug in (run ng version)

> yarn ng version
Node.js version v19.3.0 detected.
Odd numbered Node.js versions will not enter LTS status and should not be used for production. For more information, please see https://nodejs.org/en/about/releases/.

Angular CLI: 15.0.4
Node: 19.3.0 (Unsupported)
Package Manager: yarn 3.2.4
OS: win32 x64

Angular: 15.0.4
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1500.4
@angular-devkit/build-angular   15.0.4
@angular-devkit/core            15.0.4
@angular-devkit/schematics      15.0.4
@angular/cdk                    15.0.3
@schematics/angular             15.0.4
ng-packagr                      15.0.3
rxjs                            7.5.7
typescript                      4.8.4
webpack                         5.74.0

Anything else?

No response

Activity

  1. transferred this issue fromangular/angularon Dec 27, 2022
  2. clydin commented on Dec 27, 2022

    @clydin
    Member

    The include option is now used to determine the set of tests to include and execute. By default this option uses ["**/*.spec.ts"] but can be changed to reflect the specific requirements of a project. In this case, changing it to ["**/*.karma-spec.ts"] should provide the expected behavior.
    If you are still encountering issues after adjusting the include option, please let us know.

  3. johncrim commented on Jan 3, 2023

    @johncrim
    Author

    That solves it - thank you for the help @clydin .

  4. angular-automatic-lock-bot commented on Feb 3, 2023

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  5. locked and limited conversation to collaborators on Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions