mocha.opts is deprecated - how to migrate to .mocharc

Viewed 4384

I am using an mocha.opts file to configure my tests in VS Code.

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 am unable to run my tests now and would like to migrate to a mocharc file. I don't mind the mocharc format being yaml or json. The mocha documentation is lengthy and doesn't provide migration examples.

Q1: How to do that, are there any examples?

EDIT:

I have found this: https://github.com/mochajs/mocha/blob/master/example/config/.mocharc.yml

Which is an example of a .mocharc.yaml config containing all possible fields.

Q2: What about env vars, I could set them in mocha.opts, how to do that in the .mocharc?

1 Answers

EDITED

A1 : All of thats. mocha document has been seem pretty unkind ..

it seem like file key on yml format can be array field in js, json format. example

// in .mocharc.yml
file:
  - '/path/to/some/file'
  - '/path/to/some/other/file'

should be

...
file : [
  '/path/to/some/file',
  '/path/to/some/other/file'
]

A1. you can choose many formats like json, js, yml etc.

when ur mocha opt file is

---ui tdd
--r ts-node/register
--r tsconfig-paths/register

can be changed to mocharc.json like this

{
    "require" : [
        "ts-node/register",
        "tsconfig-paths/register"
    ],
    "package": "./package.json",
    "ui": "tdd"
}

A2. this thread maybe help you github mocha env var setting link

I think it is better setting on package.json, not in mocha file.

but you can specify env vars with require args like this

// In .mocharc.json
{
    "require" : [
        "ts-node/register",
        "tsconfig-paths/register",
        "test/mocha.env"
    ],
    "package": "./package.json",
    "ui": "tdd"
}
// In mocha.env.ts or mocha.env.js
process.env.NODE_ENV = 'test'
Related