I want users to submit code and run tests against that code using AVA. The AVA CLI seems great. But this would be running in a hosted environment like Heroku and need to respond back to the POST request containing the code submission with the results of the unit tests.
When I try var test = require('ava') and then call the test function in a route, I get this message when starting the node server:
[OKAY] Loaded ENV .env File as KEY=VALUE Format
10:24:11 PM web.1 | Test files must be run with the AVA CLI:
10:24:11 PM web.1 | $ ava index.js
[DONE] Killing all processes with signal null
10:24:11 PM web.1 Exited with exit code 1
I think it is not possible, it is thought to be used through the cli. Actually to me it does not make much sense for it be used as a required module in your code.
How did you plan to use it? If you just want to use the assertions you can use some assertion library like chai, expect or power-assert.
If you want more information opening an issue in the AVA repo also could be an option.
Related
I'm building an E-commerce site, where there's an Authentication system.
I noticed that if the client login with a wrong user or password, the backend/server that works with nodemon will crach and hang in there crashed till i restart manually nodemon. This is example output error of the nodemon crash:
[nodemon] app crashed - waiting for file changes before starting...
node:internal/errors:464
ErrorCaptureStackTrace(err);
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent
to the client
Ofcourse, when server crashes, client can no more access or do login again till server restarts.
After some googling, i found this question and this repository that fix my problem but particulary and not as expected precisely, i dont want nodemon to restart forever on any error that occure ofcourse, but only with specifics errors that i set them -like Authentication errors as i mentionned above-.
So, my idea/question is: is there anyway to get nodemon restarts by itself in some cases of failures or errors (NOT ALL)?
Seems like you a referring to a production situation, and nodemon is a development node server, which is not intended for use in production, as the intro states:
nodemon is a tool that helps develop Node.js based applications by
automatically restarting the node application when file changes in the
directory are detected.
You should use node.js in production, instead of nodemon.
For managing your node server in production, you could use a process manager like PM2..
That said, an authentication server that crashes every time a user uses a wrong password seams very ineffective in handling a common use case. So I would advise to start with fixing the root cause, which is the buggy server, and then for recovery from incidental crashes use something like PM2.
PS:
The error you are getting looks like an express error you get when you send a response (in this case an error response) without exiting the function e.g. by using return. Because you are not returning, another res.send is called, which causes the 'ERR_HTTP_HEADERS_SENT' error. See this answer.
This is really bad since it can send your program into a loop of restarting, but if you really want it, replace app.js with your file's name and try this:
nodemon -x 'node app.js || copy /b app.js +,,'
Linux version:
nodemon -x 'node app.js || touch app.js'
Next time try a little googleing before you ask since it is most likely faster.
I attempted to use the command Heroku pg:psql to connect to my database addon in heroku but got a response below
--> Connecting to postgresql-regular-61345
unrecognized win32 error code: 123could not find a "psql" to execute
unrecognized win32 error code: 123could not find a "psql" to execute
psql: fatal: could not find own program executable
! psql exited with code 1
After using the heroku logs --tail command i got the following errors
sh: 1: nodemon: not found
Process exited with status 127
State changed from starting to crashed
I can also see all processes stopping with SIGTERM and the process exiting with status 143
Resolution steps I have taken
Verified that the environment variables have the path for installed postgress14 on my PC
Added a procfile to the root file in my backend code and spcified "web: node matthewfaceappback/server.js in the file"
Changed my set port to a variable port using process.env.PORT || 3000
Set all environment variable including my database url(set by default) on config variable in heroku
Verified there is a start up script
Updated all my packages using "npm update". after doing this i started expereincing the issue of processes stopping with SIGTERM and the process exiting with status 143
I moved nodemon from devDependencies to dependencies. nodemon version is 2.0.15
In package.json i inputed an engines parameter using the version of node in my case
{"engines": {
"node": "14.17.4"
}}
I restarted heroku using "heroku restart"
Below are links to the screenshots of the error
https://www.dropbox.com/s/5bdbyi9e99lbxhu/pic1.PNG?dl=0
https://www.dropbox.com/s/41euniaes5q68c9/pic2.PNG?dl=0
https://www.dropbox.com/s/50oqzbwmwrqogax/pic3.PNG?dl=0
Put nodemon back in the devDependencies and add it as a second node script in package.json:
"scripts": {
"start": "node matthewfaceappback/server.js",
"dev": "nodemon matthewfaceappback/server.js"
},
These two errors are completely unrelated.
The database connection error
The first issue, which I believe is the one you actually care about at the moment based on the title of the question, indicates that the Heroku CLI can't find a PostgreSQL client on your local machine.
The documentation makes the following recommendation
Set up Postgres on Windows
Install Postgres on Windows by using the Windows installer.
Remember to update your PATH environment variable to add the bin directory of your Postgres installation. The directory is similar to: C:\Program Files\PostgreSQL\<VERSION>\bin. Commands like heroku pg:psql depend on the PATH and do not work if the PATH is incorrect.
If you haven't already installed Postgres locally, do so. (This is a good idea anyway as you should be developing locally and you'll probably need a database.)
Then make sure to add its bin/ directory to your PATH environment variable.
The Nodemon error
The second issue is because you are trying to use nodemon in production. Heroku strips development dependencies out of Node.js applications after building them, which normally makes sense. Nodemon is a development tool, not something that should be used for production hosting.
Depending on the contents of your package.json, this might be as simple as changing your start script from nodemon some-script.js to node some-script.js. Alternatively, you can add a Procfile with the command you actually want to run on Heroku:
web: node some-script.js
See also Need help deploying a RESTful API created with MongoDB Atlas and Express
I have a script that sequentially executes 2 instructions as following:
node server.js
node tests.js
Server.js initializes a local Node.js + Express server
Tests.js executes some unit tests on that server
The problem is that the first instruction keeps listening for requests, so the second is never executed.
Is it possible to solve this behaviour?
I advise you to use the npm package start-server-and-test to be sure that your api server is ready to be tested
start-server-and-test 'node server.js' http://localhost:{API_PORT}/ 'node tests.js'
I'm trying to get coverage report for the API code. I have all the test cases running perfectly in mocha. My problem is that my API server and the test cases are written in separate repositories.
I start my node API server on localhost, on a particular port, and then using supertest in mocha, hit the localhost url to test server's response.
Can you suggest me the best way to generate a coverage report for those APIs?
Testing env
If you want to get coverage, supertest should be able to bootstrap the app server, like in the express example.
The drawback is that you must not run your tests against a running server, like
var api = request('http://127.0.0.1:8080');
but you must include your app entrypoint to allow supertest to start it like
var app = require('../yourapp');
var api = request(app);
Of course, this may (or may not) result in a bit of refactoring on your app bootstrap process.
As other options, you can use node CLI debug capabilities or use node-inspector.
Coverage setup
Supposing you are willing to install istanbul in association with mocha to get coverage.
npm install -g istanbul
then
istanbul cover mocha --root <path> -- --recursive <test-path>
cover is the command use to generate code coverage
mocha is the executable js file used to run tests
--root <path> the root path to look for files to instrument (aka the "source files")
-- is used to pass arguments to your test runner
--recursive <test-path> the root path to look for test files
You can then add --include-all-sources to get cover info on all your source files.
In addition you can get more help running
istanbul help cover
15:05:09 web.1 | started with pid 4888
15:05:10 web.1 |
15:05:10 web.1 | > finext_server#0.0.1 test F:\Projects\finext\server
15:05:10 web.1 | > set NODE_ENV=development && node ./bin/www.js
15:05:11 web.1 |
15:05:12 web.1 | exited with code 5
15:05:12 system | sending SIGKILL to all processes
15:05:12 | app.get('env'): development
This is the output I get, and it is a little variable in terms of the code that gets executed after SIGKILL. Sometimes, a few more lines of coide get executed before I see the command prompt again.
I tried using node-foreman, and my application works fine there. So i am guessing it has something to do with foreman. My code and procfile can be found here: https://github.com/chintanp/finext
This is the reason that the process cannot start on heroku as well.
Someone in another world had a similar problem. Express Hello World -- Heroku Foreman Returns Code 5, 'npm start' works just fine
In Procfile try this:
web: node app.js
I recently faced exactly the same situation as you when running my application:
foreman logged the messages "exited with code 5" and "sending SIGKILL to all processes".
My app worked fine if I used npm ("npm start") or node-foreman ("nf start") instead of foreman.
The app also worked using forego, a go application written by the author of foreman.
But a couple of weeks ago heroku added the new command "heroku local", which uses forego behind the scenes, and formally supersedes the use of foreman:
Starting today, new Heroku Toolbelt installs will not come with
Foreman. The command heroku local has replaced foreman. Heroku Local
makes use of Forego to accomplish its tasks and it’s faster and has
better cross-platform support.
Note that heroku also now state that foreman is "not officially supported".
So the way to resolve your problem is:
Stop using foreman.
Download and install the latest version of heroku toolbelt.
Replace "foreman start" with "heroku local".
This approach worked for me on Windows 7.