Adonis 4.1.0.
This is not a feature request, more a question. Though if there is a simple solution to make what I am asking for, I could improve Adonis docs with the solution.
I make all my testing with Mocha. I know Adonis provides its own testing infrastructre but I tend to use more universal testing boilerplate accross all the frameworks I use as I use a lot of them.
I make unit testing where I need to run my units under Adonis application environment but I do not need a web server running. I would like to have the access to all the Adonis app globals, configs as if I am in the Adonis app server side code.
What I would like to see is similar to Laravel's use CreatesApplication; or Lumen's require __DIR__ . '/../bootstrap/app.php'; in src/back/tests/TestCase.php
there.
I hope there is some similar simple way in Adonis to spin the application and I hope to hear from the framework developers on it.
Well, I could not infer much from the current Adonis test docs to produce the solution myself.
Yes.
If I get it right, you want to bootstrap the application before running your mocha tests?
If I get it right, you want to bootstrap the application before running your mocha tests?
@thetutlage Yes. This is it. I need only the core code to run. Do not need HTTP environment.
I could contribute to the docs section on how to run Unit / Integration tests with Moca / Chai if there is a need for this. Just need a hint if this bootstapping from under tests is possible and how.
I believe, writing the following code to a setup file should do it.
const { Ignitor } = require('@adonisjs/ignitor')
await new Ignitor(require('@adonisjs/fold')).appRoot(__dirname).fire()
I could contribute to the docs section on how to run Unit / Integration tests with Moca / Chai if there is a need for this
I think, publishing it as a blog post will be more helpful, since there is no end to these sorts of customizations. Someone may want to Jest, Ava or any other testing library
I believe, writing the following code to a setup file should do it.
@thetutlage Thanks a lot. The quick check shows it does. At least use('Config') works and provides a valid config object.
Meanwhile I experimented with the original bootstrap code from my-adonis-website/server.js. It also gave the nearly desired result (I am in the Adonis environment), with the exception that I cannot close the server connection upon test finish. With a minor hint it could give me an ablilty to make integration HTTP tests.
This minor hint is the ability to make server.close() at the end of my Mocha test file.
I dug into node_modules/@adonisjs/ignitor/src/Ignitor/index.js and see the Server object is the private variable (const Server = this._fold.ioc.use('Adonis/Src/Server')). Then I dug into node_modules/@adonisjs/framework/src/Server/index.js and see this._httpInstance holds the pointer to the actual HTTP server created with Node http module. But I suspect that due to the fact that Ignitor Server is not publicly exposed it is impossible to call say Server.close() that would close actual Adonis HTTP server.
With all this in mind can you tell do you know by chance is there a way to close the server explicitly?
I think, publishing it as a blog post will be more helpful
I could write some tutorial on Adonis testing with Mocha, including HTTP testing. Where / how do you think I should better put a post? Do you mean some community Adonis site?
Okay, so the first step is working, where the code I shared, allows you to run the unit tests without starting the server?
Now, you want the ability to also start the server for tests that uses the HTTP server and need the manual control to close the server? If yes
Then pulling the Server from the IoC container should do the Job.
use('Adonis/Src/Server').close(function () {
console.log('closed')
})
Okay, so the first step is working, where the code I shared, allows you to run the unit tests without starting the server?
Yes.
Then pulling the Server from the IoC container should do the Job.
This works too. Just brilliant! Thank you so much for the help.
Where would you recommend I should put the tutorial post to be the most helpful for the framework?
Great. I definitely will within a week or so.
And though I took Adonis train starting with 4.1, but I eagerly would write the post update for v5 as soon as I touch it if you think this is feasible.
Well, after some time working with Adonis + Mocha using the above workarounds I faced the issue with HTTP testing I could not fix.
Just to dicument it, the issue is with HTTP testing via chai-http. The request object in Adonis controller was empty each time I run more than just a single test in my test suite (irrespectively of the other tests were HTTP or simple unit testst with no Adonis web server starting).
Therefore I resorted to the default adonis/vow test infrastructure where the issue did not appear.
Most helpful comment
I believe, writing the following code to a setup file should do it.
I think, publishing it as a blog post will be more helpful, since there is no end to these sorts of customizations. Someone may want to Jest, Ava or any other testing library