angularjestjsnrwl-nxvmware-clarity

Problem importing Clarity components in Jest Unit Tests


When working with Angular in an NX workspace and writing unit tests with Jest I can not import Clarity components without braking the tests.

I am using an NX workspace with Angular preset and install @cds packages.

npx create-nx-workspace@latest
npm i @cds/{core,angular} --legacy-peer-deps

Then, if a clarity component is part of a Jest unit test, for example:

import { TestBed } from '@angular/core/testing';
import { CdsButtonModule } from '@cds/angular';                           // <---
import { AppComponent } from './app.component';
import { NxWelcomeComponent } from './nx-welcome.component';

describe('AppComponent', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [CdsButtonModule],                                         // <---
      declarations: [AppComponent, NxWelcomeComponent],
    }).compileComponents();
  });
});

It is throwing:

Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
 • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation

/PATHTO/clarity-jest-test/node_modules/@cds/core/accordion/register.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import{registerElementSafely as o,ClarityMotion as r,AnimationAccordionPanelOpenName as c,AnimationAccordionPanelOpenConfig as e}from"@cds/core/internal";import{CdsAccordion as n}from"./accordion.element.js";import{CdsAccordionPanel as d}from"./accordion-panel.element.js";import{CdsAccordionContent as t}from"./accordion-content.element.js";import{CdsAccordionHeader as i}from"./accordion-header.element.js";import"@cds/core/icon/register.js";import"@cds/core/button-expand/register.js";o("cds-accordion",n),o("cds-accordion-panel",d),o("cds-accordion-content",t),o("cds-accordion-header",i),r.add(c,e);
                                                                                  ^^^^^^


SyntaxError: Cannot use import statement outside a module

  at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1796:14)

Here is a demo repository for reproduction https://github.com/ngfelixl/clarity-jest-test and here is a Github issue describing the problem as well https://github.com/vmware-clarity/core/issues/194.

Any help is very much appreciated, thank you! I've already tried to solve the issue by using the following ressources.


Solution

  • We solved the issue. It works with Jest 28. In the demo repository we've used Jest 28, however in our real repo we've used Jest 27. So the combination of

    works. Still one remark: The transformIgnorePattern might be wrong as the "or" includes any mjs files (see |.*).