Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make expect/should/assert be global in test files and be able to pass eslint #891

Closed
xareelee opened this issue Dec 23, 2016 · 3 comments

Comments

@xareelee
Copy link

xareelee commented Dec 23, 2016

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?

@shvaikalesh
Copy link
Contributor

shvaikalesh commented Dec 23, 2016

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

@xareelee
Copy link
Author

xareelee commented Dec 23, 2016

@shvaikalesh Great! Thanks for your reply, and it works for me. I'm looking forward to the next version of chai. 👍

@shvaikalesh
Copy link
Contributor

shvaikalesh commented Dec 23, 2016

@xareelee Me too. However, there are some issues that have to be resolved first: #890.

I will close this issue then. If you have another question or suggestion, please don't hesitate to open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants