playwright-fluent
Version:
Fluent API around playwright
152 lines (151 loc) • 5.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const wait_until_1 = require("./wait-until");
const SUT = tslib_1.__importStar(require("./index"));
describe('wait until', () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach(() => { });
test('should wait', async () => {
// Given
const wait = 3000;
const startTime = new Date().getTime();
const predicate = async () => {
const endTime = new Date().getTime();
if (endTime - startTime <= wait) {
return false;
}
return true;
};
// When
await SUT.waitUntil(predicate, 'cannot wait any more!');
const result = await predicate();
// Then
expect(result).toBe(true);
});
test('should wait until timeout is reached then should not throw', async () => {
// Given
const options = {
stabilityInMilliseconds: 300,
throwOnTimeout: false,
timeoutInMilliseconds: 3000,
verbose: false,
wrapPredicateExecutionInsideTryCatch: false,
};
const predicate = async () => {
return false;
};
// When
await SUT.waitUntil(predicate, 'cannot wait any more!', options);
const result = await predicate();
// Then
expect(result).toBe(false);
});
test('should wait until timeout is reached then should not throw even when predicate throws', async () => {
// Given
const options = {
stabilityInMilliseconds: 300,
throwOnTimeout: false,
timeoutInMilliseconds: 3000,
verbose: false,
wrapPredicateExecutionInsideTryCatch: true,
};
const predicate = async () => {
throw new Error('predicate error');
};
// When
await SUT.waitUntil(predicate, 'cannot wait any more!', options);
// Then
});
test('should not wait and not throw with predicate always false', async () => {
// Given
const options = {
...wait_until_1.noWaitNoThrowOptions,
};
const predicate = async () => {
return false;
};
const startTime = new Date().getTime();
// When
await SUT.waitUntil(predicate, 'cannot wait any more!', options);
const result = await predicate();
const endTime = new Date().getTime();
// Then
const duration = endTime - startTime;
// eslint-disable-next-line no-console
console.log(`measured duration with noWaitNoThrowOptions = ${duration} ms`);
expect(duration).toBeLessThan(3000);
expect(result).toBe(false);
});
test('should not wait and not throw with predicate always true', async () => {
// Given
const options = {
...wait_until_1.noWaitNoThrowOptions,
};
const predicate = async () => {
return true;
};
const startTime = new Date().getTime();
// When
await SUT.waitUntil(predicate, 'cannot wait any more!', options);
const result = await predicate();
const endTime = new Date().getTime();
// Then
const duration = endTime - startTime;
// eslint-disable-next-line no-console
console.log(`measured duration with noWaitNoThrowOptions = ${duration} ms`);
expect(duration).toBeLessThan(3000);
expect(result).toBe(true);
});
test('should throw when timeout is reached', async () => {
// Given
const options = {
stabilityInMilliseconds: 300,
throwOnTimeout: true,
timeoutInMilliseconds: 2000,
verbose: false,
wrapPredicateExecutionInsideTryCatch: false,
};
const predicate = async () => {
return false;
};
// When
// Then
const expectedError = new Error('cannot wait any more!');
await SUT.waitUntil(predicate, 'cannot wait any more!', options).catch((error) => expect(error).toMatchObject(expectedError));
});
test('should throw when timeout is reached - error message is a func', async () => {
// Given
const options = {
...wait_until_1.defaultWaitUntilOptions,
stabilityInMilliseconds: 300,
throwOnTimeout: true,
timeoutInMilliseconds: 2000,
verbose: false,
};
const predicate = async () => {
return false;
};
// When
// Then
const expectedError = new Error('cannot wait any more!');
await SUT.waitUntil(predicate, () => Promise.resolve('cannot wait any more!'), options).catch((error) => expect(error).toMatchObject(expectedError));
});
test('should throw when predicate throw', async () => {
// Given
const options = {
stabilityInMilliseconds: 300,
throwOnTimeout: false,
timeoutInMilliseconds: 2000,
verbose: false,
wrapPredicateExecutionInsideTryCatch: false,
};
const predicate = async () => {
throw new Error('predicate error');
};
// When
// Then
const expectedError = new Error('predicate error');
await SUT.waitUntil(predicate, 'cannot wait any more!', options).catch((error) => expect(error).toMatchObject(expectedError));
});
});