I am using typeorm and I have created a test file (city.test.js) inside a entity folder src/entity/City/citi.test.js The test runs without problems when doing npm run test (which just runs jest), the problem is when I want to start the service so I can work with it.
My command is:
"serve": "nodemon ./src/index.js --ext js,graphql --ignore 'src/**/*.test.js' --exec babel-node"
but I get this result
Error: ReferenceError: test is not defined
And I suppose is my test file, it is not getting ignore, any idea what should I do. ?
Thank you.
Related
I'm trying to do something that should be REALLY simple but it's proving exceedingly difficult. I've burned 4 hrs on this today. I have a nodeJS project with server and client parts. I'm using concurrently to launch them both. I want to use the same .env for both server and client. Should be simple, right? Apparently not. The problem is that when I try to use dotenv in my React client, it never finds the .env no matter what I do, and the reason is that __dirname (and process.cwd()) always point to the root directory (i.e. /). I need it to be a relative path, or at least I need to pass somehow the explicit path to my react app.
I've even tried running the client directly by calling 'npm start' from my client directory - the process.cws() still defaults to the root partition and not my project directory.
My directory structure is as follows:
+project_root
- .env
- index.js
- package.json (for the server side)
+ client
- package.json (for client)
+ src
- App.js (this is where I'm trying to access .env)
My package.json for the server has the following in the scripts section:
"scripts": {
"start": "node index.js",
"start-watch": "nodemon index.js",
"dev": "concurrently \"npm run start-watch\" \"cd client && npm start\""
}
On the client side I have
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
I normally start all of this from the project root by calling 'npm run dev'. However, I thought to try and build a run script for it, and from my run script pass in environment variables of the form REACT_APP_
The above works, but in the absence of being able to actually get REACT to set itself to be in a directory other than root, I still cannot get React to read the project .env.
What I've been trying next is to have my run script output all the contents of .env as REACT environment variables. I so far haven't got it to work, here is the run script I tried:
!/bin/sh
export REACT_APP_CURR_DIR=`pwd`
grep -v '^#' .env | while read line
do
echo exporting line $line
VARNAME=REACT_APP_`echo $line | sed -E 's/(.*)=.*/\1/'`
VALUE=`echo $line | sed -E 's/(.*)=(.*)/\2/'`
echo $VARNAME
`export ${VARNAME}=${VALUE}`
done
npm run dev
When I do this, it barfs on some of the variables, but none of them make it into process.env within my react client.
Am I missing something very basic here? I feel like I've gone to extreme lengths just to have server and client use the same .env file so I'm not duplicating parameters everywhere. Even if I put a .env file in my client, the fact that even executing npm start is not giving the React app the right directory is problematic.
I can't believe no one has come into this before.
Well, I have these scripts in my package.json for testing some code in NodeJS.
"scripts": {
"pretest": "env NODE_ENV=test sequelize db:migrate",
"test": "jest",
"posttest": "env NODE_ENV=test sequelize db:migrate:undo:all"
}
When the tests go clear, the "posttest" runs, but when the tests fail, I receive a
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
in VS Code. There is nothing usefull on the link about the problem, neither on the internet.
So I found this link about NPM:
https://github.com/npm/npm/issues/5493
The guy said:
In the vast majority of cases, users are going to be unpleasantly
surprised if posttest runs after test failures (you don't want your
test environment being cleaned up or new versions being published if
there were test failures, for instance). As such, this behavior isn't
going to change. Putting something like "test":"npm run-script
test-failing || npm run-script mandatory-cleanup" into your
package.json will give you what you want.
This did not solve my problem. With more research I found this:
npm posttest doesn't trigger if npm test fails
The solutions did not work for me either.
So how can I run the "posttest" script even if the tests fail?
Well, with the conversation above, I got to this solution:
This is my scripts in package.json:
"scripts": {
"dev": "nodemon src/server.js",
"pretest": "env NODE_ENV=test sequelize db:migrate",
"run-tests": "jest",
"run-after-tests": "env NODE_ENV=test sequelize db:migrate:undo:all",
"test": "npm-run-all run-tests run-after-tests --continue-on-error"
},
I installed the npm-run-all package and it runs the run-after-test script even if the tests fail. Now I get the errors
error Command failed with exit code 1.
from the test script, and
ERROR: "test" exited with 1.
error Command failed with exit code 1.
from run-after-test, but in the end my problem got solved. If someone has a better solution with no errors at the end of the scripts, please share with us.
So I am having this error on Heroku. I just woke up this morning and found it after adding a new feature. It says my www.js file is not found. This file is not on GitHub. I am writing typescript so I'm guessing Heroku will build my project and create a "./dist" folder which will include the /bin/www.js file. The application was working before and still works locally. The application still builds successfully. I have tried adding a Procfile and also used a preinstall script but nothing seems to work. Is it possible to know if the "./dist" folder was successfully created?
Error on Heroku:
Folder Structure:
Scripts:
"scripts": {
"test": "SET NODE_ENV=test&& mocha --exit -r ts-node/register tests/**/*.ts",
"dev": "nodemon src/bin/www.ts",
"start": "nodemon dist/bin/www.js",
"build": "tsc -p .",
"lint": "eslint . --ext .ts",
"precommit": "npm run lint&& npm test",
"preinstall": "npm run build"
}`
The Procfile just has: web: npm start
I found the problem. Apparently, the new build created another www.js file in "dist/src/bin/www.js" and deleted "dist/bin/www.js" on Heroku. I have no idea why this happened. So I changed my start script to "nodemon dist/src/bin/www.js".
We are working on nodeJs/ExpressJs we have configured multiple .env files for development and production and pointing it to package.json for different execution process, we have naming conversation issues at scripts.
Whenever we run npm run prod it takes to preprod configuration. what could be the issues?
Update: we have figured that the suffix of the script key is the same in the next script, after update/rename preprod to preProd the both runs fine. but why?
Eg :
"scripts": {
"dev": "clear; env-cmd -f ./config/hostedDev.env nodemon --exec babel-node index.js",
"prod": "clear; env-cmd -f ./config/prod.env nodemon --exec babel-node index.js",
"preprod": "clear; env-cmd -f ./config/preprod.env nodemon --exec babel-node index.js"
},
Apparently the issue is with the word 'pre'.
If you would have noticed it runs both preprod and prod commands (pre running first).
If you change the script name to 'postprod' the postprod script will run later.
So, I guess npm uses 'pre' as to run before the 'prod' script and then running 'prod' script itself.
I'm trying to run a functional test for a node app.
In my package.json I have the following scripts:
"scripts": {
"web-server": "NODE_ENV=test node app.js &",
"test": "npm run web-server && mocha ./tests/functional/*.js --exit",
"posttest": "pkill -f node"
}
But when running it, tests run before the server completes starting.
How can I wait for the server?
I found wait-on today and like its approach. It only does the wait, not other things like command launching.
Using it with concurrently, like so:
"scripts": {
"xxx": "concurrently -n server,mocha \"npm run web-server\" \"npx wait-on http://localhost:8080 && npx mocha ...\"",
Wanted to mention to new visitors. I think wait-on is currently the best fitting answer to the title's question.
As far as I understand,
you'd like to run your local server, once the server is up tests cycle should be triggered.
I suggest to use the package "start-server-and-test" sounds suite for your solution, the NPM package page is here
Let's take your current package.json script object, and rewrite them.
The start and test scripts are the two basic scripts you need to maintain your app easily.
start - to start your app (I suggest to use nodemon or pm2)
test - call your test script
Notes:
To dev tests you will need to handle two terminals, each for the above.
I'm assuming you're running on port 8080
The package is also handling the termination of both processes (node and mocha) in both cases success and failure so no need (posttest:ci, --exit, etc..)
There is no need to use child process (the &) that mentioned at the end of your web-server package.json's script.
Here is the new script object, from my POV
"scripts": {
"start": "node app.js",
"test": "NODE_ENV=test mocha ./tests/functional/*.js",
"test:ci": "NODE_ENV=test start-server-and-test start \"http://localhost:8080\" test"
}
Now, from your CLI:
npm run test:ci
The ci suffix mentions this process is fully automated
It's expected that you'll have to define CI=true for a real CI environment,
just as all CI tools do and it's not necessary for local usage.