How to use _generateDriverError method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

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

js.js

Source: js.js Github

copy
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3    if (k2 === undefined) k2 = k;
4    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6    if (k2 === undefined) k2 = k;
7    o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10    Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12    o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15    if (mod && mod.__esModule) return mod;
16    var result = {};
17    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18    __setModuleDefault(result, mod);
19    return result;
20};
21var __importDefault = (this && this.__importDefault) || function (mod) {
22    return (mod && mod.__esModule) ? mod : { "default": mod };
23};
24Object.defineProperty(exports, "__esModule", { value: true });
25exports.rewriteJs = exports._rewriteJsUnsafe = exports.rewriteJsSourceMap = void 0;
26const astTypes = __importStar(require("ast-types"));
27const debug_1 = __importDefault(require("debug"));
28const js_rules_1 = require("./js-rules");
29const recast = __importStar(require("recast"));
30const sourceMaps = __importStar(require("./util/source-maps"));
31const debug = (0, debug_1.default)('cypress:rewriter:js');
32const defaultPrintOpts = {
33    // will only affect reprinted quotes
34    quote: 'single',
35};
36function _generateDriverError(url, err) {
37    const args = JSON.stringify({
38        errMessage: err.message,
39        errStack: err.stack,
40        url,
41    });
42    return `window.top.Cypress.utils.throwErrByPath('proxy.js_rewriting_failed', { args: ${args} })`;
43}
44function rewriteJsSourceMap(url, js, inputSourceMap) {
45    try {
46        const { sourceFileName, sourceMapName, sourceRoot } = sourceMaps.getPaths(url);
47        const ast = recast.parse(js, { sourceFileName });
48        astTypes.visit(ast, js_rules_1.jsRules);
49        return recast.print(ast, Object.assign({ inputSourceMap,
50            sourceMapName,
51            sourceRoot }, defaultPrintOpts)).map;
52    }
53    catch (err) {
54        debug('error while parsing JS %o', { err, js: js.slice ? js.slice(0, 500) : js });
55        return { err };
56    }
57}
58exports.rewriteJsSourceMap = rewriteJsSourceMap;
59function _rewriteJsUnsafe(url, js, deferSourceMapRewrite) {
60    const ast = recast.parse(js);
61    try {
62        astTypes.visit(ast, js_rules_1.jsRules);
63    }
64    catch (err) {
65        // if visiting fails, it points to a bug in our rewriting logic, so raise the error to the driver
66        return _generateDriverError(url, err);
67    }
68    const { code } = recast.print(ast, defaultPrintOpts);
69    if (!deferSourceMapRewrite) {
70        // no sourcemaps
71        return sourceMaps.stripMappingUrl(code);
72    }
73    // get an ID that can be used to lazy-generate the source map later
74    const sourceMapId = deferSourceMapRewrite({ url, js });
75    return sourceMaps.urlFormatter(
76    // using a relative URL ensures that required cookies + other headers are sent along
77    // and can be reused if the user's sourcemap requires an HTTP request to be made
78    `/__cypress/source-maps/${sourceMapId}.map`, code);
79}
80exports._rewriteJsUnsafe = _rewriteJsUnsafe;
81function rewriteJs(url, js, deferSourceMapRewrite) {
82    try {
83        // rewriting can throw on invalid JS or if there are bugs in the js-rules, so always wrap it
84        return _rewriteJsUnsafe(url, js, deferSourceMapRewrite);
85    }
86    catch (err) {
87        debug('error while parsing JS %o', { err, js: js.slice ? js.slice(0, 500) : js });
88        return js;
89    }
90}
91exports.rewriteJs = rewriteJs;
92
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 Cypress 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)