-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Get rid of chai-as-promised
and use async
.
#928
Comments
chai-as-promised
and use async
.chai-as-promised
and use async
.
I have been trying to get my head around this issue, and wondered what was your point of view today ? I rewrote most of the tests for But I can't see how to test for Promise rejection without using the What would you reckon ? |
Relative to eclipse-theia#928 Signed-off-by: WKnight02 <wknight02@gmail.com>
Relative to eclipse-theia#928 Signed-off-by: marechal-p <marechap.info@gmail.com>
@marechal-p, good point. Would you be fine with something like this? import { expect } from 'chai';
describe('testMe', async () => {
it('resolve', async () => {
expect(await testMe(true)).to.be.true;
});
it('reject', async () => {
try {
await testMe(false);
throw new Error('Expected a rejection.');
} catch (e) {
expect(e.message).to.be.equal('arg was false');
}
});
});
function testMe(arg: boolean): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
if (!arg) {
return reject(new Error('arg was false'));
}
resolve(arg);
});
} |
Throwing an error directly inside the try statement could do the trick, though I'm not fond of testing the exact string of an error... Maybe something along those lines: import { expect } from 'chai';
// unique error thrown from tests
class TestError extends Error {}
describe('testMe', async () => {
it('resolve', async () => {
expect(await testMe(true)).to.be.true;
});
it('reject', async () => {
try {
await testMe(false);
throw new TestError('Expected a rejection.');
} catch (e) {
expect(e).to.not.be.instanceof(TestError);
}
});
});
function testMe(arg: boolean): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
if (!arg) {
return reject(new Error('arg was false'));
}
resolve(arg);
});
} But at this point I'm wondering if trying to get rid of chai-as-promised is really important ? Just want to make sure that this change is necessary. |
So the initial idea of this task was the following: we had the first tests with CC: @akosyakov |
let's clean up first with keeping |
Are you talking about the following piece ? before(() => {
chai.config.showDiff = true;
chai.config.includeStack = true;
chai.should();
chai.use(chaiAsPromised);
}); Because when looking at http://chaijs.com/guide/plugins/ in order to see how to add assertion statements, it seems like we would need something that resembles it. import * as chai from "chai";
import * as our_plugin from ".../x/y/plugin";
var expect = chai.expect;
chai.use(our_plugin); So I don't really know where we would place this async function expectThrow(promise: Promise<any>, errorType = Error) {
try {
await promise;
throw new TestError('Expected a rejection');
} catch (e) {
expect(e).to.not.be.instanceof(TestError);
expect(e).to.be.instanceof(errorType);
}
} To be used like: await expectThrow(asyncFunction(), Error); But the Sorry for getting into so much details on this, just trying to clear things out. |
Relative to eclipse-theia#928 Signed-off-by: marechal-p <marechap.info@gmail.com>
Relative to eclipse-theia#928 Signed-off-by: marechal-p <marechap.info@gmail.com>
Yes.
I like that. I will check what could be the problem right now.
Hey, there is nothing to be sorry about. I get back to you as soon as possible. |
You were very very close. The custom error construction is a bit tricky if you would like to use Let me know what do you think: import { expect } from 'chai';
// tslint:disable:no-unused-expression
class UnfulfilledPromiseRejectionError extends Error {
constructor(message: string = 'Expected a promise rejection.') {
super(message);
Object.setPrototypeOf(this, UnfulfilledPromiseRejectionError.prototype);
}
}
describe('testMe', async () => {
it('resolve', async () => {
expect(await testMe(true)).to.be.true;
});
it('reject - OK Error', async () => {
await expectThrows(testMe(false), Error);
});
it('reject - OK ReferenceError', async () => {
await expectThrows(testMe(false), ReferenceError);
});
it('reject - NOK was not rejected', async () => {
await expectThrows(testMe(true), Error);
});
it('reject - NOK different error', async () => {
await expectThrows(testMe(false), EvalError);
});
});
/** Throws a reference error if the argument is `false`.s */
function testMe(arg: boolean): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
if (!arg) {
return reject(new ReferenceError('arg was false'));
}
resolve(arg);
});
}
// tslint:disable-next-line:no-any
export async function expectThrows(promise: Promise<any>, errorType: any) {
try {
await promise;
throw new UnfulfilledPromiseRejectionError();
} catch (e) {
if (e instanceof UnfulfilledPromiseRejectionError) {
throw new Error(`The promise was not rejected.`);
} else {
expect(e).to.be.instanceof(errorType, `Expected an error of type ${errorType.name}, got ${e.name} instead.`);
}
}
} |
We could have |
I am OK with both although I do not see any reason throwing anything but Error. |
I really like the one linked on StackOverflow: async function assertThrowsAsync(fn, regExp) {
let f = () => {};
try {
await fn();
} catch(e) {
f = () => {throw e};
} finally {
assert.throws(f, regExp);
}
} Also it seems like only the FS test suite is actually using chai-as-promised, other tests setup the Getting rid of chai-as-promised seems to be relevant, but because we could still test for async exceptions in other files I wonder where to write the Do we place it in such a way that it can be imported from tests that wants it ? If so where ? |
👍
That was the actual goal of this task. Thanks for checking.
What about creating a new module for it under the: |
Relative to eclipse-theia#928 Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
Turns out I was looking for the use of
They mostly use the The only requirement is to write This is a trivial issue but understanding the logic here may help me understand the general mindset to have later. |
I do not know this off the top of my head but isn't |
It didn't cross my mind at all. But you are totally right. Going with this :) |
Relative to eclipse-theia#928 Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
Relative to eclipse-theia#928 Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
… removing `chai-as-promised` Relative to eclipse-theia#928 Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
… removing `chai-as-promised` Relative to eclipse-theia#928 Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
… removing `chai-as-promised` Relative to eclipse-theia#928 Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
… removing `chai-as-promised` Relative to #928 Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
One good place to start with the clean-up is the FS tests.
We could get rid of the
before
in the tests, and simply useawait
in the tests.The text was updated successfully, but these errors were encountered: