Skip to content

feat: add babelrc helper script #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ workflows:
# to see live static HTML site
- store_artifacts:
path: coverage
# print code coverage summary to the terminal
- run: npx nyc report
# publish new version if necessary
- run: npm run semantic-release

30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,41 @@ If your application is loaded Istanbul-instrumented source code, then the covera

![Coverage report](images/coverage.jpg)

## Instrument unit tests

If you test your application code directly from `specs` you might want to instrument them and combine unit test code coverage with any end-to-end code coverage (from iframe). You can easily instrument spec files using [babel-plugin-istanbul](https://github.com/istanbuljs/babel-plugin-istanbul) for example. Put the following in `cypress/plugins/index.js` file to use `.babelrc` file

```js
const browserify = require('@cypress/browserify-preprocessor')
That should be it!

module.exports = (on, config) => {
on('task', require('cypress-istanbul/task'))
## Instrument unit tests

// tell Cypress to use .babelrc when bundling spec code
const options = browserify.defaultOptions
options.browserifyOptions.transform[1][1].babelrc = true
on('file:preprocessor', browserify(options))
}
```
If you test your application code directly from `specs` you might want to instrument them and combine unit test code coverage with any end-to-end code coverage (from iframe). You can easily instrument spec files using [babel-plugin-istanbul](https://github.com/istanbuljs/babel-plugin-istanbul) for example.

Install the plugin

```
npm i -D babel-plugin-istanbul
```

and set in your `.babelrc` file
Set your `.babelrc` file

```rc
{
"plugins": ["istanbul"]
}
```

Put the following in `cypress/plugins/index.js` file to use `.babelrc` file

```js
module.exports = (on, config) => {
on('task', require('cypress-istanbul/task'))
on('file:preprocessor', require('cypress-istanbul/use-babelrc'))
}
```

Now the code coverage from spec files will be combined with end-to-end coverage.

## Examples

- [Demo battery app](https://github.com/bahmutov/demo-battery-api/tree/bundle) branch "bundle"
- Read ["Code Coverage by Parcel Bundler"](https://glebbahmutov.com/blog/code-coverage-by-parcel/) blog post
- Read ["Combined End-to-end and Unit Test Coverage"](https://glebbahmutov.com/blog/combined-end-to-end-and-unit-test-coverage/)

## License

Expand Down
12 changes: 12 additions & 0 deletions cypress/integration/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// https://on.cypress.io/intelligent-code-completion
/// <reference types="Cypress" />

import {add} from '../unit'

context('Page test', () => {
beforeEach(() => {
cy.visit('/', {
Expand All @@ -17,3 +19,13 @@ context('Page test', () => {
.should('have.been.calledWith', 'just names', ['joe', 'mary'])
})
})

context('Unit tests', () => {
it('adds numbers', () => {
expect(add(2, 3)).to.equal(5)
})

it('concatenates strings', () => {
expect(add('foo', 'Bar')).to.equal('fooBar')
})
})
5 changes: 5 additions & 0 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module.exports = (on, config) => {
on('task', require('../../task'))

// also use .babelrc file when bundling spec files
// to get the code coverage from unit tests
// https://glebbahmutov.com/blog/combined-end-to-end-and-unit-test-coverage/
on('file:preprocessor', require('../../use-babelrc'))
}
1 change: 1 addition & 0 deletions cypress/unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const add = (a, b) => a + b
Loading