Skip to content

Commit a665691

Browse files
committed
add typescript example
1 parent adc7a0d commit a665691

File tree

13 files changed

+513
-5
lines changed

13 files changed

+513
-5
lines changed

.circleci/config.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ jobs:
2929
# we don't need Cypress to do the release
3030
CYPRESS_INSTALL_BINARY: '0'
3131
# trick semantic-release into thinking this is NOT a pull request
32-
# (under the hood env-ci is used)
33-
CIRCLE_PR_NUMBER:
34-
CIRCLE_PULL_REQUEST:
35-
CI_PULL_REQUEST:
32+
# (under the hood the module env-ci is used to check if this is a PR)
33+
CIRCLE_PR_NUMBER: ''
34+
CIRCLE_PULL_REQUEST: ''
35+
CI_PULL_REQUEST: ''
3636
steps:
3737
- checkout
3838
- node/with-cache:

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ npm run dev:no:coverage
337337

338338
- [examples/before-each-visit](examples/before-each-visit) checks if code coverage correctly keeps track of code when doing `cy.visit` before each test
339339
- [examples/before-all-visit](examples/before-all-visit) checks if code coverage works when `cy.visit` is made once in the `before` hook
340+
- [examples/ts-example](examples/ts-example) uses Babel + Parcel to instrument and serve TypeScript file
340341

341342
### External examples
342343

examples/ts-example/.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["istanbul"]
3+
}

examples/ts-example/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# example: ts-example
2+
3+
Code coverage for TypeScript code. See [nyc TS support](https://github.com/istanbuljs/nyc#typescript-projects) docs too.
4+
5+
Code is instrumented on the fly using [.babelrc](.babelrc) and Parcel to run it.
6+
7+
## Use
8+
9+
- start the server and Cypress with `npm run dev`
10+
- run the Cypress tests
11+
- look at the generated reports in folder `coverage`

examples/ts-example/cypress.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"baseUrl": "http://localhost:1234",
3+
"fixturesFolder": false
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference types="cypress" />
2+
describe('ts-example', () => {
3+
beforeEach(() => {
4+
cy.visit('/')
5+
})
6+
7+
it('calls add', () => {
8+
cy.window()
9+
.invoke('add', 2, 3)
10+
.should('equal', 5)
11+
})
12+
13+
it('calls sub', () => {
14+
cy.window()
15+
.invoke('sub', 2, 3)
16+
.should('equal', -1)
17+
})
18+
19+
it('calls abs twice', () => {
20+
cy.window()
21+
.invoke('abs', 2)
22+
.should('equal', 2)
23+
24+
cy.window()
25+
.invoke('abs', -5)
26+
.should('equal', 5)
27+
})
28+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = (on, config) => {
2+
on('task', require('../../../../task'))
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '../../../../support'

examples/ts-example/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<body>
2+
Page body
3+
<script src="main.ts"></script>
4+
</body>

examples/ts-example/main.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const add = (a: number, b: number) => {
2+
return a + b
3+
}
4+
5+
const sub = (a: number, b: number) => {
6+
return a - b
7+
}
8+
9+
function abs(x: number) {
10+
if (x >= 0) {
11+
return x
12+
} else {
13+
return -x
14+
}
15+
}
16+
17+
// @ts-ignore
18+
window.add = add
19+
// @ts-ignore
20+
window.sub = sub
21+
// @ts-ignore
22+
window.abs = abs

examples/ts-example/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "example-before-each-visit",
3+
"description": "Getting code coverage when cy.visit is used in beforeEach hook",
4+
"devDependencies": {},
5+
"nyc": {
6+
"extends": "@istanbuljs/nyc-config-typescript",
7+
"all": true
8+
},
9+
"scripts": {
10+
"start": "../../node_modules/.bin/parcel serve index.html",
11+
"build": "../../node_modules/.bin/parcel build index.html",
12+
"serve": "../../node_modules/.bin/serve dist",
13+
"cy:open": "../../node_modules/.bin/cypress open",
14+
"coverage": "../../node_modules/.bin/nyc report --report-dir ./coverage --temp-dir .nyc_output --reporter=lcov",
15+
"dev": "../../node_modules/.bin/start-test 1234 cy:open",
16+
"dev:dist": "CYPRESS_baseUrl=http://localhost:5000 ../../node_modules/.bin/start-test serve 5000 cy:open"
17+
}
18+
}

0 commit comments

Comments
 (0)