cypress-replay
Advanced tools
Comparing version 1.0.11 to 1.0.12
export type ReplayConfig = { | ||
interceptPattern: string; | ||
interceptPattern?: string; | ||
dynamicRequestEnvComponents?: Array<string>; | ||
responseDelayOverride?: number; | ||
}; | ||
@@ -9,2 +10,2 @@ export declare enum ReplayMode { | ||
} | ||
export default function enableCypressReplay(mode?: ReplayMode | null): void; | ||
export default function enableCypressReplay(mode?: ReplayMode | null, config?: ReplayConfig): void; |
@@ -9,2 +9,3 @@ "use strict"; | ||
const replayRequests_1 = __importDefault(require("./replay/replayRequests")); | ||
const loadConfiguration_1 = __importDefault(require("./utility/loadConfiguration")); | ||
var ReplayMode; | ||
@@ -15,11 +16,16 @@ (function (ReplayMode) { | ||
})(ReplayMode = exports.ReplayMode || (exports.ReplayMode = {})); | ||
function enableCypressReplay(mode = null) { | ||
function enableCypressReplay(mode = null, config = {}) { | ||
const replayMode = mode !== null ? mode : (Cypress.env('REPLAY_RECORD_REQUESTS') ? ReplayMode.Recording : ReplayMode.Replaying); | ||
// Allow the configuration to be defined globally and then to be overridden on a test by test basis. | ||
const configuration = { | ||
...(0, loadConfiguration_1.default)(), | ||
...config, | ||
}; | ||
if (replayMode === ReplayMode.Recording) { | ||
(0, recordRequests_1.default)(); | ||
(0, recordRequests_1.default)(configuration); | ||
} | ||
if (replayMode === ReplayMode.Replaying) { | ||
(0, replayRequests_1.default)(); | ||
(0, replayRequests_1.default)(configuration); | ||
} | ||
} | ||
exports.default = enableCypressReplay; |
@@ -1,1 +0,2 @@ | ||
export default function recordRequests(): void; | ||
import { ReplayConfig } from "../index"; | ||
export default function recordRequests(configuration: ReplayConfig): void; |
@@ -6,3 +6,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const loadConfiguration_1 = __importDefault(require("../utility/loadConfiguration")); | ||
const RequestCollection_1 = __importDefault(require("../utility/RequestCollection")); | ||
@@ -12,9 +11,8 @@ const sanitizeHeaders_1 = __importDefault(require("../utility/sanitizeHeaders")); | ||
const EnvComponentManager_1 = __importDefault(require("../utility/EnvComponentManager")); | ||
function recordRequests() { | ||
function recordRequests(configuration) { | ||
let requestCollection; | ||
const configuration = (0, loadConfiguration_1.default)(); | ||
const dynamicComponentManager = EnvComponentManager_1.default.fromEnvironment(configuration.dynamicRequestEnvComponents || [], Cypress.env); | ||
beforeEach(() => { | ||
requestCollection = new RequestCollection_1.default(dynamicComponentManager); | ||
cy.intercept(new RegExp((0, loadConfiguration_1.default)().interceptPattern), (request) => { | ||
cy.intercept(new RegExp(configuration.interceptPattern || ".*"), (request) => { | ||
const startTime = Date.now(); | ||
@@ -21,0 +19,0 @@ request.on("after:response", (response) => { |
@@ -1,1 +0,2 @@ | ||
export default function recordRequests(): void; | ||
import { ReplayConfig } from "../index"; | ||
export default function recordRequests(configuration: ReplayConfig): void; |
@@ -6,3 +6,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const loadConfiguration_1 = __importDefault(require("../utility/loadConfiguration")); | ||
const RequestCollection_1 = __importDefault(require("../utility/RequestCollection")); | ||
@@ -12,4 +11,3 @@ const createFixtureFilename_1 = __importDefault(require("../utility/createFixtureFilename")); | ||
const Logger_1 = __importDefault(require("../utility/Logger")); | ||
function recordRequests() { | ||
const configuration = (0, loadConfiguration_1.default)(); | ||
function recordRequests(configuration) { | ||
const dynamicComponentManager = EnvComponentManager_1.default.fromEnvironment(configuration.dynamicRequestEnvComponents || [], Cypress.env); | ||
@@ -22,6 +20,9 @@ let logger; | ||
}).then(requestCollection => { | ||
cy.intercept(new RegExp((0, loadConfiguration_1.default)().interceptPattern), (req) => { | ||
cy.intercept(new RegExp(configuration.interceptPattern || ".*"), (req) => { | ||
const fixtureResponse = requestCollection.shiftRequest(req); | ||
if (fixtureResponse) { | ||
req.reply(fixtureResponse); | ||
req.reply({ | ||
...fixtureResponse, | ||
delay: configuration.responseDelayOverride !== undefined ? configuration.responseDelayOverride : fixtureResponse.delay, | ||
}); | ||
} | ||
@@ -28,0 +29,0 @@ }); |
{ | ||
"name": "cypress-replay", | ||
"version": "1.0.11", | ||
"version": "1.0.12", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "types": "lib/index.d.ts", |
@@ -23,3 +23,2 @@ Cypress Replay | ||
interceptPattern: ".*", | ||
dynamicRequestEnvComponents: [], | ||
} | ||
@@ -43,5 +42,9 @@ }); | ||
Configuration can either be set globally (in the `cypress.config.ts` file as above), or be passed in to the | ||
`enableCypressReplay` function, used to enable the replay functionality. Each configuration option is optional and | ||
documented below: | ||
``` | ||
/** | ||
* A Regex that matches all the endpoints you intend to replay. | ||
* A Regex that matches all the endpoints you intend to record and replay. | ||
*/ | ||
@@ -51,7 +54,17 @@ interceptPattern: "jsonplaceholder\.cypress\.io|some-other-endpoint\.com", | ||
/** | ||
* A list of environment variables that should be substituted in your replay files - this is helpful if your API | ||
* endpoints are defined with environment variables and you would like a deterministic replay, regardless of how each | ||
* A list of environment variables that should be substituted in your replay | ||
* files - this is helpful if your API endpoints are defined with environment | ||
* variables and you would like a deterministic replay, regardless of how each | ||
* is configured. | ||
*/ | ||
dynamicRequestEnvComponents: ["REACT_APP_MY_API_ENDPOINT"], | ||
/** | ||
* To ensure tests are deterministic, the time taken for each request during | ||
* recording is used as a delay when replaying. For some applications, replaying | ||
* with an accurate delay may not matter and it's preferable for tests to run as | ||
* fast as possible. In this case, you may specify an override (with 0 being | ||
* instant) for how long a response is delayed during a replay. | ||
*/ | ||
responseDelayOverride: 20, | ||
``` | ||
@@ -64,4 +77,10 @@ | ||
* Passing an environment variable while starting the cypress runner: `CYPRESS_REPLAY_RECORD_REQUESTS=1 yarn run cy` | ||
* Passing an argument to `enableCypressReplay`: `enableCypressReplay(ReplayMode.Recording | ReplayMode.Replaying)` | ||
* Passing an argument to `enableCypressReplay`: | ||
``` | ||
enableCypressReplay() // Uses the "CYPRESS_REPLAY_RECORD_REQUESTS" environment variable or defaults to "Replaying". | ||
enableCypressReplay(ReplayMode.Recording) // Enforces "Recording" mode. | ||
enableCypressReplay(ReplayMode.Replaying) // Enforces "Replaying" mode. | ||
``` | ||
## Best practices | ||
@@ -68,0 +87,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
15498
285
87