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

[BUG] Several Issues with Jest #2965

Open
ThanatosGit opened this issue Jan 17, 2025 · 4 comments
Open

[BUG] Several Issues with Jest #2965

ThanatosGit opened this issue Jan 17, 2025 · 4 comments
Assignees
Labels

Comments

@ThanatosGit
Copy link

Describe the bug

? Choose the target Framework: Express.js
? Choose the architecture for your project: Ts.ED
? Choose the convention file styling: Ts.ED
? Check the features needed for your project Documentation, Testing
? Choose a documentation plugin
? Choose unit framework Jest
? Choose the runtime: Node.js + SWC
? Choose the package manager: PNPM
↓ Write RC files
↓ Write RC files
✔ Initialize package.json
✔ Install plugins
✔ Load plugins
✔ Install plugins dependencies
✔ Generate project files
✔ Install dependencies
✔ Generate barrels files

Creates a package.json without jest itself`.

{
  "name": "jest-test",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "build": "pnpm run barrels && swc src --out-dir dist -s  --strip-leading-paths",
    "barrels": "barrels",
    "start": "pnpm run barrels && nodemon src/index.ts",
    "start:prod": "cross-env NODE_ENV=production node --import @swc-node/register/esm-register src/index.js"
  },
  "dependencies": {
    "@swc-node/register": "^1.10.9",
    "@swc/cli": "^0.6.0",
    "@swc/core": "^1.10.7",
    "@swc/helpers": "^0.5.15",
    "@tsed/ajv": "^8.4.2",
    "@tsed/barrels": "^6.1.6",
    "@tsed/core": "^8.4.2",
    "@tsed/di": "^8.4.2",
    "@tsed/engines": "^8.4.2",
    "@tsed/exceptions": "^8.4.2",
    "@tsed/json-mapper": "^8.4.2",
    "@tsed/logger": "^7.0.1",
    "@tsed/openspec": "^8.4.2",
    "@tsed/platform-cache": "^8.4.2",
    "@tsed/platform-exceptions": "^8.4.2",
    "@tsed/platform-express": "^8.4.2",
    "@tsed/platform-http": "^8.4.2",
    "@tsed/platform-log-request": "^8.4.2",
    "@tsed/platform-middlewares": "^8.4.2",
    "@tsed/platform-params": "^8.4.2",
    "@tsed/platform-response-filter": "^8.4.2",
    "@tsed/platform-views": "^8.4.2",
    "@tsed/schema": "^8.4.2",
    "ajv": "^8.17.1",
    "body-parser": "^1.20.3",
    "compression": "^1.7.5",
    "cookie-parser": "^1.4.7",
    "cors": "^2.8.5",
    "cross-env": "^7.0.3",
    "dotenv": "^16.4.7",
    "dotenv-expand": "^12.0.1",
    "dotenv-flow": "^4.1.0",
    "express": "^4.21.2",
    "method-override": "^3.0.0",
    "typescript": "^5.7.3"
  },
  "devDependencies": {
    "@tsed/cli-plugin-jest": "6.1.6",
    "@types/compression": "^1.7.5",
    "@types/cookie-parser": "^1.4.8",
    "@types/cors": "^2.8.17",
    "@types/express": "^5.0.0",
    "@types/method-override": "^3.0.0",
    "@types/multer": "^1.4.12",
    "@types/node": "^22.10.7",
    "@types/supertest": "^6.0.2",
    "nodemon": "^3.1.9",
    "supertest": "^7.0.0",
    "tslib": "^2.8.1"
  },
  "tsed": {
    "convention": "conv_default",
    "architecture": "arc_default",
    "packageManager": "pnpm",
    "runtime": "node"
  },
  "type": "module",
  "packageManager": "[email protected]+sha512.b2dc20e2fc72b3e18848459b37359a32064663e5627a51e4c74b2c29dd8e8e0491483c3abb40789cfd578bf362fb6ba8261b05f0387d76792ed6e23ea3b1b6a0"
}

Though it does work if you add Swagger for Documentation (I haven't tested any other combinations).

? Choose the target Framework: Express.js
? Choose the architecture for your project: Ts.ED
? Choose the convention file styling: Ts.ED
? Check the features needed for your project Documentation, Testing
? Choose a documentation plugin Swagger
? Choose unit framework Jest
? Choose the runtime: Node.js + SWC
? Choose the package manager: PNPM
↓ Write RC files
↓ Write RC files
✔ Initialize package.json
✔ Install plugins
✔ Load plugins
✔ Install plugins dependencies
✔ Generate project files
✔ Generate files for jest
✔ Install dependencies
✔ Generate barrels files
{
  "name": "jest-test",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "build": "pnpm run barrels && swc src --out-dir dist -s  --strip-leading-paths",
    "barrels": "barrels",
    "start": "pnpm run barrels && nodemon src/index.ts",
    "start:prod": "cross-env NODE_ENV=production node --import @swc-node/register/esm-register src/index.js",
    "test": "pnpm run test:lint && pnpm run test:coverage ",
    "test:unit": "cross-env NODE_ENV=test jest",
    "test:coverage": "pnpm run test:unit "
  },
  "dependencies": {
    "@swc-node/register": "^1.10.9",
    "@swc/cli": "^0.6.0",
    "@swc/core": "^1.10.7",
    "@swc/helpers": "^0.5.15",
    "@tsed/ajv": "^8.4.2",
    "@tsed/barrels": "^6.1.6",
    "@tsed/core": "^8.4.2",
    "@tsed/di": "^8.4.2",
    "@tsed/engines": "^8.4.2",
    "@tsed/exceptions": "^8.4.2",
    "@tsed/json-mapper": "^8.4.2",
    "@tsed/logger": "^7.0.1",
    "@tsed/openspec": "^8.4.2",
    "@tsed/platform-cache": "^8.4.2",
    "@tsed/platform-exceptions": "^8.4.2",
    "@tsed/platform-express": "^8.4.2",
    "@tsed/platform-http": "^8.4.2",
    "@tsed/platform-log-request": "^8.4.2",
    "@tsed/platform-middlewares": "^8.4.2",
    "@tsed/platform-params": "^8.4.2",
    "@tsed/platform-response-filter": "^8.4.2",
    "@tsed/platform-views": "^8.4.2",
    "@tsed/schema": "^8.4.2",
    "@tsed/swagger": "^8.4.2",
    "ajv": "^8.17.1",
    "body-parser": "^1.20.3",
    "compression": "^1.7.5",
    "cookie-parser": "^1.4.7",
    "cors": "^2.8.5",
    "cross-env": "^7.0.3",
    "dotenv": "^16.4.7",
    "dotenv-expand": "^12.0.1",
    "dotenv-flow": "^4.1.0",
    "express": "^4.21.2",
    "method-override": "^3.0.0",
    "typescript": "^5.7.3"
  },
  "devDependencies": {
    "@tsed/cli-plugin-jest": "6.1.6",
    "@types/compression": "^1.7.5",
    "@types/cookie-parser": "^1.4.8",
    "@types/cors": "^2.8.17",
    "@types/express": "^5.0.0",
    "@types/jest": "^29.5.14",
    "@types/method-override": "^3.0.0",
    "@types/multer": "^1.4.12",
    "@types/node": "^22.10.7",
    "@types/supertest": "^6.0.2",
    "jest": "^29.7.0",
    "nodemon": "^3.1.9",
    "supertest": "^7.0.0",
    "ts-jest": "^29.2.5",
    "tslib": "^2.8.1"
  },
  "tsed": {
    "convention": "conv_default",
    "architecture": "arc_default",
    "packageManager": "pnpm",
    "runtime": "node"
  },
  "type": "module",
  "packageManager": "[email protected]+sha512.b2dc20e2fc72b3e18848459b37359a32064663e5627a51e4c74b2c29dd8e8e0491483c3abb40789cfd578bf362fb6ba8261b05f0387d76792ed6e23ea3b1b6a0"
}

But this package.json is still bugged. "test": "pnpm run test:lint && pnpm run test:coverage ", is referencing test:lint which is not defined.

If you run the test command:

thanatos@DESKTOP-5NP8L7E:~/projects/tsed-repos/jest-test$ pnpm run test:unit

> [email protected] test:unit /home/thanatos/projects/tsed-repos/jest-test
> cross-env NODE_ENV=test jest

ReferenceError: module is not defined
    at file:///home/thanatos/projects/tsed-repos/jest-test/jest.config.js:4:1
    at ModuleJobSync.runSync (node:internal/modules/esm/module_job:395:35)
    at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:329:47)
    at loadESMFromCJS (node:internal/modules/cjs/loader:1411:24)
    at Module._compile (node:internal/modules/cjs/loader:1544:5)
    at Object..js (node:internal/modules/cjs/loader:1668:16)
    at Module.load (node:internal/modules/cjs/loader:1313:32)
    at Function._load (node:internal/modules/cjs/loader:1123:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
 ELIFECYCLE  Command failed with exit code 1.

ReferenceError: module is not defined is thrown because the config file is wrong.
You could (but shouldn't) change from module.exports = { to export default {:

thanatos@DESKTOP-5NP8L7E:~/projects/tsed-repos/jest-test$ pnpm run test:unit

> [email protected] test:unit /home/thanatos/projects/tsed-repos/jest-test
> cross-env NODE_ENV=test jest

 FAIL  src/controllers/rest/HelloWorldController.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:

    /home/thanatos/projects/tsed-repos/jest-test/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@[email protected]/node_modules/@tsed/platform-http/lib/esm/testing/index.js:4
    export * from "./FakeAdapter.js";
    ^^^^^^

    SyntaxError: Unexpected token 'export'

    > 1 | import { PlatformTest } from "@tsed/platform-http/testing";
        | ^
      2 | import { HelloWorldController } from "./HelloWorldController.js";
      3 |
      4 | describe("HelloWorldController", () => {

      at Runtime.createScriptFromCode (node_modules/.pnpm/[email protected]/node_modules/jest-runtime/build/index.js:1505:14)
      at Object.<anonymous> (src/controllers/rest/HelloWorldController.spec.ts:1:1)

You can then do some experimental stuff mentioned in https://jestjs.io/docs/ecmascript-modules but it was a nightmare to get this to work for me.
Eventually, it didn't even work completly. Because I'm using Swagger it needs import.meta.dirname if you bootstrap the Server in a test.
There is another rabbit hole about mocking it etc. shown here:
https://stackoverflow.com/questions/64961387/how-to-use-import-meta-when-testing-with-jest

At the end it was much easier to switch from babel to swc and from jest to vitest because this just works.

To Reproduce

See description.

Expected behavior

Tests and package.json should be correct if the project was created with the tsed cli.
To me it looks very hard to maintain and support jest with the limited esmsupport, especially regarding the fact that tsed is now esm only.
So...maybe just drop jest support?
(Funny side note: The first example at https://tsed.dev/docs/testing.html is even importing vitest for the jest example)

Code snippets

Repository URL example

No response

OS

Ubuntu 22.04.5

Node version

22.13.0

Library version

8.4.2

Additional context

No response

@Romakita
Copy link
Collaborator

I don't recommend to use jest with ESM. Instead use vitest!

@Romakita
Copy link
Collaborator

Agree, I'll remove jest from cli for v8 branch, because isn't stable and it'll be nightmare to maintain that until jest haven't fixed esm issue.

@Romakita
Copy link
Collaborator

@ThanatosGit I released a fix for th CLI. Now Jest appear with (unstable) title ^^.
I also changed the testing.md page to present vitest example first.

@ThanatosGit
Copy link
Author

Alright. I don't know if you want to do anything to fix the issues I mentioned above or keep it like it is and close this issue here.
But honestly I would just keep it as unstable =" use at your own risk" as long as jest has no proper esm support. Then this could be revisited

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants