mocha.opts 已弃用 - 如何迁移到 .mocharc

luk*_*s_o 5 mocha.js vscode-debugger

我正在使用一个mocha.opts文件在 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.
Run Code Online (Sandbox Code Playgroud)

我现在无法运行我的测试,想迁移到 mocharc 文件。我不介意 mocharc 格式是 yaml 或 json。mocha 文档很长并且没有提供迁移示例。

Q1:怎么做,有没有例子?

EDIT:

我发现了这个:https : //github.com/mochajs/mocha/blob/master/example/config/.mocharc.yml

这是包含所有可能字段的 .mocharc.yaml 配置的示例。

Q2: env vars 怎么样,我可以在 mocha.opts 中设置,如何在 .mocharc 中设置?

cya*_*sin 3

编辑

A1: 就是这些。摩卡文档看起来很不友善..

看起来yml格式的文件key可以是js、json格式的数组字段。例子

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

Run Code Online (Sandbox Code Playgroud)

应该

...
file : [
  '/path/to/some/file',
  '/path/to/some/other/file'
]
Run Code Online (Sandbox Code Playgroud)

A1。你可以选择多种格式,如 json、js、yml 等。

当你的摩卡选择文件是

---ui tdd
--r ts-node/register
--r tsconfig-paths/register
Run Code Online (Sandbox Code Playgroud)

可以像这样更改为mocharc.json

{
    "require" : [
        "ts-node/register",
        "tsconfig-paths/register"
    ],
    "package": "./package.json",
    "ui": "tdd"
}
Run Code Online (Sandbox Code Playgroud)

A2。这个线程也许可以帮助你 github mocha env var 设置链接

我认为最好在 package.json 上设置,而不是在 mocha 文件中设置。

但你可以像这样使用 require args 指定环境变量

// In .mocharc.json
{
    "require" : [
        "ts-node/register",
        "tsconfig-paths/register",
        "test/mocha.env"
    ],
    "package": "./package.json",
    "ui": "tdd"
}
Run Code Online (Sandbox Code Playgroud)
// in .mocharc.yml
file:
  - '/path/to/some/file'
  - '/path/to/some/other/file'

Run Code Online (Sandbox Code Playgroud)