Unlike non-approved code with custom developer-compatible checks,
class Some { constructor(arg) { if (Array.isArray(arg) && arg[0] === 'foo') this.foobar = arg.concat('bar').join(''); else console.error('Bad Some constructor arg'); } }
The code currently checked is heavily packed with Node assert with reasonably significant message arguments:
class Some { constructor(arg) { assert.deepEqual(arg, ['foo'], 'Some constructor arg'); this.foobar = arg.concat('bar').join(''); } }
Approval exists for
- save the code in the same place and readable
- provide meaningful feedback on misuse with the call stack
- prevent function execution and do not propagate the error further
- output an error and leave the error handling to the caller
The current specification may look like this:
it('...', () => { let some = new Some(['foo']); expect(some).to...
And it will pass - the desired use is stated in the spec, the unwanted use that it claimed in the tested code.
To partially overlap code statements, it can even be
it('...', () => { const { AssertionError } = require('assert'); let some = new Some(['foo']); expect(some).to... expect(() => new Some(['bar']).to.throw(AssertionError);
So, we basically assume that half of the test task has already been completed in the code itself using assert and skip the details ( to.not.throw and matching message AssertionError).
The above example uses Mocha + Chai, but the same applies to Jasmine.
Should application statements be treated like any other lines of code and doubled with statements about specifications (in order to throw, not throw, AssertionError message correspondence), what are the consequences of accepting a shortcut?
Is it possible to test coverage tools (Istanbul) with assert statements in the application code in addition to expect ?
Can runners be confused by the fact that it was an application, not a statement of the specification that caused the error?
Some examples of successful open source JS projects that prove or disprove the claim to claim statements in practice may also be helpful.
source share