How can I avoid "No test specified" errors in npm? - testing

I'm using npm#5.6.0 on Mac High Sierra. I want to run tests that were setup in this Stratum client project. I have run npm install successfully. But when I try and run individual tests, I get the error:
no test specified
What gives? I am in the root project directory and the tests are in my "test" folder. Here is what happens:
localhost:stratum-client-master davea$ npm install
up to date in 0.381s
localhost:stratum-client-master davea$ npm test test/callbacks.js
> stratum-client#1.0.1 test /Users/davea/Documents/workspace/stratum-client-master
> echo "Error: no test specified" && exit 1 "test/callbacks.js"
Error: no test specified
sh: line 0: exit: too many arguments
npm ERR! Test failed. See above for more details.

Try replacing
"scripts": {
"test": "mocha"
},
in your package.json.

You're outputting exactly what the package.json file was told to output. Take a peek under scripts.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"int-test": "mocha --require babel-core/register --recursive test"
},
Try int-test, the other command in there.
Update: The package link has changed to the following and mocha should be the default test suite. You can run the other script with npm run bump-version; the original script above can be run with npm run int-test.
"scripts": {
"test": "mocha --recursive test",
"bump-version": "npm version patch"
},

You didn't specify which testing framework you're using such as Jest or Mocha.
In case of Jest, add this in your package.json:
"scripts" : {
"test" : "jest"
}
In the case of mocha refer to #Divyeshpal's answer.

The error can be for "exit 1"
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Change it to this:
"scripts": {
"test": "echo \"No test specified\""
},
This change works because exit 1 creates an error.

if you are using Jest and enzyme for unit testing and you have a jest.config.json config file, you can update your package.json under scripts to the following:
"scripts":{
"test": "jest --config=./jest.config.json --coverage",
}

The error can be for "exit 1"
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Change it to this:
"scripts": {
"test": "echo \"No test specified\""
},
This change worked for me because exit 1 created the error.

find and make changes in your package.json
"scripts":{
"test":"mocha"
}

add this on your packages.json file:
"scripts":{
"test": "mocha --ui tdd tests/callbacks.js",
}
then npm test on the console.

Replace
echo \"Error: no test specified\" && exit 1
in your
package.json
with
mocha.

Related

How to generate a coverage report with vscode extension tests

I'm implementing a VSCode extension. I set up the project following this link.
It generates a starter project with a src/test/runTest.ts file:
import * as path from 'path';
import { runTests } from '#vscode/test-electron';
async function main() {
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, '../../');
// The path to test runner
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './suite/index');
// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath });
} catch (err) {
console.error('Failed to run tests');
process.exit(1);
}
}
main();
And a command in the package.json:
{
"compile": "tsc -p ./",
"pretest": "npm run compile && npm run lint",
"lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js"
}
Is there a way to generate a coverage report with it?
VSCode extension unittesting uses Mocha under the hood. You can generate coverage reports like in any other Typescript/Javascript project using one of the many available frameworks, e.g. c8, jest, istanbul, etc.
Install the framework of your choice, here I use c8
npm i --save-dev c8
and add to scripts
"scripts": {
"compile": "tsc -p ./",
"pretest": "npm run compile && npm run lint",
"lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js",
"coverage": "c8 --check-coverage npm run test"
}
Depending on your extension you might need to create a configuration file with the files you want to check for coverage. Here we are checking the compiled .js files placed under the out/ dir and we also excluding the files responsible for unittesting i.e. out/test/ (usually).
.c8rc
{
"all": true,
"include": ["out/**"],
"exclude": ["**/node_modules/**", "out/test/"],
"reporter": ["html", "text"]
}
Run the coverage script and you should get an output of your coverage
npm run coverage

Generate html coverage report in Adonis

Good afternoon, how are you guys?
Thanking you already
I’m new yet not adonis and following this topic to generate a test report coverage file for this project
project ->https://github.com/Yuri-Tiofilo/adonisTdd 1, and adding the file .nycrc to the project’s root folder and running the adonis test command it doesn’t generate this report.
is there any configuration?
To generate the reports goto to your package.json file where you have
"scripts": {
"start": "node server.js",
"test": "node ace test"
},
replace it with
"scripts": {
"start": "node server.js",
"test": "nyc node ace test"
},
Cool, thank you very much, #Onwubiko Chibuike, make and replace and add nyc modules and it worked, I will leave the steps here, for future interested
1. install module nyc
yarn add nyc or npm i nyc
2º - replace package.json
"scripts": { "start": "node server.js", "test": "nyc node ace test" },
3º - create file in base file project .nycrc
{
"all": true,
"include": ["app/**"],
"reporter": "html",
"report-dir": "test/coverage"
}
4º - run comand
npm test work's for me, Thx.

npm - Pass arguments from CLI through multiple scripts

Let's say I have the following in a file called print-last-arg.js:
console.log(process.argv[process.argv.length-1])
And the following scripts in my package.json:
"scripts": {
"print_a": "node print-last-arg.js",
"print_b": "npm run print_a"
}
When I run npm run print_a -- --foo=bar, I get --foo=bar as expected.
However, npm run print_b -- --foo=bar gives me no output.
How do I pass the CLI arguments from print_b to print_a?
It turns out that you just have to add an extra -- on the end of print_b, which will tell npm to pass whatever arguments print_b got to print_a. So,
"scripts": {
"print_a": "node print-last-arg.js",
"print_b": "npm run print_a"
}
becomes
"scripts": {
"print_a": "node print-last-arg.js",
"print_b": "npm run print_a -- "
}
Voilà! Now npm run print_b -- --foo=bar prints --foo=bar as expected.

Running eslint command every time you save

I have my package.json script set up like this:
"scripts": {
"lint": "eslint src webpack.config.js || exit 0"
},
How can I have this lint command run every time I save a file so I don't have to run npm run lint every single time?
eslint-watch looks like it would do this:
"scripts": {
"lint-watch": "esw --watch src webpack.config.js"
}

How can I pass parameters from a npm script to another?

I am trying to pass the parameters from a npm script to another (that is called by the first one), but I absolutely can't figure out how to do it.
Here is the case, I have the following scripts section in my package.json :
{
"scripts": {
"one": "npm run two && npm run three",
"two": "gulp build",
"three": "another random command"
}
}
I'm running script one like this : npm run one -- --arg=value. But I want to dynamically pass down arg to the script two.
To sum up, what I want is :
I type npm run one -- --arg=value
It runs npm run two -- --arg=value && npm run three
Which results in running gulp build --arg=value, followed by the other random command
Does anybody have an idea ? Thank you very much.
All three methods below work with npm run one -- --arg=value.
1. Using pre & post scripts
You can run the second script directly and define the third one as a post script.
{
"scripts": {
"one": "npm run two --",
"postone": "npm run three",
"two": "gulp build",
"three": "another random command"
}
2. Using npm-run-all
Pass argument placeholders to individual scripts.
"scripts": {
"one": "run-s 'two -- {#}' three --",
"postone": "npm run three",
"two": "gulp build",
"three": "another random command"
}
3. Using concurrently
Change the first script to run everything with concurrently, instructing that the second script should get pass through arguments.
{
"scripts": {
"one": "concurrently -P 'npm run two -- {#} && npm run three' --",
"two": "gulp build",
"three": "another random command"
}
}
Since we are passing only one command to concurrently, there is no concurrency.