Should we stop using `this` in specs?
Discussion
We currently have some jasmine tests that attach resources to this
to be tested in nested specs. Other tests define the resources as variables in the outmost describe
block.
If we use this.resource = new MyResource()
, for example, we can't use arrow functions for the inner describe
and it
blocks. Rather, we should use regular functions, so that we can keep this
scope. If we use regular variables, we can use arrow functions as the global variable will still have scope inside those arrow functions.
Examples
// this.resource
describe('test example class', () => {
beforeEach(function() {
this.resource = new exampleClass();
});
it('specific test', function() {
expect(this.resource.specific).toBe('specific');
});
});
// var testClass
describe('test example class', () => {
let testClass;
beforeEach(() => {
testClass = new exampleClass();
});
it('specific test', () => {
expect(testClass.specific).toBe('specific');
});
});
Tradeoffs
Benefits of using this.resource
- Easy to know that
this.resource
is referencing the test class (when all the specs are doing so) - More benefits found in https://gist.github.com/traviskaufman/11131303
Benefits of using var testClass
- More straight forward, less potential of being confused by
this
(May make it easier for beginner contributors to understand and write specs)
Voting
this
in specs (Only use variables)
this
in specs (Don't use variables)
Voting will close in 48 hours from opening of this issue