-
Notifications
You must be signed in to change notification settings - Fork 34
Async Testing
James Richford edited this page Jan 6, 2017
·
5 revisions
You can also have asynchronous tests using the AsyncTest
annotation,
import { Expect, AsyncTest, TestFixture } from "alsatian";
@TestFixture("Example Test Fixture")
export class ExampleTestFixture {
@AsyncTest()
public async asyncTest() {
return new Promise((resolve, reject) => {
const result = await somethingToHappen();
Expect(result).toBe(1);
});
}
}
If you for whatever reason can't use the async/await syntax you can return a promise instead and everything will work fine.
import { Expect, AsyncTest, TestFixture } from "alsatian";
@TestFixture("Example Test Fixture")
export class ExampleTestFixture {
@AsyncTest()
public asyncTest() {
return new Promise((resolve, reject) => {
waitForSomethingToHappen((result: number) => {
Expect(result).toBe(1);
resolve();
});
});
}
}
Alsatian will fail an AsyncTest
if it takes longer than 500 ms to execute. You can change this if you need to though using the Timeout
decorators
import { Expect, AsyncTest, Timeout, TestFixture } from "alsatian";
@TestFixture("Example Test Fixture")
export class ExampleTestFixture {
@AsyncTest()
@Timeout(5000) // Alsatian will now wait 5 seconds before failing
public async asyncTest() {
const result = await somethingThatTakesAlmostFiveSeconds();
Expect(result).toBe(1);
}
}