Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Svish committed Dec 13, 2019
0 parents commit c052e84
Show file tree
Hide file tree
Showing 7 changed files with 4,229 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*]
end_of_line = lf
indent_style = space
indent_size = 2
charset = utf-8
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"endOfLine": "lf",
"quoteProps": "consistent",
"singleQuote": true,
"trailingComma": "es5"
}
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# cypress-helper-getcy

A simple [Cypress](https://www.cypress.io/) [command](https://docs.cypress.io/api/cypress-api/custom-commands.html) for getting elements via `data-cy` attributes.

[![npm version](https://img.shields.io/npm/v/cypress-helper-getcy.svg?style=flat-square) ![npm downloads](https://img.shields.io/npm/dm/cypress-helper-getcy?style=flat-square)](https://www.npmjs.com/package/cypress-helper-getcy)

## Inspiration

- [Cypress Best Practices](https://docs.cypress.io/guides/references/best-practices.html#Selecting-Elements)

### Why not just `cy.get('[data-cy=submit]')`?

Well, I like clean tests, and I found both the test code and logs to be rather ugly and harder to read when doing that. So I wanted something cleaner, and made this, which cleans up both the code and the log. 👍

## Setup

### 1. Install

```shell
npm install --save-dev cypress-helper-getcy
```

### 2. Include

```js
// E.g. in cypress/support/index.js
include 'cypress-helper-getcy';
```

## Usage

```html
<div data-cy="my-test-subject"></div>
```

```js
it('finds my test subject', () => {
cy.getCy('my-test-subject').should('exist');
});
```

_**Note:** See [tests](test/tests/getCy.ts) for more examples._
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const DELAY = 1500;
const LOG_TAG = '[cypress-hmr-restarter]';

const clickRestart = win => {
const btn = win.top.document.querySelector('.restart');
btn && btn.click();
};

Cypress.on('window:load', win => {
const host = Cypress.config().baseUrl.replace(/https?/, 'wss');
const socket = new WebSocket(`${host}/sockjs-node`);

socket.onopen = () => console.debug(LOG_TAG, 'Connected to HMR socket');
socket.onclose = () => console.debug(LOG_TAG, 'Disconnected from HMR socket');
socket.onmessage = e => {
const { type } = JSON.parse(e.data);
switch (type) {
case 'invalid':
console.debug(LOG_TAG, `Restarting due to HMR in ${DELAY}ms...`);
// TODO: Debounce this somehow?
setTimeout(() => clickRestart(win), DELAY);
break;
}
};
});
Loading

0 comments on commit c052e84

Please sign in to comment.