I extend Chai with a helper in my TypeScript test.
import * as chai from 'chai';
chai.use((_chai) => {
let Assertion = _chai.Assertion;
Assertion.addMethod('sortedBy', function(property) {
// ...
});
});
const expect = chai.expect;
In the same file test case makes use of this method:
expect(tasks).to.have.been.sortedBy('from');
Compiler gives error that "Property 'sortedBy' does not exist on type 'Assertion'".
How can I add declaration of sortedBy
to Chai.Assertion
?
I've tried to add module declaration, just like other Chai plugin modules do, but it doesn't work.
declare module Chai {
interface Assertion {
sortedBy(property: string): void;
}
}
I don't want to make the helper an individual module, because it's trivial.
Try the following:
Extend chai in the chaiExt.ts like this:
declare module Chai
{
export interface Assertion
{
sortedBy(property: string): void;
}
}
Consume in chaiConsumer.ts:
import * as chai from 'chai';
//...
chai.expect(tasks).to.have.been.sortedBy('from');
[EDIT]
If you are using 'import' - you turn your file into the external module and declaration merging is not supported: link