Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NX 20.4.x Lit element unit tests config issue with Jest #29834

Open
1 of 4 tasks
sandeepdussa opened this issue Jan 31, 2025 · 2 comments
Open
1 of 4 tasks

NX 20.4.x Lit element unit tests config issue with Jest #29834

sandeepdussa opened this issue Jan 31, 2025 · 2 comments
Assignees
Labels
scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug

Comments

@sandeepdussa
Copy link

Current Behavior

Facing below error when trying to run test cases for Lit Element through Jest in our NX monorepo. This happens even with node_modules transformed.

transformIgnorePatterns: ["node_modules/(?!(lit-html|lit-element|lit|@lit)/)"]

nx run my-lit-element:test

FAIL my-lit-element libs/my-lit-element/src/lib/my-lit-element.spec.ts
● Test suite failed to run

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

Details:

/Users/POC/nx-lit-element-example/node_modules/lit/index.js:2
import "@lit/reactive-element";
^^^^^^

SyntaxError: Cannot use import statement outside a module

> 1 | import { LitElement, css, html } from 'lit';
    | ^
  2 | import { customElement, property } from 'lit/decorators.js';
  3 | @customElement('my-lit-element')
  4 | export class MyLitElement extends LitElement {

  at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1505:14)
  at Object.<anonymous> (src/lib/my-lit-element.ts:1:1)
  at Object.<anonymous> (src/lib/my-lit-element.spec.ts:1:1)

Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 4.135 s
Ran all test suites.

Expected Behavior

Test suite to be passed.

GitHub Repo

No response

Steps to Reproduce

Step 1: Create empty NX workspace and then add @nx/angular, @nx/js schematics.

Step 2: Create host angular app using below generator cmd.

npx nx generate @nx/angular:application --directory=apps/angular-host --bundler=webpack --name=angular-host --routing=false --e2eTestRunner=none --style=scss

Step 3: Create sample js library using below generator cmd.

npx nx generate @nx/js:library --directory=libs/my-lit-element --importPath=@test --linter=eslint --name=my-lit-element --unitTestRunner=jest

Step 4: Run npm i lit --save and add below to respective files from scaffolding.

my-lit-element.ts file content

import { LitElement, css, html } from 'lit';
import { customElement, property } from 'lit/decorators.js';
@CustomElement('my-lit-element')
export class MyLitElement extends LitElement {
public static override readonly styles = css:host { display: block; padding: 16px; };

@Property() name = 'My Lit Element!';

override render() {
return html<p>This is the development zone of ${this.name}!</p>;
}
}

my-lit-element.spec.ts content

import { MyLitElement } from './my-lit-element';
import { html } from 'lit';

describe('MyViewModal ---', () => {
let testComponent: MyLitElement;

beforeEach(() => {
testComponent = new MyLitElement();
});

test('should create component', () => {
const actual = testComponent.render();
expect(actual).toBeDefined();
});

test('renders default message', () => {
const actual = testComponent.render();
expect(typeof actual).toBe(typeof html``);
});
});

Nx Report

Node           : 20.18.2
OS             : darwin-arm64
Native Target  : aarch64-macos
npm            : 10.8.2

nx                     : 20.4.0
@nx/js                 : 20.4.0
@nx/jest               : 20.4.0
@nx/eslint             : 20.4.0
@nx/workspace          : 20.4.0
@nx/angular            : 20.4.0
@nx/devkit             : 20.4.0
@nx/eslint-plugin      : 20.4.0
@nx/module-federation  : 20.4.0
@nx/web                : 20.4.0
@nx/webpack            : 20.4.0
typescript             : 5.7.3
---------------------------------------
Registered Plugins:
@nx/eslint/plugin
---------------------------------------
Community plugins:
angular-eslint : 19.0.2

Failure Logs

/Users/POC/nx-lit-element-example/node_modules/lit/index.js:2
    import "@lit/reactive-element";
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

    > 1 | import { LitElement, css, html } from 'lit';
        | ^
      2 | import { customElement, property } from 'lit/decorators.js';
      3 | @customElement('my-lit-element')
      4 | export class MyLitElement extends LitElement {

      at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1505:14)
      at Object.<anonymous> (src/lib/my-lit-element.ts:1:1)
      at Object.<anonymous> (src/lib/my-lit-element.spec.ts:1:1)

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

Due to security concerns, not able to upload sample repo from my work machine.

@FrozenPandaz FrozenPandaz added the scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx label Jan 31, 2025
@sumegha26
Copy link

sumegha26 commented Feb 4, 2025

+1 Any update on this ?

@sunilrathore24
Copy link

+1 Any update on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug
Projects
None yet
Development

No branches or pull requests

5 participants