Skip to content
This repository was archived by the owner on Feb 27, 2025. It is now read-only.

Commit c2a4ece

Browse files
author
Brent Schmidt
committed
feat: Support Cypress 10
BREAKING CHANGE: In Cypress 10, the 'integrationFolder' was removed from the config. This value had been used to determine where to save and load the request fixtures, so a new approach has been implemented and the plugin cannot find previously created fixtures. BREAKING CHANGE: The file extension of the recorded fixtures was changed to '.cy-playback' to make it easier to identify that these files belong to the plugin.
1 parent a2b9796 commit c2a4ece

27 files changed

+3093
-1095
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
.cy-request binary
1+
.cy-playback binary

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ A "stale" entity becomes "fresh" when...
483483
### The Recorded Fixtures File
484484

485485
All the captured responses for a test are grouped together into a single
486-
`.cy-requests` file that is saved to the Cypress fixtures folder. A subdirectory
486+
`.cy-playbacks` file that is saved to the Cypress fixtures folder. A subdirectory
487487
is created in the fixtures folder for each spec file. Within that folder, a
488488
fixture file is created for each test in the spec file. The file name is a
489489
combination of the test's name and any `describe` blocks it is nested under.
@@ -509,9 +509,9 @@ Consider the following example spec file.
509509
```
510510
* **Generated Fixture Files:**
511511
```bash
512-
./cypress/fixtures/app/basic-spec/app-works.cy-requests
513-
./cypress/fixtures/app/basic-spec/app-still-works.cy-requests
514-
./cypress/fixtures/app/basic-spec/app-another-language-works.cy-requests
512+
./cypress/fixtures/app/basic-spec/app-works.cy-playbacks
513+
./cypress/fixtures/app/basic-spec/app-still-works.cy-playbacks
514+
./cypress/fixtures/app/basic-spec/app-another-language-works.cy-playbacks
515515
```
516516

517517
> ⚠️ As can seen, if the location of the spec file or the structure or name of
@@ -521,7 +521,7 @@ Consider the following example spec file.
521521
522522
#### File Format
523523

524-
The `.cy-requests` file is a binary file, which is created by JSON stringifying
524+
The `.cy-playbacks` file is a binary file, which is created by JSON stringifying
525525
the request matchers and requests and compressing that output with Node's
526526
`zlib.deflate` function. There are two reasons for this approach:
527527

lib/.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33

44
!commands/**/*
5-
!plugins/**/*
5+
!tasks/**/*
66
!addCommands.js
77
!addTasks.js
88

lib/addTasks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { readRequestsFromDisk } = require('./plugins/functions/readRequestsFromDisk');
2-
const { writeRequestsToDisk } = require('./plugins/functions/writeRequestsToDisk');
1+
const { readRequestsFromDisk } = require('./tasks/functions/readRequestsFromDisk');
2+
const { writeRequestsToDisk } = require('./tasks/functions/writeRequestsToDisk');
33

44
module.exports = (on, config) => {
55
on('task', {

lib/commands/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const ENV_MODE = 'PLAYBACK_MODE';
33
/**
44
* The current version of the serialized requests and responses.
55
*/
6-
const SERIALIZE_VERSION = 2;
6+
const SERIALIZE_VERSION = 3;
77

88
module.exports = {
99
CONFIG_KEY,

lib/commands/functions/getTestMetadata.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@
44
* @returns {{ file: string, title: string }}
55
*/
66
function getTestMetadata(context) {
7-
const test = context?.currentTest ?? context?.test ?? null;
8-
if (!test) {
9-
throw new Error('No "test" in context');
7+
// The data we need is spread across two different properties in the context.
8+
// - `currentTest` represents the test that is being run. It tends to be
9+
// present more reliably.
10+
// - `test` is more specific and may actually be tied to a hook, such as
11+
// beforeEach / afterEach. It may not always be present. However, if
12+
// present, it's `file` property is a better relative path to the file for
13+
// the current test suite.
14+
const { currentTest, test } = context ?? {};
15+
if (!currentTest) {
16+
throw new Error('No "currentTest" in context');
1017
}
1118

12-
const invocationDetails = test.invocationDetails || test.parent?.invocationDetails;
13-
const file = invocationDetails?.absoluteFile;
19+
const invocationDetails = currentTest.invocationDetails ?? currentTest.parent?.invocationDetails;
20+
const file = test?.file ?? currentTest?.file ?? invocationDetails?.relativeFile;
1421

15-
let titles = [test.title];
16-
let parent = test.parent;
22+
let titles = [currentTest.title];
23+
let parent = currentTest.parent;
1724
while (!parent.root) {
1825
titles.push(parent.title);
1926
parent = parent.parent;
@@ -28,11 +35,11 @@ function getTestMetadata(context) {
2835
}
2936
return {
3037
file,
31-
state: test.state,
38+
state: currentTest.state,
3239
title
3340
};
3441
}
3542

3643
module.exports = {
3744
getTestMetadata
38-
};
45+
};
Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
const { getTestMetadata } = require('../getTestMetadata');
22

3-
function getMockContext(testProp = 'test') {
3+
function getMockContext() {
44
return {
5-
[testProp]: {
5+
test: {
6+
file: 'relative/path/to/file.js',
7+
},
8+
currentTest: {
69
title: 'does thing',
710
state: 'passed',
811
parent: {
@@ -16,7 +19,7 @@ function getMockContext(testProp = 'test') {
1619
}
1720
},
1821
invocationDetails: {
19-
absoluteFile: '/path/to/file.js'
22+
relativeFile: 'relative/path/to/file.js'
2023
}
2124
}
2225
};
@@ -25,25 +28,28 @@ function getMockContext(testProp = 'test') {
2528
describe('getTestMetadata', () => {
2629
describe('returns expected values', () => {
2730
const expected = {
28-
file: '/path/to/file.js',
31+
file: 'relative/path/to/file.js',
2932
state: 'passed',
3033
title: 'grandparent-parent-does thing'
3134
};
3235

33-
it('when context uses "currentTest"', () => {
34-
expect(getTestMetadata(getMockContext('currentTest'))).to.eql(expected);
36+
it('works with a fully defined context.', () => {
37+
expect(getTestMetadata(getMockContext())).to.eql(expected);
3538
});
3639

37-
it('when context uses "test"', () => {
38-
expect(getTestMetadata(getMockContext('test'))).to.eql(expected);
40+
it('works with a missing "test" property in the context.', () => {
41+
const context = getMockContext();
42+
delete context.test;
43+
expect(getTestMetadata(context)).to.eql(expected);
3944
});
4045
});
4146

4247
describe('throws exceptions', () => {
4348
it('when missing "file"', () => {
4449
// Arrange
4550
const context = getMockContext();
46-
delete context.test.invocationDetails;
51+
delete context.test.file;
52+
delete context.currentTest.invocationDetails;
4753

4854
// Act / Assert
4955
expect(() => getTestMetadata(context)).to.throw()
@@ -53,17 +59,17 @@ describe('getTestMetadata', () => {
5359
it('when missing "title"', () => {
5460
// Arrange
5561
const context = getMockContext();
56-
delete context.test.title;
57-
context.test.parent = { root: true };
62+
delete context.currentTest.title;
63+
context.currentTest.parent = { root: true };
5864

5965
// Act / Assert
6066
expect(() => getTestMetadata(context)).to.throw()
6167
.property('message').to.match(/Could not determine "title"/);
6268
});
6369

64-
it('when no "test" object on the context.', () => {
70+
it('when no "currentTest" object on the context.', () => {
6571
expect(() => getTestMetadata()).to.throw()
66-
.property('message').to.match(/No "test" in context/);
72+
.property('message').to.match(/No "currentTest" in context/);
6773
});
6874
});
6975
});

0 commit comments

Comments
 (0)