How to use whilst method in Testcafe

Best JavaScript code snippet using testcafe

Run Testcafe automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

whilst.js

Source: whilst.js Github

copy
1/*
2 * Tests the "whilst" function
3 */
4
5var mod_util = require('util');
6
7var mod_tap = require('tap');
8var mod_vasync = require('..');
9
10mod_tap.test('basic whilst', function (test) {
11	var n = 0;
12
13	mod_vasync.whilst(
14	    function condition() {
15		return (n < 5);
16	    },
17	    function body(cb) {
18		n++;
19		cb(null, n);
20	    },
21	    function done(err, arg) {
22		test.ok(!err, 'error unset');
23		test.equal(n, 5, 'n == 5');
24		test.equal(n, arg, 'n == arg');
25		test.end();
26	    });
27});
28
29mod_tap.test('whilst return object', function (test) {
30	var n = 0;
31
32	var w = mod_vasync.whilst(
33	    function condition() {
34		return (n < 5);
35	    },
36	    function body(cb) {
37		n++;
38
39		test.equal(n, w.iterations, 'n == w.iterations: ' + n);
40
41		cb(null, n, 'foo');
42	    },
43	    function done(err, arg1, arg2, arg3) {
44		test.ok(!err, 'error unset');
45		test.equal(w.iterations, 5, 'whilst had 5 iterations');
46		test.equal(w.finished, true, 'whilst has finished');
47		test.equal(arg1, n, 'whilst arg1 == n');
48		test.equal(arg2, 'foo', 'whilst arg2 == "foo"');
49		test.equal(arg3, undefined, 'whilst arg3 == undefined');
50		test.end();
51	    });
52
53	test.equal(typeof (w), 'object', 'whilst returns an object');
54	test.equal(w.finished, false, 'whilst is not finished');
55	test.equal(w.iterations, 0, 'whilst has not started yet');
56});
57
58mod_tap.test('whilst false condition', function (test) {
59	mod_vasync.whilst(
60	    function condition() {
61		return (false);
62	    },
63	    function body(cb) {
64		cb();
65	    },
66	    function done(err, arg) {
67		test.ok(!err, 'error is unset');
68		test.ok(!arg, 'arg is unset');
69		test.end();
70	    });
71});
72
73mod_tap.test('whilst error', function (test) {
74	var n = 0;
75
76	var w = mod_vasync.whilst(
77	    function condition() {
78		return (true);
79	    },
80	    function body(cb) {
81		n++;
82
83		if (n > 5) {
84			cb(new Error('n > 5'), 'bar');
85		} else {
86			cb(null, 'foo');
87		}
88	    },
89	    function done(err, arg) {
90		test.ok(err, 'error is set');
91		test.equal(err.message, 'n > 5');
92		test.equal(arg, 'bar');
93
94		test.equal(w.finished, true, 'whilst is finished');
95
96		/*
97		 * Iterations is bumped after the test condition is run and
98		 * before the iteration function is run.  Because the condition
99		 * in this example is inside the iteration function (the test
100		 * condition always returns true), the iteration count will be
101		 * 1 higher than expected, since it will fail when (n > 5), or
102		 * when iterations is 6.
103		 */
104		test.equal(w.iterations, 6, 'whilst had 6 iterations');
105
106		test.end();
107	    });
108});
109
Full Screen

whilist.js

Source: whilist.js Github

copy
1'use strict'
2
3var painless = require('../../assertion/painless')
4var test = painless.createGroup('Test promise/promtie::whilst')
5var t = painless.assert
6
7var whilst = require('../../../src/promise/promtie/whilst')
8
9test('whilst(conditionFn, fn)', () => {
10  let count = 0;
11  let lastCount = count;
12
13  setInterval(() => {
14    count += 1;
15  }, 20);
16
17  return whilst(() => {
18      lastCount = count;
19
20      return count <= 3;
21    }, () => t.truthy(count <= 3))
22    .then(() => t.truthy(lastCount > 3));
23});
24
25test('whilst(conditionFn, fn, options)', () => {
26  const start = Date.now();
27  let count = 0;
28  let lastCount = count;
29
30  setInterval(() => {
31    count += 1;
32  }, 20);
33
34  return whilst(() => {
35      lastCount = count;
36
37      return count <= 3;
38    }, () => t.truthy(count <= 3), { delay: 125 })
39    .then(() => {
40      t.truthy(lastCount > 3);
41      t.truthy((Date.now() - start) > 100);
42    });
43});
44
45test('whilst(conditionFn, fn): conditionFn returns promise', () => {
46  let count = 0;
47  let lastCount = count;
48
49  setInterval(() => {
50    count += 1;
51  }, 20);
52
53  return whilst(() => {
54      lastCount = count;
55
56      return Promise.resolve(count <= 3);
57    }, () => t.truthy(count <= 3))
58    .then(() => t.truthy(lastCount > 3));
59});
60
61test('whilst(conditionFn, fn): fn returns promise', () => {
62  let count = 0;
63  let lastCount = count;
64
65  setInterval(() => {
66    count += 1;
67  }, 20);
68
69  return whilst(() => {
70      lastCount = count;
71
72      return count <= 3;
73    }, () => {
74      t.truthy(count <= 3);
75
76      return Promise.resolve();
77    })
78    .then(() => t.truthy(lastCount > 3));
79});
80
81test('whilst(conditionFn, fn): conditionFn and fn return promises', () => {
82  let count = 0;
83  let lastCount = count;
84
85  setInterval(() => {
86    count += 1;
87  }, 20);
88
89  return whilst(() => {
90      lastCount = count;
91
92      return Promise.resolve(count <= 3);
93    }, () => {
94      t.truthy(count <= 3);
95
96      return Promise.resolve();
97    })
98    .then(() => t.truthy(lastCount > 3));
99});
100
101test('whilst(conditionFn, fn): conditionFn throws', () => {
102  return whilst(() => {
103      throw new Error('condition not met'); }, () => t.fail('should not have run this'))
104    .then(() => t.fail('expected to fail'), err => t.is(err.message, 'condition not met'));
105});
106
107test('whilst(conditionFn, fn): fn throws', () => {
108  return whilst(() => true, () => {
109      throw new Error('condition not met'); })
110    .then(() => t.fail('expected to fail'), err => t.is(err.message, 'condition not met'));
111});
112
Full Screen

doWhilst.js

Source: doWhilst.js Github

copy
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4    value: true
5});
6exports.default = doWhilst;
7
8var _noop = require('lodash/noop');
9
10var _noop2 = _interopRequireDefault(_noop);
11
12var _slice = require('./internal/slice');
13
14var _slice2 = _interopRequireDefault(_slice);
15
16var _onlyOnce = require('./internal/onlyOnce');
17
18var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
19
20var _wrapAsync = require('./internal/wrapAsync');
21
22var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
23
24function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
26/**
27 * The post-check version of [`whilst`]{@link module:ControlFlow.whilst}. To reflect the difference in
28 * the order of operations, the arguments `test` and `iteratee` are switched.
29 *
30 * `doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript.
31 *
32 * @name doWhilst
33 * @static
34 * @memberOf module:ControlFlow
35 * @method
36 * @see [async.whilst]{@link module:ControlFlow.whilst}
37 * @category Control Flow
38 * @param {AsyncFunction} iteratee - A function which is called each time `test`
39 * passes. Invoked with (callback).
40 * @param {Function} test - synchronous truth test to perform after each
41 * execution of `iteratee`. Invoked with any non-error callback results of
42 * `iteratee`.
43 * @param {Function} [callback] - A callback which is called after the test
44 * function has failed and repeated execution of `iteratee` has stopped.
45 * `callback` will be passed an error and any arguments passed to the final
46 * `iteratee`'s callback. Invoked with (err, [results]);
47 */
48function doWhilst(iteratee, test, callback) {
49    callback = (0, _onlyOnce2.default)(callback || _noop2.default);
50    var _iteratee = (0, _wrapAsync2.default)(iteratee);
51    var next = function (err /*, ...args*/) {
52        if (err) return callback(err);
53        var args = (0, _slice2.default)(arguments, 1);
54        if (test.apply(this, args)) return _iteratee(next);
55        callback.apply(null, [null].concat(args));
56    };
57    _iteratee(next);
58}
59module.exports = exports['default'];
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Testcafe on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)