Skip to content

Commit 2ac056b

Browse files
committed
First valuable commit
1 parent 40a9c54 commit 2ac056b

12 files changed

+1949
-1
lines changed

README.md

+60-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,61 @@
1-
# cypress-time
1+
## cypress-time
22
Record timings in Cypress
3+
4+
## Why?
5+
Sometimes you want to record how long a series of tasks/actions take. When developing, you might use browser-based time spans to give you feedback. This Cypress plugin will give you a nice way to do timings right in your test! This library uses the [User Timings API](https://developer.mozilla.org/en-US/docs/Web/API/User_Timing_API), so the time span will show up if you run a performance profile while the test is active (can only be done using `cypress open` which is dev mode).
6+
7+
## Installation
8+
To install the module:
9+
```
10+
npm install cypress-time --save-dev
11+
```
12+
13+
To get the plugin working with Cypress:
14+
```js
15+
// cypress/support/index.js or cypress/support/index.ts
16+
import 'cypress-time'
17+
```
18+
19+
## Use
20+
In your spec files, `cy.time(label)` will start a time span while `cy.timeEnd(label)` will end a time span. `cy.timeEnd` will return a [PerformanceEntry](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceEntry).
21+
22+
```js
23+
// start the 'test' time span
24+
cy.time('test')
25+
26+
cy.contains('button', 'Submit').click()
27+
28+
// wait until submit is complete - assume a backend is involved
29+
cy.get('Loading').should('not.exist')
30+
31+
// ends the 'test' time span, printing the duration
32+
cy.timeEnd('test')
33+
```
34+
35+
If you want to do something with the time output, the [PerformanceEntry](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceEntry) is returned. You can save this to a file, send it off to a service, or whatever you'd like. `startTime` and `duration` may be of the most interest.
36+
37+
```js
38+
cy.time('test')
39+
40+
// do something
41+
42+
cy.timeEnd('test').then(entry => {
43+
console.log(entry.startTime) // start in ms elapsed since page loaded
44+
console.log(entry.duration) // duration between `time` and `timeEnd`
45+
})
46+
```
47+
48+
**Here's a screenshot of what the command log will look like:**
49+
![Cypress Command Log](./time-span-command-log.png)
50+
51+
**Here's a screenshot of the Performance tools with the 'test' time span:**
52+
![Performance Tab of Time Span](./time-span-performance.png)
53+
54+
## Typescript support
55+
This plugin comes with its own type definitions. If you're using Typescript already and your support index file is a Typescript file and your tsconfig.json is configured to include *.ts files in the support directory, nothing more needs to be done. If you don't have your project set up this way, add the following to your tsconfig (ideally in `cypress/tsconfig.json`):
56+
57+
```json
58+
{
59+
"types" ["cypress", "cypress-time"]
60+
}
61+
```

cypress.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

cypress/fixtures/example.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Using fixtures to represent data",
3+
"email": "[email protected]",
4+
"body": "Fixtures are a great way to mock data for responses to routes"
5+
}

cypress/integration/time.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference types="cypress" />
2+
/// <reference path="../../index.d.ts" />
3+
4+
describe('time', () => {
5+
it('should show timings', () => {
6+
cy.time('test')
7+
cy.wrap('foo').wait(300)
8+
cy.timeEnd('test')
9+
.its('duration')
10+
.should('be.a', 'number')
11+
})
12+
})

cypress/plugins/index.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference types="cypress" />
2+
// ***********************************************************
3+
// This example plugins/index.js can be used to load plugins
4+
//
5+
// You can change the location of this file or turn off loading
6+
// the plugins file with the 'pluginsFile' configuration option.
7+
//
8+
// You can read more here:
9+
// https://on.cypress.io/plugins-guide
10+
// ***********************************************************
11+
12+
// This function is called when a project is opened or re-opened (e.g. due to
13+
// the project's config changing)
14+
15+
/**
16+
* @type {Cypress.PluginConfig}
17+
*/
18+
module.exports = (on, config) => {
19+
// `on` is used to hook into various events Cypress emits
20+
// `config` is the resolved Cypress config
21+
}

cypress/support/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// import the time commands
2+
import '../../index'

index.d.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference types="cypress" />
2+
3+
namespace Cypress {
4+
interface Chainable<Subject = any> {
5+
/**
6+
* Starts a time span using the User Timings API. A `cy.time`
7+
* should always be followed by a `cy.timeEnd` with the same label.
8+
* This command is useful for getting durations for a series of
9+
* commands and getting performance profiles out of your application
10+
* given some series of commands you want to profile.
11+
* @param label Name of the time span you wish to capture
12+
*/
13+
time(label: string): Chainable<null>;
14+
/**
15+
* Ends a time span using the User Timings API.
16+
* @param label Name of the time span you wish to capture
17+
*/
18+
timeEnd(label: string): Chainable<PerformanceEntry>;
19+
}
20+
}

index.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Cypress.Commands.add('time', name => {
2+
performance.mark(`${name}_start`);
3+
cy.state(name, performance.now());
4+
Cypress.log({
5+
name: 'time',
6+
}).end();
7+
return null;
8+
});
9+
10+
Cypress.Commands.add('timeEnd', name => {
11+
performance.mark(`${name}_end`);
12+
performance.measure(name, `${name}_start`, `${name}_end`)
13+
const data = performance.getEntriesByName(name)[0]
14+
console.log(data)
15+
Cypress.log({
16+
name: 'timeEnd',
17+
message: [name, `${data.duration.toFixed(2)}ms`],
18+
});
19+
return data;
20+
});

0 commit comments

Comments
 (0)