I want to ask the community if they have ever seen this issue before.
when running a simple "npm t" command, I am getting the following error
GTaylor#slayer MINGW64 /d/slayer/packages/components (feature/HUBBLU-54)
$ npm t
> fl-components#1.0.0 test D:\slayer\packages\components
> jest --config=jest.json
s" --config=jest.json was unexpected at this time.
D:\slayer\packages\components>)s" --config=jest.json
npm ERR! Test failed. See above for more details.
the script is
"scripts": {
"build": "tsc -d",
"test": "jest --config=jest.json"
}
If I run the command directly this work. I also have other project (in another solution) that works.
This is a Lerna project with multiple packages and they all suffer from the same issue. Note that this was working on one point.
I have no idea where the "s" is coming from. It feels like it trying to run a following command .. but where the hell is the "s" and new lines coming from????
"jest --config=jest.json\n\ns -config=jest.json"
please help before I go mad.... :)
Related
I have two types of test suites - normal and coverage.
At present, I am allowing one of them via npm test and one of them via npm start:
"scripts": {
"test": "node scripts/run-truffle-tests.js",
"start": "node scripts/run-sol-coverage.js"
}
I have a feeling that npm start was not originally designated for this purpose.
Is there a better way to implement this?
I was thinking of passing an argument to npm test, but I'm not sure that npm would pass it on to the script which it is set to invoke.
Add more scripts.
I usually have the tests for actual, full, single run unit tests to work with CI and other scripts for variations:
{
"scripts": {
"test": "node scripts/run-truffle-tests.js && npm run test:coverage",
"test:continuous": "karma start some.config --run-continuous",
"test:coverage": "node scripts/run-sol-coverage.js"
"start": "node index.js"
}
}
You can also chain commands with && which will cause the script to be run in sequence and the "total" error code will propagate. In other words, using the test I have above, will run both the unit test and the coverage test. If either of them return a non-zero exit code, npm will consider the whole test process to have failed.
Bear in mind that for custom scripts not named exactly start and test as well as the other designated scripts found in the docs here: npm#scripts, must be run with
npm run scriptname
instead of just
npm scriptname
So in my above example, you would test coverage with:
npm run test:coverage
Also, the : is just a convention. As far as I know it's not special.
Additionally, you can
pass [argument] on to the script which it is set to invoke
Whenever use use npm test, what is basically happening is that npm runs whatever String value is set in the package.json's scripts.test as a process, as though you had typed that String into the shell yourself. It then looks at the return code and if it's 0, it reports as everything being ok; if it's non-zero, it prints an error.
I'm writing an app that is composed of microservices (I use micro).
I really like es6, so I use Babel to make the development process easier. The problem that I have is that I need a script that would compile my es6 code and restarted the 'server'; I don't know how to achieve this.
Right now I have the following script in my package.json:
"scripts": {
"start": "yarn run build && micro",
"build": "./node_modules/.bin/babel src --out-dir lib"
},
When I run yarn start my es6 code compiles successfully and micro starts the server. However, if I make changes to my code, I'll have to manually stop the server and run yarn start again.
I've tried to change my build script
"build": "./node_modules/.bin/babel src --watch --out-dir lib"
But in this case the micro command does not get executed as the build script just watches for changes and blocks anything else from execution. My goal is to have a script that would watch for changes and restart the server if a change occurred (compiling the code beforehand) like in Meteor.
One option is using ParallelShell module to run shell commands in parallel. You can find an example of how to use it here
The simplest solution would be to yarn run build & micro (note the single & and not &&).
As mentioned by others, parallelshell is another good hack (probably more robust than &).
This is really weird, I have the following scripts in my package.json:
"scripts": {
"lint": "./node_modules/tslint/bin/tslint src/js/**/*",
"lint:fix": "./node_modules/tslint/bin/tslint src/js/**/* --fix"
},
When I run npm run lint I don't get any errors and running echo $? immediately after shows 0.
However, if I run tslint src/js/**/* I do get linting errors.
How come?
There are a host of well-known issues in npm arising from the use of globbing. Many of them exclusively impact Windows, while others are "merely" shell-specific.
Try the following.
"scripts": {
"lint": "./node_modules/tslint/bin/tslint \"src/**/*.ts\"",
},
If this didn't immediately persuade you that computers have been a disaster for the human race, you can learn more about why these issues occur in the fantastic The Linux Programming Interface, which covers a surprising number non-Linux portability issues such as this one.
using npm run lint defaults to the tslint in the node_modules of your local project directory, while using tslint src/js/**/* defaults to the the one in your global, you should check if there is a version mismatch which could cause differences in rules
With npm-run-scripts you can omit the ./node_modules/.bin as npm will first look in there.
I have an app (https://github.com/idmillington/dendry) that uses Travis CI to monitor build status. I use Istanbul to general a coverage report, and I'd like to send this to Coveralls, to generate a coverage button for the README.
All of this I can get working. But...
When I run npm test locally, I don't want to send coveralls the coverage data. I'm typically running npm test dozens of times for each commit. But when I push and Travis does its thing, I'd like Travis to update the coverage for me.
I could have something like this in my package.json:
"scripts": {
"test": "./node_modules/.bin/istanbul test ./node_modules/.bin/_mocha",
}
Which is fine for locally, and doesn't update Coveralls, but Travis won't update Coveralls either. Or I could do:
"scripts": {
"test": "./node_modules/.bin/istanbul test ./node_modules/.bin/_mocha && ./node_modules/coveralls/bin/coveralls.js < ./coverage/lcov.info",
}
Which is perfect for Travis, but tries to push data to Coveralls every time I run npm test locally.
As far as I can tell, I can't ask Travis to run something other than npm test.
I am unwilling to ask any potential users or contributors to remember to test using
$ npm run-script test-local
or some such, especially as running npm test would generate an upload error without the correct private key for coveralls.
Is there a way to get the right behavior here?
The answer, as it turns out, was frighteningly simple. Travis does allow you to call whatever script you like as it runs, so I added this to my .travis.yml file:
script: npm run-script test-on-travis
so in package.json I could define:
"scripts": {
"test": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha",
"test-on-travis": "./node_modules/.bin/istanbul cover --report lcovonly ./node_modules/.bin/_mocha && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
}
and everything works fine.
I am using Mocha for testing my NodeJS application. I am not able to figure out how to use its code coverage feature. I tried googling it but did not find any proper tutorial. Please help.
You need an additional library for code coverage, and you are going to be blown away by how powerful and easy istanbul is. Try the following, after you get your mocha tests to pass:
npm install nyc
Now, simply place the command nyc in front of your existing test command, for example:
{
"scripts": {
"test": "nyc mocha"
}
}
Now (2023) the preferred way to use istanbul is via its "state of the art command line interface" nyc.
Setup
First, install it in your project with
npm i nyc --save-dev
Then, if you have a npm based project, just change the test script inside the scripts object of your package.json file to execute code coverage of your mocha tests:
{
"scripts": {
"test": "nyc --reporter=text mocha"
}
}
Run
Now run your tests
npm test
and you will see a table like this in your console, just after your tests output:
Customization
Html report
Just use
nyc --reporter=html
instead of text. Now it will produce a report inside ./coverage/index.html.
Report formats
Istanbul supports a wide range of report formats. Just look at its reports library to find the most useful for you.
Just add a --reporter=REPORTER_NAME option for each format you want.
For example, with
nyc --reporter=html --reporter=text
you will have both the console and the html report.
Don't run coverage with npm test
Just add another script in your package.json and leave the test script with only your test runner (e.g. mocha):
{
"scripts": {
"test": "mocha",
"test-with-coverage": "nyc --reporter=text mocha"
}
}
Now run this custom script
npm run test-with-coverage
to run tests with code coverage.
Force test failing if code coverage is low
Fail if the total code coverage is below 90%:
nyc --check-coverage --lines 90
Fail if the code coverage of at least one file is below 90%:
nyc --check-coverage --lines 90 --per-file
Blanket.js works perfect too.
npm install --save-dev blanket
in front of your test/tests.js
require('blanket')({
pattern: function (filename) {
return !/node_modules/.test(filename);
}
});
run mocha -R html-cov > coverage.html
The accepted answer (nyc) does not work if you are using ESM modules.
C8 appears to be the best solution now, which leverages built-in NodeJS capabilities and utilizes istanbul (like nyc, and shares the same config files).
npm install -g c8
c8 mocha
It will use .nycrc for configuration. A sample configuration I'm using is:
{
"all": true,
"exclude": ["test"],
"output": "reports",
"reporter" : [
"html",
"text"
]
}
(Note: I was pointed to c8 by an answer to another question https://stackoverflow.com/a/69846825/1949430)