2

I'm using visual studio code v1.19.1 to write unit test with mocha.

I found that when I hover the mouse over the after keyword, vs code says [standard] 'after' is not defined. (no-undef). This means that it doesn't recognize after keyword.

It seems that this problem has something to do with JavaScript Standard Style plugin.

I've read this StackOverflow question. But adding

"standard": {
  "env": [ "mocha" ]
}

or

 "standard": {
    "globals": [
      "describe",
      "context",
      "before",
      "beforeEach",
      "after",
      "afterEach",
      "it",
      "expect"
    ]
  }

to package.json doesn't work in my case.

What might be wrong with my visual studio code or javascript standard style plugin?

Brian
  • 12,145
  • 20
  • 90
  • 153

2 Answers2

0

I just found that this error happens because my package.json file is not located in the root folder of visual studio code.

After moving it to the root folder of visual studio code, vscode recognizes before, after keywords in mocha.

Brian
  • 12,145
  • 20
  • 90
  • 153
0

I was experiencing the same problem, but unfortunately could not move package.json out of its current directory as I am working within a monorepo. The workaround that worked for me was to directly import the mocha library inside my test files by defining its path as follows:

const mocha = require('../../../node_modules/mocha/lib/mocha.js');

and then use declare its keywords as follows:

mocha.before(() => {})

Ovidijus Parsiunas
  • 2,512
  • 2
  • 8
  • 18