mocha.opts deprecated, how to migrate to package.json?
Asked Answered
M

7

23

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

Maryannemarybella answered 18/2, 2020 at 14:30 Comment(2)
hello, did you ever find the solution or get any traction?Neglect
Nope, I'm still patiently awaiting. I'm frequently checking this post, and looking at the mocha docs for any updates. I promise you that I will write a detailed answer here as soon as I know how to migrate successfully ;)Maryannemarybella
P
12

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.

Proteus answered 20/5, 2020 at 13:23 Comment(0)
V
10

Check out this link to see the new format of the options file for mocha:

https://github.com/mochajs/mocha/blob/master/example/config/.mocharc.yml

Basically you need a .mocharc.yml, or .mocharc.json, (there are a couple more formats) to set the mocha configurations. I came to this POST hoping to find an answer too. Hope this is helpful for you!

Ventriloquize answered 10/3, 2020 at 17:48 Comment(2)
Thanks for posting this. But after creating a .mocharc.json where my mocha.opts was I can confirm that zero of those options is being applied.Maryannemarybella
Try adding --config=<config-file-location to your command e.g. my full command in my package.json under scripts is "test": "mocha -r ts-node/register --config=test/.mocharc.json 'test/**/*.ts' --exit"Braxton
M
6

I ended up getting the package.json working by using an array instead of the string literals you did. ex:

"mocha": {
  "require": ["tsconfig/register"]
}

Might be worth a try!

Marinmarina answered 24/7, 2020 at 2:3 Comment(0)
T
3

Seems like mocha won't check the package.json for config by default so you need to pass --package package.json.

enter image description here

Toting answered 7/4, 2020 at 6:48 Comment(1)
Aha, interesting. So I thought my config in package.json was being applied as the files that match the pattern "spec": "src/**/*.test.js" were being tested. But I just delete all the config, and turned out that they are tested by the default mocha settings....Maryannemarybella
L
1

    /* This example illustrates how to configure mocha globally
    *1. add the 'mocharch.json' to link mocha to the 'package.json'   like so:
    */

    {
        "package": "./package.json"
    }

    /* 2. in the 'package.json' add: */
    "mocha": {
        "recursive": "true"
    }
Lycaon answered 30/4, 2020 at 12:5 Comment(1)
Thanks for your respone. But I am seeing absolute zero effect. I have now added both .mocharch.json and mocharch.json (without dot). both pointing to package.json like in your example. In my package.json when i change "spec" to not match any of my files all tests are still run, indicating that these options is not being applied. What I see is default mocha behavior.Maryannemarybella
D
0

The answer by Rathore is great, but I just wanted to point out that if you just add the .mocharc.json file to your base directory, you don't need to specify "--config=test/.mocharc.json" in your package.json, it just finds it automatically.

Damaraland answered 22/2, 2021 at 20:57 Comment(0)
T
0
  1. you can create .mocharc.json in project root folder.

    {
      "spec": "src/tests/**/*.ts",
      "require": "ts-node/register"
    }
    
  2. in package.json add mocha property.

    "mocha": {
      "spec": ["src/tests/**/*.ts"],
      "require": ["ts-node/register"]
    }
    

js project change file name.

Thicket answered 16/8, 2022 at 0:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.