Debug adonis tests

Hello.

Is it possible debug adonis tests? There isn’t an option like --debug in adonis test.

Thanks in advance.

1 Like

Hi @giovanicascaes!

There are several ways. You can use good old console.log

Or breakpoints inside your editor (this requires some editor side setup)

Or DEBUG env variable and run tests like DEBUG=adonis:request npm run test to debug Adonis requests.

You can get full list of DEBUG env variables when you set debug to all DEBUG=*

1 Like

Hello, @McSneaky!

I forgot to mention, I would like to debug in VSCode, with breakpoints and so on. Is it possible?

I’ve found this: https://viglucci.io/debugging-adonis-with-vscode. But I don’t know what parameters ace test accepts to run only specific tests.

Thanks.

That in blog might work too, but I have lil bit different setup.

Scripts inside package.json

"scripts": {
  "start": "node server.js",
  "test": "node ace test",
  "test:debug": "node --nolazy --inspect-brk=9229 ace test"
},

And then VSCode setup:
.vscode/launch.json add this to configurations array

{
  "type": "node",
  "request": "launch",
  "name": "Launch via NPM",
  "runtimeExecutable": "npm",
  "runtimeArgs": [
    "run-script",
    "test:debug"
  ],
  "port": 9229
}

Whole launch.json file should look something similar to this:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch via NPM",
      "runtimeExecutable": "npm",
      "runtimeArgs": [
        "run-script",
        "test:debug"
      ],
      "port": 9229
    }
  ]
}
2 Likes

Got it. I tried something like this. But I’d like to run the tests for just one file. Is it that possible with ace test? Is there some option?

If you run node ace test -h it should show exact command
I think it was something like node ace test -g user and it will run all user related tests
Or if you want specific test file(s) it is node ace test -f file1.js you can comma separate if you want more than one file

I forgot to check -h option. Thanks, bud. But I still can’t run debug. This is what I get:

C:\Program Files\nodejs\node.exe --inspect-brk=6895 src\ace.js test
Debugger listening on ws://127.0.0.1:6895/3ea3d1c0-6082-42c1-b5f7-67b6e69082a8
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module '@adonisjs/ignitor'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (c:\dev\Raizen\server\src\ace.js:16:21)
    at Module._compile (internal/modules/cjs/loader.js:773:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
Waiting for the debugger to disconnect...
Error: Cannot find module '@adonisjs/ignitor'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (c:\dev\Raizen\server\src\ace.js:16:21)
    at Module._compile (internal/modules/cjs/loader.js:773:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

Hmm, there might be some Windows features that I am not aware of…

You might need to add that windows part to your launch configuration

  "runtimeExecutable": "npm",
  "windows": {
    "runtimeExecutable": "npm.cmd" // Maybe full path to npm.exe?
  },

But got question, why is your ace.js inside src folder? And why is it ace.js in first place. It should be called just ace and live in project root.

Are you sure you installed all npm packages? :thinking:

Hi!

I tried your config, but that’s all I’ve got: C:\Program Files\nodejs\npm.cmd --inspect-brk=36036 src\ace.js test. I think the executable is correct, given this output. The ace.js is because the plain ace file isn’t recognized by node. That file just requires the latter. I don’t know why it is in src, but it seems to be found, anyways.

In default template ace is in project root and it is not .js file

You can try to reinstall packages

Works like charm on Linux… Maybe there is some other Windows feature :thinking:

1 Like

I think it is in the src folder because we are running a Docker container. So the resources aren’t being find on run npm. I’ll do some research about that. Thank you very mych for the help.

Are you running app inside container and trying to execute command out of container?

Also when testing from container you have to expose port for testing too, not only main entrypoint

The app is running inside a container. I can attach the debugger to the app running, but I can’t figure out a way to launch test and debug.

Then you need separate setup. Current launch.json tries to start debugging out of container but you need to attack to already running app. Something like this:

{
  "name": "Docker: Attach to Node",
  "type": "node",
  "request": "attach",
  "port": 36036,
  "address": "localhost",
  "localRoot": "${workspaceFolder}",
  "remoteRoot": "/usr/src/app",
  "protocol": "inspector"
}

I’m using something like this, but port 9229. I can get my app debugging, but how I combine this with launching tests?

Inside Dockerfile expose both ports, API one and debug

FROM node:alpine #example image

EXPOSE 3000 # example API port
EXPOSE 9229 # example debug port
... rest of Dockerfile

Then start it up with docker build and run (or however your flow is set up)
And then run tests inside container
docker exec CONTAINER_ID_or_NAME npm run test

My Dockerfile is like this. I tried docker exec and docker-compose calling node ace test while debugging app, but no success on fire breakpoints.