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

[WIP] testing guidelines #449

Closed
SBoudrias opened this issue Dec 30, 2013 · 3 comments
Closed

[WIP] testing guidelines #449

SBoudrias opened this issue Dec 30, 2013 · 3 comments

Comments

@SBoudrias
Copy link
Member

Yeoman testing guidelines

This testing guide is based on Mocha BDD interface (describe / it).

Main principles

Tests must be always start with a clean state

This means prefer beforeEach to before. Re-instantiate objects before running each it blocks. Create every files required by a test in a beforeEach (or commit them in fixtures/). Reset any side effets done on the test environment after each test.

Tests must be runnable in isolation

Each test must pass if they're runned alone. You can run a single test by using mocha test.js --grep 'test name'.

Stub most performance heavy operation

When possible, always stub networks or other long operations.

We use sinon.js for most stubbing needs.

Naming convention

describe blocks should cover three types of information: Object to be tested, method/property, circumstantial group (basically, "when this").

it blocks cover assertions. They should use as few lines of code as possible. There should be as many it block as there is assertion on a method effect.

Instances methods and property should be prefixed by a bang sign (#find()). Static methods and property should be prefixed by a dot (.exclude()).

// Given this object
function Class() {
  this.args = nopt();
};
Class.exclude = function () {};
Class.name = 'Yeoman';
Class.prototype.find = function () {};

// We'd had this test structure
describe('Class', function () {
  describe('.exclude()', function () {});
  describe('.name', function () {});
  describe('#find()', function () {});
  describe('#args', function () {});
});

Methods should end using parentheses, but should not include expected parameters (parameters should be cover in documentation comments).

it blocks should be declarative.

// BAD
it('should find generators');

// GOOD
it('find generators');

Assertion

Don't add message to assertions unless the error thrown makes it unclear what failed.

If you must add a message, then describe the expected outcome and why it failed. For example:

// BAD
assert(Generator.appname, 'Generator have an `appname` property');

// GOOD
assert(Generator.appname, 'Expected Generator to have an `appname` property');

Remember that these message are the error message thrown with the failure. Let those be useful in these occasions.

Stylistic preferences

.bind() throwing assertions

When testing that a method throw with invalid parameters, prefer binding the parameters to creating an anonymous function.

// BAD
assert.throws(function () {
  class.method('Invalid param');
});

// GOOD
assert.throws(class.method.bind(class, 'Invalid param'));
@sindresorhus
Copy link
Member

huge 👍 to this. though it's not just style, so I would rather call it "testing guidelines".

@addyosmani
Copy link
Member

Big thanks for getting this started @SBoudrias! We've needed this for a while.

@SBoudrias
Copy link
Member Author

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

3 participants