I use mocha/chai with eslint/flow.
Currently, I run the tests with an npm script:
"testonly": "babel-node ./node_modules/.bin/_mocha --require ./scripts/mocha-bootload --check-leaks --full-trace src/**/__tests__/**/*.test.js"
which will run ./scripts/mocha-bootload.js first to set expect global:
// ./scripts/mocha-bootload.js
var chai = require('chai');
chai.use(require('dirty-chai')); // use dirty-chai to avoid eslint errors
global.expect = chai.expect;
...
Now I can use mocha/chai as global without importing them.
// ./src/__tests__/index.test.js
// no any import is needed
describe('first test', () => {
it ('should be passed', () => {
expect(true).to.be.true();
})
})
But this will raise errors when I run eslint. To make mocha pass eslint, I add "env": {"mocha": true} in the .eslintrc config file:
// .eslintrc
{
"env": {
"mocha": true,
...
},
...
}
However, it doesn't support "env": {"chai": true} to tell eslint treat chai as environment variables. How do I do to make it pass eslint (without adding any line in each test files)? By the way, is there any better way to set chia's expect/should/assert to be global?
Hey @xareelee, thanks for reaching out.
How do I do to make it pass eslint
You can specify "expect": true in ESLint's globals: http://eslint.org/docs/user-guide/configuring#specifying-globals
By the way, is there any better way to set chia's expect/should/assert to be global?
We are going to ship shiny new way of doing this (currently in master): https://github.com/chaijs/chai#pre-native-modules-usage-registers-the-chai-testing-style-globally
@shvaikalesh Great! Thanks for your reply, and it works for me. I'm looking forward to the next version of chai. 馃憤
@xareelee Me too. However, there are some issues that have to be resolved first: https://github.com/chaijs/chai/issues/890.
I will close this issue then. If you have another question or suggestion, please don't hesitate to open a new one.
Most helpful comment
Hey @xareelee, thanks for reaching out.
You can specify
"expect": truein ESLint'sglobals: http://eslint.org/docs/user-guide/configuring#specifying-globalsWe are going to ship shiny new way of doing this (currently in
master): https://github.com/chaijs/chai#pre-native-modules-usage-registers-the-chai-testing-style-globally