Skip to content

Commit ad97cd5

Browse files
committed
docs: add e2e testing with cypress section
1 parent 52f8da1 commit ad97cd5

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

website/docs/development/testing.mdx

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,94 @@ expect(authMethods[0].type).to.equal('local');
184184
**All new lines of code introduced in a PR, must have over 80% coverage** (patch coverage). This is enforced by our CI, and generally a PR will not be merged unless this coverage requirement is met. Please make sure to write thorough unit tests to increase GitProxy's code quality!
185185

186186
If test coverage is still insufficient after writing your tests, check out the [CodeCov report](https://app.codecov.io/gh/finos/git-proxy) after making the PR and take a look at which lines are missing coverage.
187+
188+
### E2E testing with Cypress
189+
190+
Although coverage is currently low, we have introduced Cypress testing to make sure that end-to-end flows are working as expected with every added feature.
191+
192+
This is a sample test from `cypress/e2e/repo.cy.js`:
193+
194+
```js
195+
describe('Repo', () => {
196+
beforeEach(() => {
197+
// Custom login command
198+
cy.login('admin', 'admin');
199+
200+
cy.visit('/dashboard/repo');
201+
202+
// prevent failures on 404 request and uncaught promises
203+
cy.on('uncaught:exception', () => false);
204+
});
205+
206+
describe('Code button for repo row', () => {
207+
it('Opens tooltip with correct content and can copy', () => {
208+
const cloneURL = 'http://localhost:8000/finos/git-proxy.git';
209+
const tooltipQuery = 'div[role="tooltip"]';
210+
211+
cy
212+
// tooltip isn't open to start with
213+
.get(tooltipQuery)
214+
.should('not.exist');
215+
216+
cy
217+
// find the entry for finos/git-proxy
218+
.get('a[href="/dashboard/repo/git-proxy"]')
219+
// take it's parent row
220+
.closest('tr')
221+
// find the nearby span containing Code we can click to open the tooltip
222+
.find('span')
223+
.contains('Code')
224+
.should('exist')
225+
.click();
226+
227+
cy
228+
// find the newly opened tooltip
229+
.get(tooltipQuery)
230+
.should('exist')
231+
.find('span')
232+
// check it contains the url we expect
233+
.contains(cloneURL)
234+
.should('exist')
235+
.parent()
236+
// find the adjacent span that contains the svg
237+
.find('span')
238+
.next()
239+
// check it has the copy icon first and click it
240+
.get('svg.octicon-copy')
241+
.should('exist')
242+
.click()
243+
// check the icon has changed to the check icon
244+
.get('svg.octicon-copy')
245+
.should('not.exist')
246+
.get('svg.octicon-check')
247+
.should('exist');
248+
249+
// failed to successfully check the clipboard
250+
});
251+
});
252+
});
253+
```
254+
255+
Here, we use a similar syntax to Mocha to **describe the behaviour that we expect**. The difference, is that Cypress expects us to write actual commands for executing actions in the app. Some commands used very often include `visit` (navigates to a certain page), `get` (gets a certain page element to check its properties), `contains` (checks if an element has a certain string value in it), `should` (similar to `expect` in unit tests).
256+
257+
#### Custom commands
258+
259+
Cypress allows defining **custom commands** to reuse and simplify code.
260+
261+
In the above example, `cy.login('admin', 'admin')` is actually a custom command defined in `/cypress/support/commands.js`. It allows logging a user into the app, which is a requirement for many E2E flows:
262+
263+
```js
264+
Cypress.Commands.add('login', (username, password) => {
265+
cy.session([username, password], () => {
266+
cy.visit('/login');
267+
cy.intercept('GET', '**/api/auth/me').as('getUser');
268+
269+
cy.get('[data-test=username]').type(username);
270+
cy.get('[data-test=password]').type(password);
271+
cy.get('[data-test=login]').click();
272+
273+
cy.wait('@getUser');
274+
cy.url().should('include', '/dashboard/repo');
275+
});
276+
});
277+
```

0 commit comments

Comments
 (0)