LoginSignup
5
3

More than 5 years have passed since last update.

VisualStudioCode + Node.js + Mocha(^6.0.0) test + debug run TIPS for "ReferenceError: describe is not defined"

Last updated at Posted at 2019-03-17

サマリー

VisualStudioCodeのNode.js Mocha testのデバッグ実行で、ReferenceError: describe is not definedのエラーがでる人は、VisualStudioCodeのlounch.jsonのtddbddに変更するとエラーが解消される。

lounch.json
{
    "type": "node",
    "request": "launch",
    "name": "Mocha Tests",
    "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
    "args": [
        "-u",
        "bdd",
        "--timeout",
        "999999",
        "--colors",
        "${workspaceFolder}/test"
    ],
    "internalConsoleOptions": "openOnSessionStart"
},

現象

VisualStudioCodeのNode.js Mocha testをデバッグ実行すると、以下のエラーがでるようになった。

  • 環境
    • VisualStudioCode: 1.32.3
    • mocha: 6.0.2
/Users/hoge/.nodebrew/current/bin/node --inspect-brk=16665 node_modules/mocha/bin/_mocha -u tdd --timeout 999999 --colors /Users/hoge/Git/my-project/test 
Debugger listening on ws://127.0.0.1:16665/0cd5be4c-b761-4eb0-b934-0c3d7df7f99b
Debugger attached.
/Users/hoge/Git/my-project/node_modules/yargs/yargs.js:1148
      else throw err
           ^
ReferenceError: describe is not defined
    at Object.<anonymous> (/Users/hoge/Git/my-project/test/index.spec.js:20:1)
    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 Module.require (module.js:596:17)
    at require (internal/module.js:11:18)

原因

mocha 6.0.0から、RCファイルがサポートされたり、yargsでオプションを解析するようになった関係で、今まで、たまたま動作していた部分が動かなくなったようです。
 ・add config file support; switch to yargs #3556

Visual Studio Codeがデフォルトで指定する--u tddは、TDD方式を意味していて、こんな書き方です。

tdd.js
mocha.setup('tdd');

suite('something', function() {
  setup(function() {
  });

  test('should work', function() {
  });

  teardown(function() {
  });
});

一方、mochaのチュートリアルに記載されている書き方はBDD方式で、こんな書き方です。

bdd.js
mocha.setup('bdd');

describe('something', function() {
  beforeEach(function() {
  });

  it('should work', function() {
  });
});

Visual Studio Codeのlounch.jsonに--u tddとある場合は、TDD方式を想定しているため、BDD方式のdescribeに対して ReferenceError: describe is not definedのエラーメッセージがでます。

Mocha version 6.0.0

実行オプションの指定に関する機能拡張がありました。

  • --no-timeouts
    デバッグ時にタイムアウトしないように指定できるようになりました。
    今まで、--timeout 999999のおまじないを書いていましたが、これの代わりです。
  • RCファイル
    mochaの実行オプションを設定ファイル(RCファイル)で記載できるようになりました。

    RCファイルは以下が可能です。

    • .mocharc.js
    • .mocharc.json
    • .mocharc.yaml
    • .mocharc.yml
    mocharc.json
    {
      "diff": true,
      "extension": ["js"],
      "opts": "./test/mocha.opts",
      "package": "./package.json",
      "reporter": "spec",
      "slow": 75,
      "timeout": 2000,
      "ui": "bdd"
    }
    

    特定のRCファイルを指定して実行したい場合は、--config /path/to/rc/fileオプションを指定します。

  • package.json
    設定は、package.jsonにも記載可能です。

    package.json
    "mocha": {
      "diff": true,
      "extension": ["js"],
      "opts": "./test/mocha.opts",
      "package": "./package.json",
      "reporter": "spec",
      "slow": 75,
      "timeout": 2000,
      "ui": "bdd"
    }
    

    特定のpackage.jsonを指定して実行したい場合は、--package /path/to/package.jsonオプションを指定します。

  • 設定の優先順位

    設定は、以下の順に適用されます。

    1. Command-line arguments
    2. RC file
    3. package.json
    4. mocha.opts
    5. Mocha's own defaults
  • 設定の無効化
    --no-config--no-packageを指定すると設定を無効にできます。

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3