SAP / ui5-webcomponents-ngx

UI5 Web Components for Angular provides directives for each UI5 Web Component. The directives allow to easily build your Angular application following the SAP Design System.
https://ui5-webcomponents-ngx.netlify.app/
Apache License 2.0
27 stars 5 forks source link

Jest tests fail: Cannot use import statement outside a module #118

Open ThePuscher opened 8 months ago

ThePuscher commented 8 months ago

Bug Description

I am working with an angular project that uses jest for unit tests instead of karma. When trying to run the tests, the following error occurs:

Details:

    [...]/node_modules/@ui5/webcomponents/dist/Toast.js:7
    import Integer from "@ui5/webcomponents-base/dist/types/Integer.js";
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      10 | import { ActivatedRoute, Router } from '@angular/router';
    > 12 | import { ToastComponent } from '@ui5/webcomponents-ngx/main/toast';
         | ^
      13 | import { CardComponent } from '@ui5/webcomponents-ngx/main/card';
      14 | import { PopoverComponent } from '@ui5/webcomponents-ngx/main/popover';
      15 | import {

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
      at Object.<anonymous> (node_modules/@ui5/webcomponents-ngx/fesm2022/ui5-webcomponents-ngx-main-toast.mjs:5:8)
      at Object.<anonymous> (src/app/x/x.component.ts:12:1)
      at Object.<anonymous> (src/app/x/x.component.spec.ts:3:1)

It seems like jest would expect commonJS syntax here.

Steps to Reproduce

Run jest on an angular component that imports a component from webcomponents-ngx in the context given below.

Context

jest.config.ts:

import type { Config } from 'jest';

const config: Config = {
  clearMocks: true,
  collectCoverage: true,
  coverageDirectory: './coverage',
  moduleDirectories: ['node_modules', '<rootDir>'],
  preset: 'jest-preset-angular',
  rootDir: './',
  roots: ['<rootDir>'],
  setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
  testEnvironment: 'jsdom',
};

export default config;

setup-jest.ts:

import 'jest-preset-angular/setup-jest';
import 'jest-canvas-mock';

From tsconfig.json:

"compilerOptions": {
    "target": "ES2022",
    "module": "ES2022",
    "lib": ["ES2022", "dom"],
    // ...
}

Priority

Stakeholder Info (if applicable)

github-actions[bot] commented 8 months ago

Hello @ThePuscher, thank you for using ui5-webcomponents-ngx! The ui5-webcomponents-ngx team will triage your issue as soon as possible.