Node JS typescript project: running tests with Mocha and using @swc-node

154 Views Asked by At

I am trying to learn Node JS, to build a REST API. Coming from a Python background, this is quite challenging.

My issue is: I am trying to set up automated testing using Mocha. To do this, I have the following command in my package.json:

"test": "cross-env SWCRC=true NODE_OPTIONS='--loader @swc-node/register/esm' mocha ./src/tests/**/*.test.ts"

I run the test script using pnpm on my Windows machine.

  • Node.js version 18.14.0
  • pnpm version 8.14.0

Here is the error message:

> cross-env SWCRC=true NODE_OPTIONS='--loader @swc-node/register/esm' mocha ./src/tests/**/*.test.ts

The system cannot find the path specified.
node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: spawn @swc-node\register\esm ENOENT
    at notFoundError (PATH_TO_FOLDER\Nodejs_tutorials\node_modules\.pnpm\[email protected]\node_modules\cross-spawn\lib\enoent.js:6:26)
    at verifyENOENT (PATH_TO_FOLDER\Nodejs_tutorials\node_modules\.pnpm\[email protected]\node_modules\cross-spawn\lib\enoent.js:40:16)
    at cp.emit (PATH_TO_FOLDER\Nodejs_tutorials\node_modules\.pnpm\[email protected]\node_modules\cross-spawn\lib\enoent.js:27:25)
    at ChildProcess._handle.onexit (node:internal/child_process:291:12)
Emitted 'error' event on ChildProcess instance at:
    at cp.emit (PATH_TO_FOLDER\Nodejs_tutorials\node_modules\.pnpm\[email protected]\node_modules\cross-spawn\lib\enoent.js:30:37)
    at ChildProcess._handle.onexit (node:internal/child_process:291:12) {
  code: 'ENOENT',
  errno: 'ENOENT',
  syscall: 'spawn @swc-node\\register\\esm',
  path: '@swc-node\\register\\esm',
  spawnargs: [ 'mocha', './src/tests/**/*.test.ts' ]        
}

Node.js v18.14.0
 ELIFECYCLE  Test failed. See above for more details. 

I have tried multiple times to reinstall the node_packages, without success. I've also tried to run mocha with the ts-node compiler instead of @swc, but there too I ran into issues. (I've changed the full path with "PATH_TO_FOLDER" for brevity).

Any ideas of what I could try? Please let me know if more information is needed. Any help is extremely appreciated.

Here is my package.json:

{
   "name": "Nodejs_tutorials",
   "version": "1.0.0",
   "description": "",
   "main": "index.js",
   "scripts": {
      "build": "swc src --out-dir build",
      "build:clean": "rm -rf build && pnpm run build",
      "watch": "swc src -d build --watch",
      "start": "node build/server.js",
      "dev": "concurrently \"pnpm run watch\" \"node --watch build/server.js\"",
      "test": "cross-env SWCRC=true NODE_OPTIONS='--loader @swc-node/register/esm' mocha ./src/tests/**/*.test.ts"
   },
   "type": "module",
   "keywords": [],
   "author": "",
   "license": "ISC",
   "devDependencies": {
      "@swc-node/register": "^1.6.8",
      "@swc/cli": "^0.1.63",
      "@swc/core": "^1.3.103",
      "@swc/helpers": "^0.5.3",
      "@types/chai": "^4.3.11",
      "@types/express": "^4.17.21",
      "@types/mocha": "^10.0.6",
      "@types/node": "^20.11.3",
      "chai": "^5.0.0",
      "chokidar": "^3.5.3",
      "class-transformer": "^0.5.1",
      "class-validator": "^0.14.1",
      "concurrently": "^8.2.2",
      "mocha": "^10.2.0",
      "ts-node": "^10.9.2",
      "typescript": "^5.3.3"
   },
   "dependencies": {
      "cross-env": "^7.0.3",
      "es6-shim": "^0.35.8",
      "express": "^4.18.2",
      "help": "^3.0.2",
      "reflect-metadata": "^0.2.1",
      "yarn": "^1.22.21"
   }
}

And here is my tsconfig.json:

{
    "compilerOptions": {
      "esModuleInterop": true,
      "allowImportingTsExtensions": true,
      "noEmit": true,
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true,
      "strictPropertyInitialization": false, /* Check for class properties that are declared but not set in the constructor. */
      "strict": false /* Enable all strict type-checking options. */,
    },
  }
  • I ran pnpm test, expecting the code to run properly,but got the error above instead;
  • I deleted all packages inside of node_modules and reinstalled them. The issue persists.
  • I ran with another compiler, ts-node, using the command pnpm mocha --loader ts-node/esm .\src\tests\handlers\user.handler.test.ts. Here, I get the following error message:
PS PATH_TO_FOLDER\Nodejs_tutorials> pnpm mocha --loader ts-node/esm .\src\tests\handlers\user.handler.test.ts
(node:9336) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file and data are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'
    at new NodeError (node:internal/errors:399:5)
    at throwIfUnsupportedURLScheme (node:internal/modules/esm/resolve:1059:11)
    at defaultResolve (node:internal/modules/esm/resolve:1135:3)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ESMLoader.import (node:internal/modules/esm/loader:525:22)
    at initializeLoader (node:internal/process/esm_loader:75:58)
    at loadESM (node:internal/process/esm_loader:90:11)
    at runMainESM (node:internal/modules/run_main:55:21) {  
  code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
}

Node.js v18.14.0
0

There are 0 best solutions below