cypress-replay
Advanced tools
Comparing version 1.0.12 to 1.0.13
@@ -18,3 +18,3 @@ "use strict"; | ||
request.on("after:response", (response) => { | ||
requestCollection.pushRequest(request, { | ||
requestCollection.pushIncomingRequest(request, { | ||
body: response.body, | ||
@@ -21,0 +21,0 @@ headers: (0, sanitizeHeaders_1.default)(response.headers), |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -7,3 +30,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
const RequestCollection_1 = __importDefault(require("../utility/RequestCollection")); | ||
const createFixtureFilename_1 = __importDefault(require("../utility/createFixtureFilename")); | ||
const createFixtureFilename_1 = __importStar(require("../utility/createFixtureFilename")); | ||
const EnvComponentManager_1 = __importDefault(require("../utility/EnvComponentManager")); | ||
@@ -16,5 +39,22 @@ const Logger_1 = __importDefault(require("../utility/Logger")); | ||
logger = new Logger_1.default(); | ||
cy.readFile((0, createFixtureFilename_1.default)(Cypress.config().fixturesFolder, Cypress.spec.name, Cypress.currentTest.titlePath)).then(fileContents => { | ||
return new RequestCollection_1.default(dynamicComponentManager, fileContents, logger); | ||
}).then(requestCollection => { | ||
// Create a request collection from the fixture fix. | ||
cy.readFile((0, createFixtureFilename_1.default)(Cypress.config().fixturesFolder, Cypress.spec.name, Cypress.currentTest.titlePath)) | ||
.then(fileContents => { | ||
const collection = new RequestCollection_1.default(dynamicComponentManager, logger); | ||
collection.appendFromFixture(fileContents); | ||
return collection; | ||
}) | ||
// Optionally append an additional hand-crafted fixture file to merge into the request collection, | ||
// for cases where tests are non-deterministic or may otherwise require additional fixed requests in the | ||
// same collection format. | ||
.then(requestCollection => cy.wrap(new Promise((resolve) => { | ||
cy.readFile((0, createFixtureFilename_1.createMergedFixtureFilename)(Cypress.config().fixturesFolder, Cypress.spec.name, Cypress.currentTest.titlePath)).should(mergeFixture => { | ||
if (mergeFixture) { | ||
requestCollection.appendFromFixture(mergeFixture); | ||
} | ||
resolve(requestCollection); | ||
}); | ||
}), { log: false })) | ||
// Start interception based on the resolved request collection. | ||
.then(requestCollection => { | ||
cy.intercept(new RegExp(configuration.interceptPattern || ".*"), (req) => { | ||
@@ -21,0 +61,0 @@ const fixtureResponse = requestCollection.shiftRequest(req); |
export default function createFixtureFilename(fixtureFolder: string, specName: string, specPath: string[]): string; | ||
export declare function createMergedFixtureFilename(fixtureFolder: string, specName: string, specPath: string[]): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createMergedFixtureFilename = void 0; | ||
function createFixtureFilename(fixtureFolder, specName, specPath) { | ||
return fixtureFilename(fixtureFolder, specName, specPath, 'json'); | ||
} | ||
exports.default = createFixtureFilename; | ||
function createMergedFixtureFilename(fixtureFolder, specName, specPath) { | ||
return fixtureFilename(fixtureFolder, specName, specPath, 'merged.json'); | ||
} | ||
exports.createMergedFixtureFilename = createMergedFixtureFilename; | ||
function fixtureFilename(fixtureFolder, specName, specPath, extension) { | ||
const pathComponents = [ | ||
fixtureFolder, | ||
sanitizeForFileSystem(specName), | ||
`${specPath.map(sanitizeForFileSystem).join('-')}.json`, | ||
`${specPath.map(sanitizeForFileSystem).join('-')}.${extension}`, | ||
]; | ||
return pathComponents.join('/'); | ||
} | ||
exports.default = createFixtureFilename; | ||
function sanitizeForFileSystem(input) { | ||
@@ -13,0 +21,0 @@ input = input.replaceAll(' ', '-'); |
@@ -8,2 +8,7 @@ import { CyHttpMessages, StaticResponse } from "cypress/types/net-stubbing"; | ||
}; | ||
export type RequestMapFixture = { | ||
[key: string]: (StaticResponse & { | ||
insertAtIndex?: number; | ||
})[]; | ||
}; | ||
export default class RequestCollection { | ||
@@ -13,5 +18,6 @@ private envComponentManager; | ||
private logger; | ||
constructor(envComponentManager: EnvComponentManager, requests?: RequestMap, logger?: LoggerInterface); | ||
pushRequest(request: IncomingRequest, response: StaticResponse): void; | ||
constructor(envComponentManager: EnvComponentManager, logger?: LoggerInterface); | ||
appendFromFixture(fixture: RequestMapFixture): void; | ||
pushIncomingRequest(request: IncomingRequest, response: StaticResponse): void; | ||
shiftRequest(request: IncomingRequest): StaticResponse | null; | ||
} |
@@ -9,8 +9,26 @@ "use strict"; | ||
class RequestCollection { | ||
constructor(envComponentManager, requests, logger) { | ||
constructor(envComponentManager, logger) { | ||
this.envComponentManager = envComponentManager; | ||
this.requests = requests || {}; | ||
this.logger = logger || new Logger_1.default(); | ||
this.requests = {}; | ||
} | ||
pushRequest(request, response) { | ||
appendFromFixture(fixture) { | ||
Object.keys(fixture).forEach(key => { | ||
if (!this.requests[key]) { | ||
this.requests[key] = []; | ||
} | ||
fixture[key].forEach(request => { | ||
// Allow requests in fixture files to specify an index where they'll be inserted. This gives | ||
// some control over where manually authored fixtures are inserted, otherwise they'll be | ||
// appended in the order they are encountered. | ||
if (request.insertAtIndex) { | ||
this.requests[key].splice(request.insertAtIndex, 0, request); | ||
} | ||
else { | ||
this.requests[key].push(request); | ||
} | ||
}); | ||
}); | ||
} | ||
pushIncomingRequest(request, response) { | ||
const key = this.envComponentManager.removeDynamicComponents((0, createRequestKey_1.default)(request)); | ||
@@ -17,0 +35,0 @@ if (!this.requests[key]) { |
{ | ||
"name": "cypress-replay", | ||
"version": "1.0.12", | ||
"version": "1.0.13", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "types": "lib/index.d.ts", |
@@ -82,2 +82,23 @@ Cypress Replay | ||
## Adding additional responses | ||
In some cases tests may not be deterministic, or you may have additional requests you would also like to stub. You can | ||
optionally provide these requests in a handcrafted fixture instead of implementing an additional call to `cy.intercept`. | ||
These files are loaded from the same location as recorded fixture files, but with the `.merged.json` extension. For | ||
example, for a recorded fixture file in `./cypress/fixtures/my-spec.ts/Spec-Name-test-working.json` you could optionally | ||
create the `./cypress/fixtures/my-spec.ts/Spec-Name-test-working.merged.json` file (in the same format), to stub | ||
additional requests. | ||
One additional and optional key named `insertAtIndex` exists, which allows the author to specify where in the list of | ||
responses for a given endpoint the fixed response will be inserted. | ||
``` | ||
{ | ||
"GET:{API_URL}/comments/1": [ | ||
{ | ||
"insertAtIndex": 1, | ||
"body": { | ||
... | ||
``` | ||
## Best practices | ||
@@ -84,0 +105,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19942
358
108