-
Notifications
You must be signed in to change notification settings - Fork 34
Async Testing
James Richford edited this page Feb 3, 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("async/await test")
public async asyncTest() {
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("async promise test")
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("async test with different timeout")
@Timeout(5000) // Alsatian will now wait 5 seconds before failing
public async asyncTest() {
const result = await somethingThatTakesAlmostFiveSeconds();
Expect(result).toBe(1);
}
}