Adonis 5 Mongoose unit test - node.js

I'm using Japa to create a Unit test for my adonis 5 projects,
Somehow when I try to run my unit test in my local environment(Mac Os) without mongodb, my unit test was running well, and it was ignoring the connection error. But when I try to run it on my Ubuntu server, it can't ignore the connection error.
Here is my code:
I have tried to add a try-catch block at create a connection, but it didn't work. Why there is some differences between my Mac Os and Ubuntu. Can I test it without MongoDB?

Related

pytest cannot find database and tables

I am trying to speed up the test runtime by using pytest-xdist, previously I was using pytest, pytest-django to run the tests,
After installing pytest-xdist, One is an issue which I am facing is most of the tests are failing with messages that relation does not exist
pytest -n auto accounts/tests
for example
psycopg2.errors.UndefinedTable: relation "auth_user" does not exist
I guess there is error which says database does not exist:
django.db.utils.OperationalError: FATAL: database "test_dev" does not exist
But the interesting thing is some of tests are passing :(

[AWS][Amplify] Invoke function locally crashs with no error

I have just joined a developpment team, and the project should run in the cloud using amplify. I have a function called usershandler that i want to run locally. For that, i used :
amplify invoke function usershandler
This is the output i get :
Starting execution...
EVENT: {"httpMethod":"GET","body":"{\"name\": \"Amplify\"}","path":"/users","resource":"/{proxy+}","queryStringParameters":{}}
App started
get All VSM called
Connection to database was a success
null
Result:
{"statusCode":200,"body":"{\"success\":true,\"results\":[]}","headers":{"x-powered-by":"Express","access-control-allow-origin":"*","access-control-allow-headers":"Origin, X-Requested-With, Content-Type, Accept","content-type":"application/json; charset=utf-8","content-length":"29","etag":"W/\"1d-4wD7ChrrlHssGyekznKfKxR7ImE\"","date":"Tue, 21 Jul 2020 12:32:36 GMT","connection":"close"},"isBase64Encoded":false}
Finished execution.
EDIT : Also, when running the invoke command, amplify asks me for a src/event.json while i've seen it looking for the index.js for some ??
EDIT 2 [SOLVED] : downgrading #aws-amplify/cli to 4.14.1 seems to solve this :)
Expected behavior : The server should continue running so i can use it ..
Actual behavior : It always stops after the finished execution message.
The connection to the db works fine, the config.json contains correct values. Don't know why it is acting like this. Have anybody had the same problem?
Have a nice day.
Short answer: You are running the invoke command which is doing just what it is supposed to be doing - invoking the lambda function.
If you are looking to get a local API up, then run the following command:
sam local start-api
This will read your template and based on the endpoints you have setup, run them locally essentially mocking API Gateway locally. Read more about it in the official docs here.
Explanation:
This command comes is one of offering of AWS Serverless Application Model (AWS SAM). A tool to develop serverless application. It is essentially an abstraction of AWS Cloufdformation. Similarly Amplify is an abstraction that makes it simple to not only develop and manage the backend but also brings that power to frontend.
As both of them essentially use Cloudformation templates underneeth, you can leverage the capabilities of one tool with another.
SAM provides a robust set of tools for local development invcluding running a local lambda mocking server, in case you are not using API Gateway.
I use this combination to develop and test my frontend along with backend which is in golang, a language which is not as mature as javascript as a backend language with Amplify as of now.

"Resource temporarily unavailable" error trying to run nodejs app in AS/400

I need to connect a NodeJS to AS/400 server. In order to do that, I installed NodeJS in AS/400 with IBM documentation and tryed (succesfully) to send and receive data with Class iDataQueues (https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/IBM%20i%20Technology%20Updates/page/Toolkit%20for%20i%20APIs?section=Class%20iDataQueue) between AS/400 server and my develope computer. My next step was to run a program (Class iPgm) to received some data.
I'm running my test with
/path/to/ibm/node/installation/node /home/test/app.js"
and I'm getting a "node[537]: pthread_create: Resource temporarily unavailable" error, but I have no idea what is the problem:
Server has enough resources
There are not any node running in server
There are not any changes in server config between iDataQueue and iPgm tests
I have root privileges
I think my code is not the problem because I dont' even run it ¬¬'
This is caused by running node in QSH. By default QSH does not support running threads, so any attempt to create a thread will result in that "Resource temporarily unavailable" message. This can fixed by setting the environment variable QIBM_MULTI_THREADED=Y prior to running QSH, using QP2TERM, or using SSH.

MongoError: Cannot create collection users - database is in the process of being dropped

I have a REST API and I'm writing TDD for this project. My TDD is consisted of two parts: route and service. I chose to use Jest. I have a MongoDB database that I use for testing. When each test is completed, I reset my database using the afterAll() method. In this method, I run the mongoose.connection.dropDatabase function.
There is no error when I ran only one test file but when I run multiple test files, I get an error. The error message:
MongoError: Cannot create collection auth-db.users - database is in
the process of being dropped.
I share sample codes with you:
users.route.test.ts:
https://gist.github.com/mksglu/8c4c4a3ddcb0e56782725d6457d97a0e
users.service.test.ts:
https://gist.github.com/mksglu/837202c1048687ad33b4d1dee01bd29c
When all my tests run, "sometimes" gives errors. I wrote the above error message. The reason for this error is that the reset process still continues. I can't solve this problem. I'd appreciate it if you could help.
Thanks.
https://jestjs.io/docs/en/cli.html#runinband
What you are looking for is --runInBand command. Which makes jest to run serially instead of creating a worker pool of child processes that run tests

Fail/terminate node.js process if Mithril Ospec tests fail

When I run unit tests via Ospec for Mithril, I can see if tests fail locally in the console.
What I'm looking for is a solution that will not allow a following Node.js build script to execute if one or more of the tests fail.
I don't want code to be pushed up to another environment/lane if the unit tests aren't passing.
I don't see how to accomplish this in the docs.
In Node, I'm running ospec && someBuildProcess.
The answer might be a Node.js thing, but I'm at a loss for what to look for now.
ospec calls process.exit(1) if any tests fail, and the command string you posted should work. I just verified it locally with the following setup:
https://gist.github.com/tivac/d90c07592e70395639c63dd5100b50a6
ospec runs, fails, and the echo command never gets called.
Can you post some more details about your setup?

Resources