spies_stubs_clocks.spec.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /// <reference types="cypress" />
  2. // remove no check once Cypress.sinon is typed
  3. // https://github.com/cypress-io/cypress/issues/6720
  4. context('Spies, Stubs, and Clock', () => {
  5. it('cy.spy() - wrap a method in a spy', () => {
  6. // https://on.cypress.io/spy
  7. cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')
  8. const obj = {
  9. foo () {},
  10. }
  11. const spy = cy.spy(obj, 'foo').as('anyArgs')
  12. obj.foo()
  13. expect(spy).to.be.called
  14. })
  15. it('cy.spy() retries until assertions pass', () => {
  16. cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')
  17. const obj = {
  18. /**
  19. * Prints the argument passed
  20. * @param x {any}
  21. */
  22. foo (x) {
  23. console.log('obj.foo called with', x)
  24. },
  25. }
  26. cy.spy(obj, 'foo').as('foo')
  27. setTimeout(() => {
  28. obj.foo('first')
  29. }, 500)
  30. setTimeout(() => {
  31. obj.foo('second')
  32. }, 2500)
  33. cy.get('@foo').should('have.been.calledTwice')
  34. })
  35. it('cy.stub() - create a stub and/or replace a function with stub', () => {
  36. // https://on.cypress.io/stub
  37. cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')
  38. const obj = {
  39. /**
  40. * prints both arguments to the console
  41. * @param a {string}
  42. * @param b {string}
  43. */
  44. foo (a, b) {
  45. console.log('a', a, 'b', b)
  46. },
  47. }
  48. const stub = cy.stub(obj, 'foo').as('foo')
  49. obj.foo('foo', 'bar')
  50. expect(stub).to.be.called
  51. })
  52. it('cy.clock() - control time in the browser', () => {
  53. // https://on.cypress.io/clock
  54. // create the date in UTC so its always the same
  55. // no matter what local timezone the browser is running in
  56. const now = new Date(Date.UTC(2017, 2, 14)).getTime()
  57. cy.clock(now)
  58. cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')
  59. cy.get('#clock-div').click()
  60. .should('have.text', '1489449600')
  61. })
  62. it('cy.tick() - move time in the browser', () => {
  63. // https://on.cypress.io/tick
  64. // create the date in UTC so its always the same
  65. // no matter what local timezone the browser is running in
  66. const now = new Date(Date.UTC(2017, 2, 14)).getTime()
  67. cy.clock(now)
  68. cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')
  69. cy.get('#tick-div').click()
  70. .should('have.text', '1489449600')
  71. cy.tick(10000) // 10 seconds passed
  72. cy.get('#tick-div').click()
  73. .should('have.text', '1489449610')
  74. })
  75. it('cy.stub() matches depending on arguments', () => {
  76. // see all possible matchers at
  77. // https://sinonjs.org/releases/latest/matchers/
  78. const greeter = {
  79. /**
  80. * Greets a person
  81. * @param {string} name
  82. */
  83. greet (name) {
  84. return `Hello, ${name}!`
  85. },
  86. }
  87. cy.stub(greeter, 'greet')
  88. .callThrough() // if you want non-matched calls to call the real method
  89. .withArgs(Cypress.sinon.match.string).returns('Hi')
  90. .withArgs(Cypress.sinon.match.number).throws(new Error('Invalid name'))
  91. expect(greeter.greet('World')).to.equal('Hi')
  92. // @ts-ignore
  93. expect(() => greeter.greet(42)).to.throw('Invalid name')
  94. expect(greeter.greet).to.have.been.calledTwice
  95. // non-matched calls goes the actual method
  96. // @ts-ignore
  97. expect(greeter.greet()).to.equal('Hello, undefined!')
  98. })
  99. it('matches call arguments using Sinon matchers', () => {
  100. // see all possible matchers at
  101. // https://sinonjs.org/releases/latest/matchers/
  102. const calculator = {
  103. /**
  104. * returns the sum of two arguments
  105. * @param a {number}
  106. * @param b {number}
  107. */
  108. add (a, b) {
  109. return a + b
  110. },
  111. }
  112. const spy = cy.spy(calculator, 'add').as('add')
  113. expect(calculator.add(2, 3)).to.equal(5)
  114. // if we want to assert the exact values used during the call
  115. expect(spy).to.be.calledWith(2, 3)
  116. // let's confirm "add" method was called with two numbers
  117. expect(spy).to.be.calledWith(Cypress.sinon.match.number, Cypress.sinon.match.number)
  118. // alternatively, provide the value to match
  119. expect(spy).to.be.calledWith(Cypress.sinon.match(2), Cypress.sinon.match(3))
  120. // match any value
  121. expect(spy).to.be.calledWith(Cypress.sinon.match.any, 3)
  122. // match any value from a list
  123. expect(spy).to.be.calledWith(Cypress.sinon.match.in([1, 2, 3]), 3)
  124. /**
  125. * Returns true if the given number is event
  126. * @param {number} x
  127. */
  128. const isEven = (x) => x % 2 === 0
  129. // expect the value to pass a custom predicate function
  130. // the second argument to "sinon.match(predicate, message)" is
  131. // shown if the predicate does not pass and assertion fails
  132. expect(spy).to.be.calledWith(Cypress.sinon.match(isEven, 'isEven'), 3)
  133. /**
  134. * Returns a function that checks if a given number is larger than the limit
  135. * @param {number} limit
  136. * @returns {(x: number) => boolean}
  137. */
  138. const isGreaterThan = (limit) => (x) => x > limit
  139. /**
  140. * Returns a function that checks if a given number is less than the limit
  141. * @param {number} limit
  142. * @returns {(x: number) => boolean}
  143. */
  144. const isLessThan = (limit) => (x) => x < limit
  145. // you can combine several matchers using "and", "or"
  146. expect(spy).to.be.calledWith(
  147. Cypress.sinon.match.number,
  148. Cypress.sinon.match(isGreaterThan(2), '> 2').and(Cypress.sinon.match(isLessThan(4), '< 4')),
  149. )
  150. expect(spy).to.be.calledWith(
  151. Cypress.sinon.match.number,
  152. Cypress.sinon.match(isGreaterThan(200), '> 200').or(Cypress.sinon.match(3)),
  153. )
  154. // matchers can be used from BDD assertions
  155. cy.get('@add').should('have.been.calledWith',
  156. Cypress.sinon.match.number, Cypress.sinon.match(3))
  157. // you can alias matchers for shorter test code
  158. const { match: M } = Cypress.sinon
  159. cy.get('@add').should('have.been.calledWith', M.number, M(3))
  160. })
  161. })