How to use createRegExpFromFile 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.

test.js

Source: test.js Github

copy
1const expect             = require('chai').expect;
2const fs                 = require('fs');
3const path               = require('path');
4const escapeStringRegExp = require('escape-string-regexp');
5const WARNING_MESSAGES   = require('../../../../../../lib/notifications/warning-message');
6
7const DATA_PATH = path.join(__dirname, './data');
8
9function createRegExp (message) {
10    return new RegExp(escapeStringRegExp(message));
11}
12
13function createRegExpFromFile (fileName) {
14    return createRegExp(fs.readFileSync(path.join(DATA_PATH, fileName)).toString().replace(/\r/g, ''));
15}
16
17const snapshotWarningRegExp = createRegExp(WARNING_MESSAGES.excessiveAwaitInAssertion);
18
19function getSnapshotWarnings () {
20    return testReport.warnings.filter(warningStr => {
21        return warningStr.match(snapshotWarningRegExp);
22    });
23}
24
25describe('[API] Assertions', function () {
26    it('Should perform .eql() assertion', function () {
27        return runTests('./testcafe-fixtures/assertions-test.js', '.eql() assertion', {
28            shouldFail: true,
29            only:       'chrome'
30        })
31            .catch(function (errs) {
32                expect(errs[0]).contains("AssertionError: testMessage: expected 'hey' to deeply equal 'yo'");
33                expect(errs[0]).contains(">  8 |        .expect('hey').eql('yo', 'testMessage');");
34            });
35    });
36
37    it('Should perform .notEql() assertion', function () {
38        return runTests('./testcafe-fixtures/assertions-test.js', '.notEql() assertion', {
39            shouldFail: true,
40            only:       'chrome'
41        })
42            .catch(function (errs) {
43                expect(errs[0]).contains('AssertionError: expected 2 to not deeply equal 2');
44                expect(errs[0]).contains('> 14 |        .expect(2).notEql(2);');
45            });
46    });
47
48    it('Should perform .ok() assertion', function () {
49        return runTests('./testcafe-fixtures/assertions-test.js', '.ok() assertion', {
50            shouldFail: true,
51            only:       'chrome'
52        })
53            .catch(function (errs) {
54                expect(errs[0]).contains('AssertionError: expected false to be truthy');
55                expect(errs[0]).contains('> 20 |        .expect(false).ok();');
56            });
57    });
58
59    it('Should perform .notOk() assertion', function () {
60        return runTests('./testcafe-fixtures/assertions-test.js', '.notOk() assertion', {
61            shouldFail: true,
62            only:       'chrome'
63        })
64            .catch(function (errs) {
65                expect(errs[0]).contains('AssertionError: expected 1 to be falsy ');
66                expect(errs[0]).contains('> 26 |        .expect(1).notOk();');
67            });
68    });
69
70    it('Should perform .contains() assertion', function () {
71        return runTests('./testcafe-fixtures/assertions-test.js', '.contains() assertion', {
72            shouldFail: true,
73            only:       'chrome'
74        })
75            .catch(function (errs) {
76                expect(errs[0]).contains('AssertionError: expected [ 1, 2, 3 ] to include 4');
77                expect(errs[0]).contains('> 32 |        .expect([1, 2, 3]).contains(4);');
78            });
79    });
80
81    it('Should perform .notContains() assertion', function () {
82        return runTests('./testcafe-fixtures/assertions-test.js', '.notContains() assertion', {
83            shouldFail: true,
84            only:       'chrome'
85        })
86            .catch(function (errs) {
87                expect(errs[0]).contains("AssertionError: expected 'answer42' to not include '42'");
88                expect(errs[0]).contains("> 38 |        .expect('answer42').notContains('42');");
89            });
90    });
91
92    it('Should perform .typeOf() assertion', function () {
93        return runTests('./testcafe-fixtures/assertions-test.js', '.typeOf() assertion', {
94            shouldFail: true,
95            only:       'chrome'
96        })
97            .catch(function (errs) {
98                expect(errs[0]).contains('AssertionError: expected 42 to be a function');
99                expect(errs[0]).contains("> 51 |        .expect(42).typeOf('function');");
100            });
101    });
102
103    it('Should perform .notTypeOf() assertion', function () {
104        return runTests('./testcafe-fixtures/assertions-test.js', '.notTypeOf() assertion', {
105            shouldFail: true,
106            only:       'chrome'
107        })
108            .catch(function (errs) {
109                expect(errs[0]).contains('AssertionError: expected 42 not to be a number');
110                expect(errs[0]).contains("> 64 |        .expect(42).notTypeOf('number');");
111            });
112    });
113
114    it('Should perform .gt() assertion', function () {
115        return runTests('./testcafe-fixtures/assertions-test.js', '.gt() assertion', {
116            shouldFail: true,
117            only:       'chrome'
118        })
119            .catch(function (errs) {
120                expect(errs[0]).contains('AssertionError: expected 42 to be above 42');
121                expect(errs[0]).contains('> 70 |        .expect(42).gt(42);');
122            });
123    });
124
125    it('Should perform .gte() assertion', function () {
126        return runTests('./testcafe-fixtures/assertions-test.js', '.gte() assertion', {
127            shouldFail: true,
128            only:       'chrome'
129        })
130            .catch(function (errs) {
131                expect(errs[0]).contains('AssertionError: expected 42 to be at least 53');
132                expect(errs[0]).contains('> 77 |        .expect(42).gte(53);');
133            });
134    });
135
136    it('Should perform .lt() assertion', function () {
137        return runTests('./testcafe-fixtures/assertions-test.js', '.lt() assertion', {
138            shouldFail: true,
139            only:       'chrome'
140        })
141            .catch(function (errs) {
142                expect(errs[0]).contains('AssertionError: expected 42 to be below 42');
143                expect(errs[0]).contains('> 83 |        .expect(42).lt(42);');
144            });
145    });
146
147    it('Should perform .lte() assertion', function () {
148        return runTests('./testcafe-fixtures/assertions-test.js', '.lte() assertion', {
149            shouldFail: true,
150            only:       'chrome'
151        })
152            .catch(function (errs) {
153                expect(errs[0]).contains('AssertionError: expected 42 to be at most 12');
154                expect(errs[0]).contains('> 90 |        .expect(42).lte(12);');
155            });
156    });
157
158    it('Should perform .within() assertion', function () {
159        return runTests('./testcafe-fixtures/assertions-test.js', '.within() assertion', {
160            shouldFail: true,
161            only:       'chrome'
162        })
163            .catch(function (errs) {
164                expect(errs[0]).contains('AssertionError: expected 4.5 to be within 4.6..7');
165                expect(errs[0]).contains('>  96 |        .expect(4.5).within(4.6, 7);');
166            });
167    });
168
169    it('Should perform .notWithin() assertion', function () {
170        return runTests('./testcafe-fixtures/assertions-test.js', '.notWithin() assertion', {
171            shouldFail: true,
172            only:       'chrome'
173        })
174            .catch(function (errs) {
175                expect(errs[0]).contains('AssertionError: expected 2.3 to not be within 2..3');
176                expect(errs[0]).contains('> 102 |        .expect(2.3).notWithin(2, 3);');
177            });
178    });
179
180    it('Should perform .match() assertion', function () {
181        return runTests('./testcafe-fixtures/assertions-test.js', '.match() assertion', {
182            shouldFail: true,
183            only:       'chrome'
184        })
185            .catch(function (errs) {
186                expect(errs[0]).contains("AssertionError: expected 'yo' to match /[x,z]o/");
187                expect(errs[0]).contains("> 156 |        .expect('yo').match(/[x,z]o/);");
188            });
189    });
190
191    it('Should perform .notMatch() assertion', function () {
192        return runTests('./testcafe-fixtures/assertions-test.js', '.notMatch() assertion', {
193            shouldFail: true,
194            only:       'chrome'
195        })
196            .catch(function (errs) {
197                expect(errs[0]).contains("AssertionError: expected '42 hey' not to match /\\d+ hey/");
198                expect(errs[0]).contains("> 162 |        .expect('42 hey').notMatch(/\\d+ hey/);");
199            });
200    });
201
202    it('Should raise a warning when trying to assert Selector instance', function () {
203        return runTests('./testcafe-fixtures/assertions-test.js', 'Passing Selector instance into an assertion', {
204            shouldFail: true,
205            only:       'chrome'
206        })
207            .catch(function () {
208                expect(testReport.warnings[0]).to.match(createRegExp(WARNING_MESSAGES.assertedSelectorInstance));
209            });
210    });
211
212    it('Should raise a warning when trying to assert ClientFunction instance', function () {
213        return runTests('./testcafe-fixtures/assertions-test.js', 'Passing ClientFunction instance into an assertion', {
214            shouldFail: true,
215            only:       'chrome'
216        })
217            .catch(function () {
218                expect(testReport.warnings[0]).to.contain(
219                    'You passed a ClientFunction object to \'t.expect()\'.\n' +
220                    'If you want to check the function\'s return value, use parentheses to call the function: fnName().'
221                );
222            });
223    });
224
225    it('Should raise a warning when trying to await Selector property in assertion', async function () {
226        await runTests('./testcafe-fixtures/assertions-test.js', 'Await Selector property', { only: 'chrome' });
227
228        expect(getSnapshotWarnings().length).to.eql(1);
229        expect(getSnapshotWarnings()[0]).to.match(createRegExpFromFile('expected-selector-property-awaited-callsite'));
230    });
231
232    it('Should raise a warning when using DOM Node snapshot property without await', async function () {
233        await runTests('./testcafe-fixtures/assertions-test.js', 'Snapshot property without await', { only: 'chrome' });
234
235        const missingAwaitWarningRegExp = createRegExp(WARNING_MESSAGES.missingAwaitOnSnapshotProperty);
236
237        const missingAwaitWarnings = testReport.warnings.filter(warningStr => {
238            return warningStr.match(missingAwaitWarningRegExp);
239        });
240
241        expect(missingAwaitWarnings.length).to.eql(2);
242        expect(missingAwaitWarnings[0]).to.match(createRegExpFromFile('expected-missing-await-on-snapshot-callsite/console-log'));
243        expect(missingAwaitWarnings[1]).to.match(createRegExpFromFile('expected-missing-await-on-snapshot-callsite/template-expansion'));
244    });
245
246    it('Should not raise a warning when using DOM Node snapshot property without await in assignment', async function () {
247        await runTests('./testcafe-fixtures/assertions-test.js', 'Snapshot property without await but valid', { only: 'chrome' });
248
249        expect(testReport.warnings).be.empty;
250    });
251
252    it('Should not raise a warning when reusing selector property assertions from a function', async function () {
253        await runTests('./testcafe-fixtures/assertions-test.js', 'Reused unawaited selector property assertion from a function', { only: 'chrome' });
254
255        expect(testReport.warnings).be.empty;
256    });
257
258    it('Should only raise one warning when reusing awaited selector property assertions from a function', async function () {
259        await runTests('./testcafe-fixtures/assertions-test.js', 'Reused awaited selector property assertion from a function', { only: 'chrome' });
260
261        expect(getSnapshotWarnings().length).to.eql(1);
262        expect(getSnapshotWarnings()[0]).contains("> 221 |        await t.expect(await selector.innerText).eql('');");
263    });
264
265    it('Should not raise a warning when reusing selector property assertions in a loop', async function () {
266        await runTests('./testcafe-fixtures/assertions-test.js', 'Reused unawaited selector property assertion in a loop', { only: 'chrome' });
267
268        expect(testReport.warnings).be.empty;
269    });
270
271    it('Should only raise one warning when reusing awaited selector property assertions in a loop', async function () {
272        await runTests('./testcafe-fixtures/assertions-test.js', 'Reused awaited selector property assertion in a loop', { only: 'chrome' });
273
274        expect(getSnapshotWarnings().length).to.eql(1);
275        expect(getSnapshotWarnings()[0]).contains("> 236 |        await t.expect(await Selector('#el1').innerText).eql('');");
276    });
277
278    it('Should raise multiple warnings when awaiting multiple selector properties in one assertion', async function () {
279        await runTests('./testcafe-fixtures/assertions-test.js', 'Multiple awaited selector properties in one assertion', { only: 'chrome' });
280
281        expect(getSnapshotWarnings().length).to.eql(2);
282        expect(getSnapshotWarnings()[0]).contains("> 242 |    await t.expect(await selector.innerText + await selector.innerText).eql('');");
283        expect(getSnapshotWarnings()[1]).contains("> 242 |    await t.expect(await selector.innerText + await selector.innerText).eql('');");
284    });
285
286    it('Should retry assertion for selector results', function () {
287        return runTests('./testcafe-fixtures/assertions-test.js', 'Selector result assertion', { only: 'chrome' });
288    });
289
290    it('Should raise error assertion for selector results assertion on timeout', function () {
291        return runTests('./testcafe-fixtures/assertions-test.js', 'Selector result assertion timeout', {
292            shouldFail:       true,
293            assertionTimeout: 20,
294            only:             'chrome'
295        })
296            .catch(function (errs) {
297                expect(errs[0]).contains("AssertionError: expected 'none' to deeply equal 'left'");
298                expect(errs[0]).contains("> 124 |        .expect(el.getStyleProperty('float')).eql('left');");
299            });
300    });
301
302    it('Should raise error when expecting an unawaited Promise that cannot be retried', () => {
303        return runTests('./testcafe-fixtures/assertions-test.js', 'Unawaited Promise assertion', {
304            shouldFail: true,
305            only:       'chrome'
306        })
307            .catch(function (errs) {
308                expect(errs[0]).contains(`Attempted to run assertions on a Promise object. Did you forget to await it? If not, pass "{ allowUnawaitedPromise: true }" to the assertion options.`);
309            });
310    });
311
312    it('Should allow an unawaited Promise with override option', function () {
313        return runTests('./testcafe-fixtures/assertions-test.js', 'Unawaited Promise assertion override', { only: 'chrome' });
314    });
315
316    it('Should raise error if `await` is missing', () => {
317        return runTests('./testcafe-fixtures/assertions-test.js', 'Missing await', {
318            shouldFail: true,
319            only:       'chrome'
320        })
321            .catch(function (errs) {
322                expect(errs[1]).contains('A call to an async function is not awaited.');
323                expect(errs[1]).contains('> 136 |    t.expect(42).eql(43); 137 |});');
324            });
325    });
326
327    it('Should raise error if "timeout" option is not a number', () => {
328        return runTests('./testcafe-fixtures/assertions-test.js', '"timeout" is not a number', {
329            shouldFail: true,
330            only:       'chrome'
331        })
332            .catch(function (errs) {
333                expect(errs[0]).contains('The "timeout" option is expected to be a positive integer, but it was string.');
334                expect(errs[0]).contains("> 140 |    await t.expect(42).eql(43, { timeout: 'hey' });");
335            });
336    });
337
338    it('Should provide "timeout" option', function () {
339        return runTests('./testcafe-fixtures/assertions-test.js', '"timeout" option', {
340            only:             'chrome',
341            assertionTimeout: 0
342        });
343    });
344
345    it('Should retry assertion for ClientFunction results', function () {
346        return runTests('./testcafe-fixtures/assertions-test.js', 'ClientFunction result assertion', { only: 'chrome' });
347    });
348
349    it('Should raise an error if assertion was called without method', () => {
350        return runTests('./testcafe-fixtures/assertions-test.js', 'Assertion without method call',
351            { shouldFail: true, only: 'chrome' })
352            .catch(errs => {
353                expect(errs.length).eql(1);
354                expect(errs[0]).to.contains('An assertion method is not specified.');
355                expect(errs[0]).to.contains('> 172 |    await t.expect();');
356            });
357    });
358});
359
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)