Best JavaScript code snippet using cypress
deferred-source-map-cache.js
Source:deferred-source-map-cache.js
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {22 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }23 return new (P || (P = Promise))(function (resolve, reject) {24 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }25 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }26 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }27 step((generator = generator.apply(thisArg, _arguments || [])).next());28 });29};30var __importDefault = (this && this.__importDefault) || function (mod) {31 return (mod && mod.__esModule) ? mod : { "default": mod };32};33Object.defineProperty(exports, "__esModule", { value: true });34exports.DeferredSourceMapCache = void 0;35const lodash_1 = __importDefault(require("lodash"));36const debug_1 = __importDefault(require("debug"));37const async_rewriters_1 = require("./async-rewriters");38const sourceMaps = __importStar(require("./util/source-maps"));39const url_1 = __importDefault(require("url"));40const debug = (0, debug_1.default)('cypress:rewriter:deferred-source-map-cache');41const caseInsensitiveGet = (obj, lowercaseProperty) => {42 for (let key of Object.keys(obj)) {43 if (key.toLowerCase() === lowercaseProperty) {44 return obj[key];45 }46 }47};48const getSourceMapHeader = (headers) => {49 // sourcemap has precedence50 // @see https://searchfox.org/mozilla-central/rev/dc4560dcaafd79375b9411fdbbaaebb0a59a93ac/devtools/shared/DevToolsUtils.js#611-61951 return caseInsensitiveGet(headers, 'sourcemap') || caseInsensitiveGet(headers, 'x-sourcemap');52};53/**54 * Holds on to data necessary to rewrite user JS to maybe generate a sourcemap at a later time,55 * potentially composed with the user's own sourcemap if one is present.56 *57 * The purpose of this is to avoid wasting CPU time and network I/O on generating, composing, and58 * sending a sourcemap along with every single rewritten JS snippet, since the source maps are59 * going to be unused and discarded most of the time.60 */61class DeferredSourceMapCache {62 constructor(requestLib) {63 this._idCounter = 0;64 this.requests = [];65 this.defer = (request) => {66 if (this._getRequestById(request.uniqueId)) {67 // prevent duplicate uniqueIds from ever existing68 throw new Error(`Deferred sourcemap key "${request.uniqueId}" is not unique`);69 }70 // remove existing requests for this URL since they will not be loaded again71 this._removeRequestsByUrl(request.url);72 this.requests.push(request);73 };74 this.requestLib = requestLib;75 }76 _removeRequestsByUrl(url) {77 lodash_1.default.remove(this.requests, { url });78 }79 _getRequestById(uniqueId) {80 return lodash_1.default.find(this.requests, { uniqueId });81 }82 _getInputSourceMap(request, headers) {83 return __awaiter(this, void 0, void 0, function* () {84 // prefer inline sourceMappingURL over headers85 const sourceMapUrl = sourceMaps.getMappingUrl(request.js) || getSourceMapHeader(request.resHeaders);86 if (!sourceMapUrl) {87 return;88 }89 // try to decode it as a base64 string90 const inline = sourceMaps.tryDecodeInlineUrl(sourceMapUrl);91 if (inline) {92 return inline;93 }94 // try to load it from the web95 const req = {96 url: url_1.default.resolve(request.url, sourceMapUrl),97 // TODO: this assumes that the sourcemap is on the same base domain, so it's safe to send the same headers98 // the browser sent for this sourcemap request - but if sourcemap is on a different domain, this will not99 // be true. need to use browser's cookiejar instead.100 headers,101 timeout: 5000,102 };103 try {104 const { body } = yield this.requestLib(req, true);105 return body;106 }107 catch (error) {108 // eslint-disable-next-line no-console109 debug('got an error loading user-provided sourcemap, serving proxy-generated sourcemap only %o', { url: request.url, headers, error });110 }111 });112 }113 resolve(uniqueId, headers) {114 return __awaiter(this, void 0, void 0, function* () {115 const request = this._getRequestById(uniqueId);116 if (!request) {117 throw new Error(`Missing request with ID '${uniqueId}'`);118 }119 if (request.sourceMap) {120 return request.sourceMap;121 }122 if (!request.js) {123 throw new Error('Missing JS for source map rewrite');124 }125 const inputSourceMap = yield this._getInputSourceMap(request, headers);126 // cache the sourceMap so we don't need to regenerate it127 request.sourceMap = yield (0, async_rewriters_1.rewriteJsSourceMapAsync)(request.url, request.js, inputSourceMap);128 delete request.js; // won't need this again129 delete request.resHeaders;130 return request.sourceMap;131 });132 }133}...
async-rewriters.js
Source:async-rewriters.js
...21 source: js,22 });23}24exports.rewriteJsAsync = rewriteJsAsync;25function rewriteJsSourceMapAsync(url, js, inputSourceMap) {26 return (0, threads_1.queueRewriting)({27 url,28 inputSourceMap,29 sourceMap: true,30 source: js,31 });32}...
Using AI Code Generation
1const fs = require('fs');2const cypress = require('cypress');3const sourceMap = require('source-map');4const sourceMapConsumer = new sourceMap.SourceMapConsumer(fs.readFileSync('main.js.map', 'utf8'));5const sourceMapGenerator = new sourceMap.SourceMapGenerator({ file: 'main.js' });6cypress.run({7 config: {8 onBeforeLoad: (win) => {9 win.rewriteJsSourceMapAsync = (map) => {10 sourceMapConsumer.eachMapping((mapping) => {11 sourceMapGenerator.addMapping({12 original: {13 },14 generated: {15 },16 });17 });18 return sourceMapGenerator.toString();19 };20 },21 },22});23it('should work', () => {24 cy.visit('index.html');25 cy.get('h1').contains('Hello World');26});
Using AI Code Generation
1describe("Test", () => {2 it("Test", () => {3 cy.get("button").click();4 cy.get("input").type("test");5 cy.get("button").click();6 cy.get("h1").should("have.text", "test");7 });8});9import { rewriteJsSourceMapAsync } from "@cypress/webpack-preprocessor";10const { addMatchImageSnapshotPlugin } = require("cypress-image-snapshot/plugin");11const webpackOptions = {12 resolve: {13 },14 module: {15 {16 },17 },18};19module.exports = (on, config) => {20 on("file:preprocessor", async (file) => {21 const options = {22 };23 return await rewriteJsSourceMapAsync(file, options);24 });25 addMatchImageSnapshotPlugin(on, config);26};27const { addMatchImageSnapshotPlugin } = require("cypress-image-snapshot/plugin");28module.exports = (on, config) => {29 addMatchImageSnapshotPlugin(on, config);30 return config;31};32{33 "compilerOptions": {34 },35}36{
Cypress is a renowned Javascript-based open-source, easy-to-use end-to-end testing framework primarily used for testing web applications. Cypress is a relatively new player in the automation testing space and has been gaining much traction lately, as evidenced by the number of Forks (2.7K) and Stars (42.1K) for the project. LambdaTest’s Cypress Tutorial covers step-by-step guides that will help you learn from the basics till you run automation tests on LambdaTest.
You can elevate your expertise with end-to-end testing using the Cypress automation framework and stay one step ahead in your career by earning a Cypress certification. Check out our Cypress 101 Certification.
Watch this 3 hours of complete tutorial to learn the basics of Cypress and various Cypress commands with the Cypress testing at LambdaTest.
Get 100 minutes of automation test minutes FREE!!