Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Location is not working as expected for --config #3818

Closed
4 tasks
AartiJ opened this issue Mar 8, 2019 · 38 comments · Fixed by #3829
Closed
4 tasks

Custom Location is not working as expected for --config #3818

AartiJ opened this issue Mar 8, 2019 · 38 comments · Fixed by #3829
Assignees
Labels
status: waiting for author waiting on response from OP - more information needed

Comments

@AartiJ
Copy link

AartiJ commented Mar 8, 2019

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

Steps to Reproduce

Expected behavior: [What you expect to happen]
config file should load from the given custom location

Actual behavior: [What actually happens]
Getting issue as below

mocha --no-opts --config node_modules/ace-api-testlib/lib/.mocharc.js --reporter mocha-allure-reporter "

/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/lib/cli/config.js:63
throw new Error(failed to parse ${filepath}: ${err});
^

Error: failed to parse node_modules/ace-api-testlib/lib/.mocharc.js: Error: Cannot find module 'node_modules/ace-api-testlib/lib/.mocharc.js'
at exports.loadConfig.filepath (/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/lib/cli/config.js:63:11)
at loadRc (/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/lib/cli/options.js:229:21)
at loadOptions (/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/lib/cli/options.js:299:20)
at Object. (/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/bin/mocha:29:14)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
npm ERR! code ELIFECYCLE

Reproduces how often: [What percentage of the time does it reproduce?]

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version:
    % mocha --version
    6.0.2

  • The output of node --version:
    % node --version
    v8.11.1

  • Your operating system

    • name and version: macOSHighSierra 10.13.6
    • architecture (32 or 64-bit):
  • Your shell (e.g., bash, zsh, PowerShell, cmd): bash

  • Your browser and version (if running browser tests):

  • Any third-party Mocha-related modules (and their versions):

  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version):

Additional Information

@AartiJ
Copy link
Author

AartiJ commented Mar 8, 2019

my .mocharc.js configuration file is in another node artifact which I am basically using in this e2e project.

@juergba
Copy link
Member

juergba commented Mar 8, 2019

Is the path node_modules/../lib/.mocharc.js correct? Try to use an absolute one instead.

@AartiJ
Copy link
Author

AartiJ commented Mar 8, 2019

yes, the path is correct and was using absolute path. I have updated the description above.

@juergba
Copy link
Member

juergba commented Mar 8, 2019

Is node_modules/../lib/.mocharc.js not identical with lib/.mocharc.js ?

I'm quite sure (I don't know macOS) that node_modules/ace-api-testlib/lib/.mocharc.js is not an absolute path. If your path is a relative one, try: ./node_modules/ace-api-testlib/lib/.mocharc.js

@AartiJ
Copy link
Author

AartiJ commented Mar 8, 2019

tried your suggestion and still getting the same issue.

mocha --no-opts --config ./node_modules/ace-api-testlib/lib/.mocharc.js --reporter mocha-allure-reporter

/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/lib/cli/config.js:63
throw new Error(failed to parse ${filepath}: ${err});
^

Error: failed to parse ./node_modules/ace-api-testlib/lib/.mocharc.js: Error: Cannot find module './node_modules/ace-api-testlib/lib/.mocharc.js'
at exports.loadConfig.filepath (/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/lib/cli/config.js:63:11)
at loadRc (/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/lib/cli/options.js:229:21)
at loadOptions (/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/lib/cli/options.js:299:20)
at Object. (/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/bin/mocha:29:14)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
npm ERR! code ELIFECYCLE

@plroebuck
Copy link
Contributor

plroebuck commented Mar 8, 2019

Adding "./" to the beginning will have no effect (doesn't hurt or help).

Run this from same location as your command above.

$ pwd
$ ls -al node_modules/ace-api-testlib/lib/.mocharc.js

Relatively certain the ls command will fail though...

List the possibilities...

$ find node_modules -name ".mocharc*"

@plroebuck
Copy link
Contributor

yes, the path is correct and was using absolute path. I have updated the description above.

The path above is relative. Absolute paths on UN*X begin with a slash character '/' (i.e., the path from the root directory).

@plroebuck plroebuck added the status: waiting for author waiting on response from OP - more information needed label Mar 9, 2019
@AartiJ
Copy link
Author

AartiJ commented Mar 9, 2019

@plroebuck I got the below output for the commands you requested to execute
C02Q383EG8WP:ace-ilp-lib ajadeja$ ls -al node_modules/ace-api-testlib/lib/.mocharc.js
-rw-r--r-- 1 ajadeja 1849915958 530 Mar 7 16:31 node_modules/ace-api-testlib/lib/.mocharc.js
C02Q383EG8WP:ace-ilp-lib ajadeja$ find node_modules -name ".mocharc*"
node_modules/ace-api-testlib/lib/.mocharc.js

@plroebuck
Copy link
Contributor

you left out the most important -- pwd

@AartiJ
Copy link
Author

AartiJ commented Mar 9, 2019

@plroebuck Sorry here it is
C02Q383EG8WP:ace-ilp-lib ajadeja$ pwd
/Users/ajadeja/git_test/ace-ilp-lib
C02Q383EG8WP:ace-ilp-lib ajadeja$ ls -al node_modules/ace-api-testlib/lib/.mocharc.js
-rw-r--r-- 1 ajadeja 1849915958 530 Mar 7 16:31 node_modules/ace-api-testlib/lib/.mocharc.js
C02Q383EG8WP:ace-ilp-lib ajadeja$ find node_modules -name ".mocharc*"
node_modules/ace-api-testlib/lib/.mocharc.js
C02Q383EG8WP:ace-ilp-lib ajadeja$

Just FYI: I have my project open in Webstorm, and I am running an npm command from Webstorm Project Terminal

@plroebuck
Copy link
Contributor

plroebuck commented Mar 9, 2019

$ which mocha
$ cat package.json
$ cat node_modules/ace-api-testlib/lib/.mocharc.js

@AartiJ
Copy link
Author

AartiJ commented Mar 9, 2019

@plroebuck C02Q383EG8WP:ace-ilp-lib ajadeja$ which mocha
/Users/ajadeja/.nvm/versions/node/v8.11.1/bin/mocha
C02Q383EG8WP:ace-ilp-lib ajadeja$ cat package.json
{
"name": "ace-ilp-lib",
"version": "1.0.9",
"description": "Provides basic test framework support, including session management, logging, test data management",
"main": "index.js",
"dependencies": {
"ace-api-testlib": "1.0.7",
"mocha": "^6.0.2",
"mocha-allure-reporter": "^1.4.0"
},
"files": [
"lib",
"test"
],
"directories": {
"lib": "lib"
},
"scripts": {
"test": "mocha --no-opts --config ./node_modules/ace-api-testlib/lib/.mocharc.js --reporter mocha-allure-reporter",
"report": "allure generate target/allure-results --clean -o allure-report"
},
"repository": {
"type": "git",
"url": "https://stash.intralinks.com/scm/ace/ace-ilp-lib.git"
},
"author": "ace-api",
"license": "ISC"
}
C02Q383EG8WP:ace-ilp-lib ajadeja$ cat node_modules/ace-api-testlib/lib/.mocharc.js
'use strict';
const path = require('path');

let allureResultsOutdir = process.env.OUTDIR ? path.join(process.env.OUTDIR, 'allure-results') : 'target/allure-results';
// Here's a JavaScript-based config file.
// If you need conditional logic, you might want to use this type of config.
// Otherwise, JSON or YAML is recommended.

module.exports = {
timeout: 50000,
file: './node_modules/ace-api-testlib/lib/mocha.global.js',
reporter: 'mocha-allure-reporter',
reporterOption: [ 'targetDir='+allureResultsOutdir]
}
C02Q383EG8WP:ace-ilp-lib ajadeja$

@plroebuck
Copy link
Contributor

Quick test with absolute path...

"scripts": {
"test": "mocha --no-opts --config '/Users/ajadeja/git_test/ace-ilp-lib/node_modules/ace-api-testlib/lib/.mocharc.js' --reporter mocha-allure-reporter",
"report": "allure generate target/allure-results --clean -o allure-report"
},
$ npm test

@plroebuck
Copy link
Contributor

Does "ace-ilp-lib" contain a Mocha config file you're trying to avoid?

@AartiJ
Copy link
Author

AartiJ commented Mar 9, 2019

Does "ace-ilp-lib" contain a Mocha config file you're trying to avoid?

No but if I put in ace-ilp-lib it works since that's the default path

@AartiJ
Copy link
Author

AartiJ commented Mar 9, 2019

Quick test with absolute path...

"scripts": {
"test": "mocha --no-opts --config '/Users/ajadeja/git_test/ace-ilp-lib/node_modules/ace-api-testlib/lib/.mocharc.js' --reporter mocha-allure-reporter",
"report": "allure generate target/allure-results --clean -o allure-report"
},
$ npm test

Yes this one works, but I can not give this path, as location of ace-ilp-lib is end users' choice

@plroebuck
Copy link
Contributor

Works means it ran your tests as expected, or it didn't immediately blow up?

@AartiJ
Copy link
Author

AartiJ commented Mar 9, 2019

Works means it ran your tests as expected, or it didn't immediately blow up?

it ran tests as expected

@plroebuck
Copy link
Contributor

plroebuck commented Mar 9, 2019

Out of curiosity, why are you putting the Mocha config files in the "lib" directory to begin with?
Does "ace-api-testlib" have a "test" directory?
Does "ace-ilp-lib" have a "test" directory?
What is the "git_test" directory for in this case?

@AartiJ
Copy link
Author

AartiJ commented Mar 9, 2019

Out of curiosity, why are you putting the Mocha config files in the "lib" directory to begin with?
Does "ace-api-testlib" have a "test" directory?
this is our inhouse framework which is being used by e2e test Projects. So basically config file should come from this so that it will be consistent across all e2e projects and also with this end user do not need to add config file. It has test directory but only unit tests.
Does "ace-ilp-lib" have a "test" directory?
Yes, it does since this is one of the e2e Project

@AartiJ
Copy link
Author

AartiJ commented Mar 9, 2019

@plroebuck that's fine, I know now that we should give absolute path here. I will try to find other ways for my requirement. Thanks for your help

@AartiJ AartiJ closed this as completed Mar 9, 2019
@plroebuck
Copy link
Contributor

plroebuck commented Mar 9, 2019

So:

  • "ace-api-testlib" package has a "test" directory with unit tests.
  • "ace-ilp-lib" package has a "test" directory with e2e tests

Still kinda lost on why put the Mocha config file in the "lib" directory.
Why not "ace-api-testlib/.mocharc.js" where it would found by design?

@AartiJ
Copy link
Author

AartiJ commented Mar 9, 2019

So:

  • "ace-api-testlib" package has a "test" directory with unit tests.
  • "ace-ilp-lib" package has a "test" directory with e2e tests

Still kinda lost on why put the Mocha config file in the "lib" directory.
Why not "ace-api-testlib/.mocharc.js" where it would found by design?

oh will it? I tried putting in ace-api-testlib/.mocharc.js and it was not working. If I keep in ace-api-testlib/.mocharc.js what will be my path for --config file?

@plroebuck
Copy link
Contributor

plroebuck commented Mar 9, 2019

Some of my concern is also that "node_modules/ace-api-testlib/lib/.mocharc.js" turns around and module.exports YA relative path for "file"...

@plroebuck
Copy link
Contributor

plroebuck commented Mar 9, 2019

Suggest:

$ cat package.json
{
  "name": "ace-ilp-lib",
  "version": "1.0.9",
  "description": "Provides basic test framework support, including session management, logging, test data management",
  "main": "index.js",
  "dependencies": {
    "ace-api-testlib": "~1.0.7"
  },
  "devDependencies": {
    "allure-commandline": "^2.9.0",
    "mocha": "^6.0.2",
    "mocha-allure-reporter": "^1.4.0"
  },
  "files": [
    "lib",
    "test"
  ],
  "directories": {
    "lib": "lib",
    "test": "test"
  },
  "scripts": {
    "pretest": "rm -rf allure-results",
    "pwd": "pwd",
    "report": "allure generate target/allure-results --clean -o allure-report && allure open allure-report",
    "test": "mocha --config ./node_modules/ace-api-testlib/.mocharc.js"
  },
  "repository": {
    "type": "git",
    "url": "https://stash.intralinks.com/scm/ace/ace-ilp-lib.git"
  },
  "author": "ace-api",
  "license": "ISC"
}

@AartiJ
Copy link
Author

AartiJ commented Mar 9, 2019

Tried the suggestion which gives same as original error

mocha --config ./node_modules/ace-api-testlib/.mocharc.js "--sut=dev2" "--spec=test/V2APITest"

/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/lib/cli/config.js:63
throw new Error(failed to parse ${filepath}: ${err});
^

Error: failed to parse ./node_modules/ace-api-testlib/.mocharc.js: Error: Cannot find module './node_modules/ace-api-testlib/.mocharc.js'
at exports.loadConfig.filepath (/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/lib/cli/config.js:63:11)
at loadRc (/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/lib/cli/options.js:229:21)
at loadOptions (/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/lib/cli/options.js:299:20)
at Object. (/Users/ajadeja/git_test/ace-ilp-lib/node_modules/mocha/bin/mocha:29:14)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
npm ERR! code ELIFECYCLE
npm ERR! errno 1

@AartiJ AartiJ reopened this Mar 9, 2019
@plroebuck
Copy link
Contributor

plroebuck commented Mar 9, 2019

$ cat node_modules/ace-api-testlib/.mocharc.js

'use strict';
const path = require('path');
const FIFTY_SECS = 50000;
const allureArtifactsDir = process.env.OUTDIR
  ? path.join(process.env.OUTDIR, 'allure-results')
  : 'target/allure-results';

module.exports = {
  timeout: FIFTY_SECS,
  file: path.resolve('./node_modules/ace-api-testlib/test/mocha.global.js'),
  opts: false,
  reporter: 'mocha-allure-reporter',
  reporterOption: [ 'targetDir=' + allureArtifactsDir ]
};

@AartiJ
Copy link
Author

AartiJ commented Mar 9, 2019

$ cat node_modules/ace-api-testlib/.mocharc.js

'use strict';
const path = require('path');
const FIFTY_SECS = 50000;
const allureArtifactsDir = process.env.OUTDIR
  ? path.join(process.env.OUTDIR, 'allure-results')
  : 'target/allure-results';

module.exports = {
  timeout: FIFTY_SECS,
  file: path.resolve('./node_modules/ace-api-testlib/test/mocha.global.js'),
  opts: false,
  reporter: 'mocha-allure-reporter',
  reporterOption: [ 'targetDir=' + allureArtifactsDir ]
}

gives same error as given above

@plroebuck
Copy link
Contributor

plroebuck commented Mar 9, 2019

What's in "./node_modules/ace-api-testlib/mocha.global.js"?

@AartiJ
Copy link
Author

AartiJ commented Mar 9, 2019

What's in './node_modules/ace-api-testlib/mocha.global.js'?

global hooks configuration for e2e tests

@plroebuck
Copy link
Contributor

You have sudo capability? Rename your global "mocha" and get it out of the way...

@AartiJ
Copy link
Author

AartiJ commented Mar 9, 2019

I deleted the global mocha, still gives same error

@plroebuck
Copy link
Contributor

$ cat node_modules/ace-api-testlib/package.json | grep version

@AartiJ
Copy link
Author

AartiJ commented Mar 9, 2019

$ cat node_modules/ace-api-testlib/package.json | grep version

C02Q383EG8WP:ace-ilp-lib ajadeja$ cat node_modules/ace-api-testlib/package.json | grep version
"type": "version",
"docs": "cross-var "rm -rf ./docs/latest ./docs/$npm_package_version && jsdoc -c ./conf.json -R ./README.md --access all ./lib && mv -f docs/js docs/$npm_package_version && ln -s -F $npm_package_version ./docs/latest"",
"docs-tar": "cross-var tar -C docs -zcvf $npm_package_name-$npm_package_version-doc.tgz $npm_package_version latest",
"version": "1.0.7"
C02Q383EG8WP:ace-ilp-lib ajadeja$

@plroebuck
Copy link
Contributor

Figured out your issue... I think anyway.
Here's the relevant code from "lib/cli/config.js":

const parsers = (exports.parsers = {
  yaml: filepath =>
    require('js-yaml').safeLoad(fs.readFileSync(filepath, 'utf8')),
  js: filepath => require(filepath),
  json: filepath =>
    JSON.parse(
      require('strip-json-comments')(fs.readFileSync(filepath, 'utf8'))
    )
});

The other parsers are reading the config file directly, so relative path is straightforward. But your JS config file is handled by require which uses these rules instead.

So rather than:

  "scripts": {
    "test": "mocha --config ./node_modules/ace-api-testlib/.mocharc.js"
  },

instead use the even shorter:

  "scripts": {
    "test": "mocha --config ace-api-testlib/.mocharc.js"
  },

Used this recipe for testing. There were additional changes needed to get your setup (as I understand it anyway) to work.

@AartiJ
Copy link
Author

AartiJ commented Mar 10, 2019

Figured out your issue... I think anyway.
Here's the relevant code from "lib/cli/config.js":

const parsers = (exports.parsers = {
  yaml: filepath =>
    require('js-yaml').safeLoad(fs.readFileSync(filepath, 'utf8')),
  js: filepath => require(filepath),
  json: filepath =>
    JSON.parse(
      require('strip-json-comments')(fs.readFileSync(filepath, 'utf8'))
    )
});

The other parsers are reading the config file directly, so relative path is straightforward. But your JS config file is handled by require which uses these rules instead.

So rather than:

  "scripts": {
    "test": "mocha --config ./node_modules/ace-api-testlib/.mocharc.js"
  },

instead use the even shorter:

  "scripts": {
    "test": "mocha --config ace-api-testlib/.mocharc.js"
  },

Used this recipe for testing. There were additional changes needed to get your setup (as I understand it anyway) to work.

Yes this works as expected.. thanks

@plroebuck
Copy link
Contributor

Everything working now? Hope recipe helped. If no further questions, please close the issue. Thx

@AartiJ
Copy link
Author

AartiJ commented Mar 11, 2019

Everything working now? Hope recipe helped. If no further questions, please close the issue. Thx

Yes, recipe helped. Everything works now. Closing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for author waiting on response from OP - more information needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants