18

I need to setup jest and JavaScript Standard Style to work together when using npm test.

Now when I am running npm test the test fails because JavaScript Standard Style thrown an errors:

'test' is not defined.
'expect' is not defined. 

I can work around this issue by defining in my package.json file some global for jest.

"standard": {
    "globals": [
        "fetch",
        "test",
        "expect"
    ]
}

But definitely I do not think it is a good solution.

In my test case sum.test.js

const sum = require('./sum')    
test('adds 1 + 2 to equal 3', () => {
    expect(sum(1, 2)).toBe(3)
})

In my package.json :

"scripts": {
    "test": "standard && jest",
}

Question:

  • How to configure JavaScript Standard Style so it does not thrown an error when used with jest?
quotesBro
  • 6,030
  • 2
  • 31
  • 41
GibboK
  • 71,848
  • 143
  • 435
  • 658

2 Answers2

54

I was able to find a solution.

In package.json

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

Or in the test case:

/* eslint-env mocha */
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • 9
    I guess `/* eslint-env mocha */` should be `/* eslint-env jest */` – Beat Nov 21 '18 at 13:36
  • In my case, I wanted to use [`fail`](https://stackoverflow.com/a/55526098/614523) but it was not recognised. I added `"globals": [ "fail" ]` to the `standard` entry package.json as per the original question and it seemed to work, but I believe this will allow `fail` in all files, not just tests. – sennett Oct 29 '20 at 12:27
  • It's really better in the test case, however for a quick fix when you are starting a toy project it's not bad at all having the environments defined globally. – Jesús Franco May 20 '21 at 18:39
2

In your .eslintrc.js file, include the following setting and you'll be good to go.

"env": {
  "node": true,
  "jest": true
}

If you are using a .eslintrc or a .eslintrc.json file, please use appropriate syntax.

Rajat Saxena
  • 3,834
  • 5
  • 45
  • 63