Skip to content

Commit 3e98904

Browse files
authored
Update cypress.md
1 parent 85cc8ff commit 3e98904

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

docs/testing/cypress.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,34 @@ it('should only call function once', () => {
301301
});
302302
```
303303

304+
## Tip: Mocking in unit testing
305+
If you are unit testing modules in your application you can provide mocks using `cy.stub` e.g. if you want to ensure that `navigate` is called in a function `foo`:
306+
307+
* `foo.ts`
308+
```ts
309+
import { navigate } from 'takeme';
310+
311+
export function foo() {
312+
navigate('/foo');
313+
}
314+
```
315+
316+
* You can do this as in `some.spec.ts`:
317+
```ts
318+
/// <reference types="cypress"/>
319+
320+
import { foo } from '../../../src/app/foo';
321+
import * as takeme from 'takeme';
322+
323+
describe('should work', () => {
324+
it('should stub it', () => {
325+
cy.stub(takeme);
326+
foo();
327+
expect(takeme.navigate).to.have.been.calledWith('/foo');
328+
})
329+
});
330+
```
331+
304332
## Tip: Breakpoint
305333
The automatic snapshots + command log generated by the cypress test are great for debugging. That said you can pause test execution if you want.
306334

@@ -310,7 +338,6 @@ First make sure you have chrome developer tools (lovingly called dev tools) open
310338
* Test code breakpoints: You can use the `.debug()` command and cypress test execution will stop at it. Alternatively you can use a `debugger` statement in a `.then` command callback to cause a pause. e.g `.then(() => { debugger })`. You can even use it to grab some element `cy.get('#foo').then(($ /* a reference to the dom element */) => { debugger; })` or a network call e.g. `cy.request('https://someurl').then((res /* network response */) => { debugger });`. However idiomatic way is `cy.get('#foo').debug()` and then when the test runner is paused on `debug` you can click on the `get` in the command log to automatically `console.log` any information you might need about the `.get('#foo')` command (and similarly for any other commands you want to debug).
311339

312340

313-
314341
## Resources
315342
* Website: https://www.cypress.io/
316343
* Write your first cypress test (gives a nice tour of the cypress IDE) : https://docs.cypress.io/guides/getting-started/writing-your-first-test.html

0 commit comments

Comments
 (0)