Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Standard Style does not recognize jest

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?
like image 593
GibboK Avatar asked Sep 07 '17 10:09

GibboK


2 Answers

I was able to find a solution.

In package.json

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

Or in the test case:

/* eslint-env mocha */
like image 132
GibboK Avatar answered Oct 13 '22 09:10

GibboK


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.

like image 33
Rajat Saxena Avatar answered Oct 13 '22 10:10

Rajat Saxena