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

compile-client-function.js

Source: compile-client-function.js Github

copy
1import hammerhead from 'testcafe-hammerhead';
2import asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
3import { noop } from 'lodash';
4import loadBabelLibs from './babel/load-libs';
5import { ClientFunctionAPIError } from '../errors/runtime';
6import { RUNTIME_ERRORS } from '../errors/types';
7import formatBabelProducedCode from './babel/format-babel-produced-code';
8
9const ANONYMOUS_FN_RE                = /^function\s*\*?\s*\(/;
10const ES6_OBJ_METHOD_NAME_RE         = /^(\S+?)\s*\(/;
11const USE_STRICT_RE                  = /^('|")use strict('|");?/;
12const TRAILING_SEMICOLON_RE          = /;\s*$/;
13const REGENERATOR_FOOTPRINTS_RE      = /(_index\d+\.default|_regenerator\d+\.default|regeneratorRuntime)\.wrap\(function _callee\$\(_context\)/;
14const ASYNC_TO_GENERATOR_OUTPUT_CODE = formatBabelProducedCode(asyncToGenerator(noop).toString());
15
16
17function getBabelOptions () {
18    const { presetEnvForClientFunction, transformForOfAsArray } = loadBabelLibs();
19
20    return {
21        presets:       [{ plugins: [transformForOfAsArray] }, presetEnvForClientFunction],
22        sourceMaps:    false,
23        retainLines:   true,
24        ast:           false,
25        babelrc:       false,
26        highlightCode: false
27    };
28}
29
30function downgradeES (fnCode) {
31    const { babel } = loadBabelLibs();
32
33    const opts     = getBabelOptions();
34    const compiled = babel.transform(fnCode, opts);
35
36    return compiled.code
37        .replace(USE_STRICT_RE, '')
38        .trim();
39}
40
41function getDependenciesDefinition (dependencies) {
42    return Object
43        .keys(dependencies)
44        .reduce((code, name) => {
45            return code + `var ${name}=__dependencies$['${name}'];`;
46        }, '');
47}
48
49function makeFnCodeSuitableForParsing (fnCode) {
50    // NOTE: 'function() {}' -> '(function() {})'
51    if (ANONYMOUS_FN_RE.test(fnCode))
52        return `(${fnCode})`;
53
54    // NOTE: 'myFn () {}' -> 'function myFn() {}'
55    const match = fnCode.match(ES6_OBJ_METHOD_NAME_RE);
56
57    if (match && match[1] !== 'function')
58        return `function ${fnCode}`;
59
60    return fnCode;
61}
62
63function containsAsyncToGeneratorOutputCode (code) {
64    const formattedCode = formatBabelProducedCode(code);
65
66    return formattedCode.includes(ASYNC_TO_GENERATOR_OUTPUT_CODE);
67}
68
69export default function compileClientFunction (fnCode, dependencies, instantiationCallsiteName, compilationCallsiteName) {
70    if (containsAsyncToGeneratorOutputCode(fnCode))
71        throw new ClientFunctionAPIError(compilationCallsiteName, instantiationCallsiteName, RUNTIME_ERRORS.regeneratorInClientFunctionCode);
72
73    fnCode = makeFnCodeSuitableForParsing(fnCode);
74
75    // NOTE: we need to recompile ES6 code for the browser if we are on newer versions of Node.
76    fnCode = downgradeES(fnCode);
77    fnCode = hammerhead.processScript(fnCode, false);
78
79    // NOTE: check compiled code for regenerator injection
80    if (REGENERATOR_FOOTPRINTS_RE.test(fnCode))
81        throw new ClientFunctionAPIError(compilationCallsiteName, instantiationCallsiteName, RUNTIME_ERRORS.regeneratorInClientFunctionCode);
82
83    if (!TRAILING_SEMICOLON_RE.test(fnCode))
84        fnCode += ';';
85
86    const dependenciesDefinition = dependencies ? getDependenciesDefinition(dependencies) : '';
87
88    return `(function(){${dependenciesDefinition} return ${fnCode}})();`;
89}
90
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)