Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mocha.opts deprecated, how to migrate to package.json?

I'm working on a massive project and since last week I updated mocha, Now we are getting warning:

DeprecationWarning: Configuration via mocha.opts is DEPRECATED and will be removed from a future version of Mocha. Use RC files or package.json instead.

I want to migrate the options to package.json but there is no good migration guide. all posts on GitHub with similar questions are all answered "see the docs". But the docs doesn't show how to transfer one option from mocha.opts to package.json, there is no information on how it should be formatted. Only thing I can find is that the "spec" property is the pattern for files to run. Nothing else seems implicit to me.

Our mocha.opts file:

--reporter dot
--require test/mocha.main
--recursive src/**/*.test.js
--grep @slow --invert

My attempt which doesn't work:

  "mocha": {
    "reporter": "dot",
    "require": "test/mocha.main",
    "spec": "src/**/*.test.js",
    "grep": "@slow --invert"
  },

Please explain how I should format this configuration block in order to achieve samme behaviour as when using the options from the above mocha.opts

like image 674
Rasmus Puls Avatar asked Feb 18 '20 14:02

Rasmus Puls


1 Answers

I too had some difficulties finding the exact solution for migrating to new standards and could finally resolve those. I hope I'm not too late and I can still help you.

So first thing, you would need a new config file to replace mocha.opts. Mocha now offers quite some variations of file formats which can be used for it. You can find these here in their GIT. I took .mocharc.json and will be using it for further examples. Although adding it didn't change anything just the way it shows no effect for you too.

The catch was to point mocha test script to this config file in package.json. Provide --config flag in the test script in the scripts section in your package.json like below.

"scripts": {
    "test": "mocha --config=test/.mocharc.json --node-env=test --exit",
    "start": "node server"
}

Now you can update your configs in the .mocharc.json file and they should reflect correctly. Below is an example of it.

{
"diff": true,
"extension": ["js"],
"package": "../package.json",
"reporter": "spec",
"slow": 1500,
"timeout": 20000,
"recursive": true,
"file": ["test/utils/helpers.js", "test/utils/authorizer.js"],
"ui": "bdd",
"watch-files": ["lib/**/*.js", "test/**/*.js"],
"watch-ignore": ["lib/vendor"]
}

I'm using file property to define which files should go first as they need to be executed first. They will be executed in the order you provide them in the file array. Another property you can play around is slow whose value defines whether mocha consider the time taken to execute any test case as slow or not.

like image 165
Rathore Avatar answered Sep 21 '22 11:09

Rathore