analogjs/analog

Can't run tests with newest update (1.9.2)

Closed this issue · 15 comments

Please provide the environment you discovered this bug in.

We use nx with analog vitest

Which area/package is the issue in?

vitest-angular

Description

Everytime I run the tests I get the error:

ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/Users/reneachtelik/workspace/fhp-bike/node_modules/.pnpm/@analogjs+vitest-angular@1.9.2_@analogjs+vite-plugin-angular@1.9.2_@angular-devkit+build-angu_tfhrvuljhjniigrh7pmftb3w5m/node_modules/@analogjs/vitest-angular/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
 ❯ ../../../node_modules/.pnpm/@analogjs+vitest-angular@1.9.2_@analogjs+vite-plugin-angular@1.9.2_@angular-devkit+build-angu_tfhrvuljhjniigrh7pmftb3w5m/node_modules/@analogjs/vitest-angular/setup-zone.js:2:23
      1| "use strict";
      2| Object.defineProperty(exports, "__esModule", { value: true });
       |                       ^
      3| require("zone.js");
      4| require("zone.js/plugins/sync-test");
 ❯ src/test-setup.ts:1:1

project is esm only.

Please provide the exception or error you saw

ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/Users/reneachtelik/workspace/fhp-bike/node_modules/.pnpm/@analogjs+vitest-angular@1.9.2_@analogjs+vite-plugin-angular@1.9.2_@angular-devkit+build-angu_tfhrvuljhjniigrh7pmftb3w5m/node_modules/@analogjs/vitest-angular/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
 ❯ ../../../node_modules/.pnpm/@analogjs+vitest-angular@1.9.2_@analogjs+vite-plugin-angular@1.9.2_@angular-devkit+build-angu_tfhrvuljhjniigrh7pmftb3w5m/node_modules/@analogjs/vitest-angular/setup-zone.js:2:23
      1| "use strict";
      2| Object.defineProperty(exports, "__esModule", { value: true });
       |                       ^
      3| require("zone.js");
      4| require("zone.js/plugins/sync-test");
 ❯ src/test-setup.ts:1:1

Other information

Happens only with the newest update

I would be willing to submit a PR to fix this issue

  • Yes
  • No

Is this Nx 20?

Make sure your vite.config.ts is named vite.config.mts if its just for testing which will force ESM.

Is this Nx 20?

Yes, sorry, thought I've included the NX Report, will do this later

Also all my vite configs are named .mts

I also use

https://github.com/jscutlery/devkit/releases/tag/swc-angular-0.16.2

Tried running without it and this threw an error as well. But something about missing snapshotSerializer. I'm in a hurry and will add more details later.

Again, sorry for the half-assed issue

NX   Report complete - copy this into the issue template

Node           : 22.11.0
OS             : darwin-arm64
Native Target  : aarch64-macos
pnpm           : 9.12.3

nx (global)        : 20.0.10
nx                 : 20.0.10
@nx/js             : 20.0.10
@nx/eslint         : 20.0.10
@nx/workspace      : 20.0.10
@nx/angular        : 20.0.10
@nx/cypress        : 20.0.10
@nx/devkit         : 20.0.10
@nx/esbuild        : 20.0.10
@nx/eslint-plugin  : 20.0.10
@nx/express        : 20.0.10
@nx/node           : 20.0.10
@nx/vite           : 20.0.10
@nx/web            : 20.0.10
typescript         : 5.5.4
---------------------------------------
Registered Plugins:
@nx/eslint/plugin
@nx/cypress/plugin
@nx/vite/plugin
---------------------------------------
Community plugins:
@analogjs/vite-plugin-angular : 1.9.2
@analogjs/vitest-angular      : 1.9.2
@jsverse/transloco            : 7.5.0
@koliveira15/nx-sonarqube     : 4.1.0
@ngrx/component-store         : 18.1.1
@ngrx/effects                 : 18.1.1
@ngrx/entity                  : 18.1.1
@ngrx/eslint-plugin           : 18.1.1
@ngrx/operators               : 18.1.1
@ngrx/router-store            : 18.1.1
@ngrx/schematics              : 18.1.1
@ngrx/store                   : 18.1.1
@ngrx/store-devtools          : 18.1.1
@nx-tools/nx-container        : 6.1.0
ng-mocks                      : 14.13.1
ngx-matomo-client             : 6.3.1
{
  "plugins": [
    {
      "name": "nx-vite-ts-paths",
      "enforce": "pre"
    },
    {
      "name": "swc",
      "vite": {},
      "rollup": {}
    }
  ],
  "cacheDir": "../../../node_modules/.vite/libs/gui-agent",
  "test": {
    "name": "gui-agent",
    },
    "include": [
      "**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"
    ],
    "reporters": [
      "basic"
    ],
    "outputFile": {
      "junit": "../../../test-reports/libs/gui-agent/test-report.xml"
    },
    "typecheck": {
      "enabled": true,
      "ignoreSourceErrors": false,
      "include": [
        "**/*.spec.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"
      ],
      "tsconfig": "/libs/gui/agent/tsconfig.json"
    },
    "setupFiles": [
      "/libs/gui/agent/src/test-setup.ts"
    ],
    "passWithNoTests": true,
    "clearMocks": true,
    "globals": true,
    "coverage": {
      "enabled": true,
      "provider": "v8",
      "thresholds": {
        "statements": 90,
        "branches": 80,
        "functions": 90,
        "lines": 90
      },
      "reportsDirectory": "../../../coverage/libs/gui-agent",
      "exclude": [
        "**/src/lib/*routes*",
        "**/src/lib/*provider*.ts",
        "!**/src/lib/*.provider.ts",
        "**/index.ts",
        "coverage/**",
        "dist/**",
        "**/node_modules/**",
        "**/[.]**",
        "packages/*/test?(s)/**",
        "**/*.d.ts",
        "**/virtual:*",
        "**/__x00__*",
        "**/\u0000*",
        "cypress/**",
        "test?(s)/**",
        "test?(-*).?(c|m)[jt]s?(x)",
        "**/*{.,-}{test,spec,bench,benchmark}?(-d).?(c|m)[jt]s?(x)",
        "**/__tests__/**",
        "**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build,eslint,prettier}.config.*",
        "**/vitest.{workspace,projects}.[jt]s?(on)",
        "**/.{eslint,mocha,prettier}rc.{?(c|m)js,yml}"
      ],
      "all": false,
      "reporter": [
        "text",
        "html"
      ]
    },
    "pool": "threads",
    "poolOptions": {
      "threads": {
        "minThreads": 1,
        "useAtomics": true
      },
      "forks": {
        "minForks": 1
      }
    },
    "fakeTimers": {
      "now": 1588550400000,
      "toFake": [
        "Date"
      ]
    },
    "environment": "happy-dom"
  }
}

I removed swc angular, and ran the tests via vitest cli, always the same error.
reverting back to v1.9.1 fixes everything

Can you provide a reproduction? I could see this happening if it's running in ESM-only mode, but Nx doesn't run as ESM. What version of Vitest?

Hi, we encountered a very similar problem (same error message). For us, it is connected to the pool = "threads" option that seems to change how module loading works (or doesn't work).

Here is a complete reproduction guide: https://github.com/fabian-paus/angular-vitest-bug

The error can simple be reproduced by:

  • Setting up an analogjs project: npm create analog@latest
  • Then changing the test config to pool = "threads" (or "forks")

The problem can be "solved" by editing @analogjs\vitest-angular\package.json to "type": "commonjs".

I am a novice on this whole CommonJS / ESM debacle. But I tried looking into @analogjs\vitest-angular. It seems to contain CommonJS code (require syntax), but is marked as "type": "module", which indicates ESM syntax (import, export).

It seems that with pool = "vmThreads" vitest somehow ignores this inconsistency.

Changing the config from .ts to .mts did not change anything.

The error is reproducible on any system and version that I tried:

  • Windows and Linux
  • Node 22.11 and 20.18
  • Vitest version 1.6 and 2.1.4

The fix has landed in Analog 1.9.3-beta.1 for testing

Ha! You saved my weekend 😁

Thanks a lot!

I had the same problem and it was caused by #1434, downgrading to v1.9.2-beta.3 and all was working as before. It's curious that it would be affected by "type": "module".

My Vitest file are already vite.config.mts.

@Yberion interesting, I wouldn't have expected that. So everything is back working with the latest beta?

@Yberion interesting, I wouldn't have expected that. So everything is back working with the latest beta?

For me it is :)

@Yberion interesting, I wouldn't have expected that. So everything is back working with the latest beta?

Yeah it is now working with the last beta.

But still confused about why adding nx to optimizeDependencies.exclude would suddently cause this problem. Perhaps esbuild was optimizing the code and translating it somehow to ESM compatible code.

@Yberion interesting, I wouldn't have expected that. So everything is back working with the latest beta?

Yeah it is now working with the last beta.

But still confused about why adding nx to optimizeDependencies.exclude would suddently cause this problem. Perhaps esbuild was optimizing the code and translating it somehow to ESM compatible code.

Probably so. nx is still CommonJS right now, so it would try to optimize it along with its dependencies, which could lead to a bit of "over optimizing"

Analog 1.9.3 is released with the fix