Giter Site home page Giter Site logo

Comments (5)

lo1tuma avatar lo1tuma commented on May 20, 2024

I general I like this proposal. I think the hard part of this would be detect if a promise is returned or not. Do you know how mocha does this check? Does it fail for any non falsy value that is returned or only for objects that have a then method?

Maybe the easiest approach would be to warn on any top-level return (maybe with exceptions for return; return undefined; and return null;). I think looking for the usage of calling a then method within the return statement is probably pretty error-prone. Consider the following examples:

it('foo', function () {
    // chai-as-promised assertions returns promises
    return expect(promiseReturningFunction()).to.be.fulfilled;
});
it('foo', function () {
    return Promise.all([ checkFoo(), checkBar() ]);
});

from eslint-plugin-mocha.

jfmengels avatar jfmengels commented on May 20, 2024

Does it fail for any non falsy value that is returned or only for objects that have a then method?

Only for Promises (so the latter). This is correct behavior for instance:

describe('foo', function() {
  it('should', function(done) {
    function foo() {
      done();
    }
    setTimeout(foo, 500);
    return 3;
  });
});

But returning anything other than a Promise doesn't provide any value, so I'd say it's fine to warn if there is a return statement and a callback.

Maybe the easiest approach would be to warn on any top-level return

Yes, that is what I was thinking too.

maybe with exceptions for return; return undefined; and return null;

Not sure I see the point of having such statements in your tests anyway 😕

I think looking for the usage of calling a then method within the return statement is probably pretty error-prone

Same here. Sometimes you just want to call catch, or custom Bluebird methods for instance.

from eslint-plugin-mocha.

lo1tuma avatar lo1tuma commented on May 20, 2024

Not sure I see the point of having such statements in your tests anyway

Maybe it is used to skip further execution of the test, but maybe you are right it is uncommon to do that anyway 😉 .

from eslint-plugin-mocha.

jfmengels avatar jfmengels commented on May 20, 2024

Maybe it is used to skip further execution of the test, but maybe you are right it is uncommon to do that anyway 😉 .

Ah, you mean something like this

it('should xyz', function() {
  const res = foo();
  assert(res);
  if (!process.env.FOO) {
    return;
  }
  assert(res.xyz === 'foo');
});

Would make sense if you're testing stuff conditionally based on the environment, or creating tests in a loop. You're right, I'll make the change later to ignore this when you return a nothing (return;) or a literal value (return null/"foo") or undefined.

from eslint-plugin-mocha.

jfmengels avatar jfmengels commented on May 20, 2024

You're right, I'll make the change later to ignore this when you return a nothing (return;) or a literal value (return null/"foo") or undefined.

That has been done in PR #94.

from eslint-plugin-mocha.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.