Skip to content

Commit

Permalink
added errors helper
Browse files Browse the repository at this point in the history
  • Loading branch information
vstirbu committed Oct 28, 2017
1 parent 7f79b33 commit 6ddc91c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.2.0 / 2017-10-28

* added `errors` helper

## 0.1.0 / 2017-10-26

* updated TypeScript target to ES2017
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ If there are type errors, an error will be thrown indicating the position of the
Error: /absolute/path/to/filename.js (Ln 5, Col 6): Argument of type '123' is not assignable to parameter of type 'string'
```

Also, in case errors are expected, we can test using the following assertions indicating the number of errors expected:

```javascript
expect('/absolute/path/to/filename.js').to.have.types.errors(1);
```

## Disclaimer

The plugin is in experimental phase. Use it gently and provide feedback! :)
28 changes: 22 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ const prettifyDiagnostic = (diagnostic) => {
const messageText = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');

return `${diagnostic.file.fileName} (Ln ${line + 1}, Col ${character + 1}): ${messageText}`
}
};

module.exports = (chai, utils) => {
const Assertion = chai.Assertion;

utils.addProperty(Assertion.prototype, 'types');

utils.addChainableMethod(Assertion.prototype, 'validated', function () {
const filename = utils.flag(this, 'object');
const getObjectDiagnostics = (target) => {
const filename = utils.flag(target, 'object');
const program = ts.createProgram([filename], options);
const results = program.emit();

const diagnostics = ts.getPreEmitDiagnostics(program).concat(results.diagnostics);
return ts.getPreEmitDiagnostics(program).concat(results.diagnostics);
};

utils.addProperty(Assertion.prototype, 'types');

utils.addChainableMethod(Assertion.prototype, 'validated', function () {
const diagnostics = getObjectDiagnostics(this);

try {
new chai.Assertion(diagnostics).to.have.length(0);
Expand All @@ -39,4 +43,16 @@ module.exports = (chai, utils) => {
}, function () {
utils.flag(this, 'types.validated', true);
});

utils.addChainableMethod(Assertion.prototype, 'errors', function (expectedErrors) {
const diagnostics = getObjectDiagnostics(this);

if (expectedErrors) {
new chai.Assertion(diagnostics).to.have.length(expectedErrors);
} else {
new chai.Assertion(diagnostics).to.have.length.above(0);
}
}, function () {
utils.flag(this, 'types.errors', true);
})
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chai-as-typed",
"version": "0.1.0",
"version": "0.2.0",
"description": "Chai assertions for types defined via JSDoc",
"main": "lib/index.js",
"scripts": {
Expand Down
42 changes: 31 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,42 @@ const expect = chai.expect;

chai.use(chaiAsTyped);

describe('Types validation', function () {
it('should succeed with correct types', function () {
this.timeout(5000);
describe('types', function () {
context('validated', function () {
it('should succeed with correct types', function () {
this.timeout(5000);

const filename = __dirname + '/fixture/valid.js';
const filename = __dirname + '/fixture/valid.js';

expect(filename).to.have.types.validated();
expect(filename).to.have.types.validated();
});

it('should fail with wrong types', function () {
this.timeout(5000);

const filename = __dirname + '/fixture/invalid.js';

try {
expect(filename).to.have.types.validated();
} catch (error) {}
});
});

it('should fail with wrong types', function () {
this.timeout(5000);
context('errors', function () {
it('should have expected number of errors', function () {
this.timeout(5000);

const filename = __dirname + '/fixture/invalid.js';
const filename = __dirname + '/fixture/invalid.js';

try {
expect(filename).to.have.types.validated();
} catch (error) {}
expect(filename).to.have.types.errors(1);
});

it('should have unspecified number of errors', function () {
this.timeout(5000);

const filename = __dirname + '/fixture/invalid.js';

expect(filename).to.have.types.errors();
});
});
});

0 comments on commit 6ddc91c

Please sign in to comment.