Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

feat: mocking ES6 imports when using Babelrc #278

Merged
merged 7 commits into from
Jun 10, 2020
Merged

Conversation

bahmutov
Copy link
Contributor

@bahmutov bahmutov commented Jun 10, 2020

@bahmutov bahmutov marked this pull request as ready for review June 10, 2020 15:52
@bahmutov bahmutov changed the title Mocking ES6 imports when using Babelrc feat: mocking ES6 imports when using Babelrc Jun 10, 2020
@bahmutov bahmutov merged commit 400f85d into master Jun 10, 2020
@bahmutov bahmutov deleted the found-problem branch June 10, 2020 15:53
@bahmutov
Copy link
Contributor Author

🎉 This PR is included in version 4.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

dmtrKovalenko pushed a commit to cypress-io/cypress that referenced this pull request Oct 1, 2020
…t-unit-test#278)

During test runs, there is a Babel plugin that transforms ES6 imports into plain objects that can be stubbed using [cy.stub](https://on.cypress.io/stub). In essence

```js
// component imports named ES6 import from "calc.js
import { getRandomNumber } from './calc'
const Component = () => {
  // then calls it
  const n = getRandomNumber()
  return <div className="random">{n}</div>
}
```

The test can mock that import before mounting the component

```js
import Component from './Component.jsx'
import * as calc from './calc'
describe('Component', () => {
  it('mocks call from the component', () => {
    cy.stub(calc, 'getRandomNumber')
      .as('lucky')
      .returns(777)
    mount(<Component />)
  })
})
```
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Confirm Babel example collects code coverage Advise how to do named imports mocking using Babelrc
1 participant