Skip to content

Commit 6718097

Browse files
authored
hmm, could not TS work example (#385)
1 parent 4d3bf42 commit 6718097

File tree

12 files changed

+625
-0
lines changed

12 files changed

+625
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ Full examples we use for testing in this repository:
404404
- [examples/ts-example](examples/ts-example) uses Babel + Parcel to instrument and serve TypeScript file
405405
- [examples/use-webpack](examples/use-webpack) shows Webpack build with source maps and Babel
406406
- [examples/unit-tests-js](examples/unit-tests-js) runs just the unit tests and reports code coverage (JavaScript source code)
407+
- [examples/unit-tests-ts](examples/unit-tests-ts) **NOT WORKING** runs just the unit tests and reports code coverage (TypeScript source code)
407408

408409
### External examples
409410

examples/unit-tests-ts/.babelrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"plugins": ["istanbul"],
3+
"presets": [
4+
"@babel/preset-typescript"
5+
]
6+
}

examples/unit-tests-ts/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# example: unit-tests-ts
2+
3+
Examples that only run unit tests to test code written in TypeScript
4+
5+
**NOT WORKING** seems the TS code goes through `tsify` plugin, and does not pass via Istanbul plugin no matter what I try. See [issue #361](https://github.com/cypress-io/code-coverage/issues/361).

examples/unit-tests-ts/cypress.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"fixturesFolder": false
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference types="cypress" />
2+
import { isObject, add } from '../../src/utils/misc'
3+
4+
describe('unit tests', () => {
5+
it('adds two numbers', () => {
6+
expect(add(2, 3)).to.equal(5)
7+
})
8+
9+
it('checks for object', () => {
10+
expect(isObject({}), '{}').to.be.true
11+
expect(isObject([]), '[]').to.be.true
12+
})
13+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// using browserify
2+
const browserify = require('@cypress/browserify-preprocessor')
3+
4+
module.exports = (on, config) => {
5+
require('../../../../task')(on, config)
6+
7+
const options = browserify.defaultOptions
8+
// options.browserifyOptions.transform[1][1].babelrc = true
9+
options.browserifyOptions.extensions.push('.ts')
10+
// options.browserifyOptions.transform.push([
11+
// require.resolve('browserify-istanbul'),
12+
// {}
13+
// ])
14+
options.typescript = require.resolve('typescript')
15+
// on('file:preprocessor', require('../../../../use-babelrc'))
16+
console.log('browserify options')
17+
console.log(JSON.stringify(options, null, 2))
18+
19+
on('file:preprocessor', browserify(options))
20+
return config
21+
}
22+
23+
// using webpack
24+
/// <reference types="cypress" />
25+
// const webpack = require('@cypress/webpack-preprocessor')
26+
27+
// /**
28+
// * @type {Cypress.PluginConfig}
29+
// */
30+
// module.exports = (on, config) => {
31+
// const options = {
32+
// // use the same Webpack options to bundle spec files as your app does "normally"
33+
// // which should instrument the spec files in this project
34+
// webpackOptions: require('../../webpack.config'),
35+
// watchOptions: {}
36+
// }
37+
// on('file:preprocessor', webpack(options))
38+
39+
// require('../../../../task')(on, config)
40+
// return config
41+
// }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '../../../../support'

examples/unit-tests-ts/package.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "example-unit-tests-ts",
3+
"description": "Run unit tests written using TypeScript",
4+
"scripts": {
5+
"cy:open": "../../node_modules/.bin/cypress open"
6+
}
7+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Very simple check, returns true for arrays as well
3+
*/
4+
export function isObject(object: any) {
5+
return object != null && typeof object === 'object'
6+
}
7+
8+
/**
9+
* Adds two numbers together
10+
*/
11+
export const add = (a: number, b: number) => a + b

examples/unit-tests-ts/tsconfig.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"moduleResolution": "node"
6+
},
7+
"files": ["./src/**/*.ts"]
8+
}

0 commit comments

Comments
 (0)