I have the below test case.
describe("Test 1", function() {
var data;
function execute(data) {
before('should create a review', function () {
res = call.post(testConfig.APP_URL + testConfig.CREATE_REVIEW, JSON.parse(data));
return expect(res).to.have.json(function(json) {
reviewid = json.reviewid;
console.log("API Response ----> " + JSON.stringify(json));
});
});
it('should return 201 in @success', function() {
return expect(res).to.have.status(201);
});
}
});
I run it using mocha -g 'Test 1' but I get the error.
/usr/local/lib/node_modules/mocha/lib/utils.js:652
throw new Error("cannot resolve path (or pattern) '" + path + "'");
^
Error: cannot resolve path (or pattern) 'test'
at Object.lookupFiles (/usr/local/lib/node_modules/mocha/lib/utils.js:652:15)
at /usr/local/lib/node_modules/mocha/bin/_mocha:326:30
at Array.forEach (native)
at Object.<anonymous> (/usr/local/lib/node_modules/mocha/bin/_mocha:325:6)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
Can anyone help please?
Thanks,
Kunal
You can use describe.only or it.only to run specific tests.
Say there are 5 test cases in a file with describe. Is there a way to run just 1 test case out of those 5 ?
Yeah, you can use .only on one of the its rather than on the describe, or you can -g/--grep for the description of the test (or the description of its suite + " " + the description of the test, e.g. --grep="Test 1 should return 201 in @success" to use the test shown here).
To the original issue: It sounds like Mocha is looking for a file named "test" or "test.js" and not finding the file, although I am having trouble matching the particular error and stacktrace to the current code to tell exactly how this is happening. What version of Mocha are you using? (If you have both a local and a global install, make sure to check both.) Do you have a mocha.opts file, and if so what is in it? And what is the relative path from the root of your project to the test file(s)?
Tangentially: once you do get Mocha to find that test file, it won't actually run that test, as the before and it calls are inside the execute function and there are no calls to execute.
Looks like the error message you might get if your test files are not in the location mocha expects them to be by default, the test folder. Are your tests located somewhere else?
@Munter Yes my tests are located in a folder other that test folder. Is there a way to fix this ?
@ksachdeva11 If no files are specified as command line argument mocha will default to look for files in /test/: https://github.com/mochajs/mocha/blob/master/bin/_mocha#L354-L360
You need to specify your files on the command line in order for mocha to pick them up correctly: mocha path/to/file.spec.js
You can of course use wildcards here, and you can specify that pattern in test/mocha.opts, which is where mocha will look for default configuration
@Munter what is the path to mocha.opts ?
How to override from terminal pattern defined in mocha.opts?
For example, I have a mocha.opts:
dist/test/**/*.test.js
...so by default I run all tests from the dist/test folder with the .test.js suffix. But when I work I need to run only a single test, like dist/test/**/client.test.js. Is there a way to pass it as an argument without editing mocha.opts every time?
I would recommend moving the default file glob from mocha.opts to the test script in package.json -- then if you want to run all tests you npm test but if you want to run one you mocha <specific file>. (Any files that need to run no matter which test file is run should remain in mocha.opts, of course.)
I struggled with the problem of running single/multi tests from cmd if they are not located inside "./test" folder. IMHO, the docs are not entirely helpful. In case, anyone else wants to run multi tests AND single tests in package.json, you may find this solution helpful:
"test": "cross-env NODE_ENV=test mocha \"./src/**/*.test.js\" --require babel-core/register",
"test-single": "cross-env NODE_ENV=test mocha --require babel-core/register",
Use like this in cmd:
Run all tests: $ npm test
Run single test: $ npm run test-single .\src\myTest.test.js
Side note: I am using "cross-env" package for Windows/Linux environments, but this is not the point here. mocha.opts is not needed.
````
// ./test/sometest.test.ts file
describe('module', () => {
it('something', () => {
expect(2).equal(2);
})
})
describe('module test 2', () => {
it('another one ', () => {
expect(2).equal(2);
})
})
// package.json
"scripts":{
"test": "mocha --exit -r ts-node/register -r tsconfig-paths/register ./test/*/.test.ts --grep--",
}
// command
npm run test module
````
This is working fine for me.
when I put a space after --grep, it returns Not enough argument ERROR.
./test/**/*.test.ts --grep --
./test/**/*.test.ts --grep--
@aemrebasus If you want to report a new bug, please open a new issue. However, don't do it for this one, It's not a bug
See the POSIX Utility Syntax Guidelines:
Guideline 10:
The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the '-' character.
Most helpful comment
You can use describe.only or it.only to run specific tests.