Skip to content

hmm, could not TS work example #385

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 1 commit into from
Jan 19, 2021
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ Full examples we use for testing in this repository:
- [examples/ts-example](examples/ts-example) uses Babel + Parcel to instrument and serve TypeScript file
- [examples/use-webpack](examples/use-webpack) shows Webpack build with source maps and Babel
- [examples/unit-tests-js](examples/unit-tests-js) runs just the unit tests and reports code coverage (JavaScript source code)
- [examples/unit-tests-ts](examples/unit-tests-ts) **NOT WORKING** runs just the unit tests and reports code coverage (TypeScript source code)

### External examples

Expand Down
6 changes: 6 additions & 0 deletions examples/unit-tests-ts/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": ["istanbul"],
"presets": [
"@babel/preset-typescript"
]
}
5 changes: 5 additions & 0 deletions examples/unit-tests-ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# example: unit-tests-ts

Examples that only run unit tests to test code written in TypeScript

**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).
3 changes: 3 additions & 0 deletions examples/unit-tests-ts/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"fixturesFolder": false
}
13 changes: 13 additions & 0 deletions examples/unit-tests-ts/cypress/integration/spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference types="cypress" />
import { isObject, add } from '../../src/utils/misc'

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

it('checks for object', () => {
expect(isObject({}), '{}').to.be.true
expect(isObject([]), '[]').to.be.true
})
})
41 changes: 41 additions & 0 deletions examples/unit-tests-ts/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// using browserify
const browserify = require('@cypress/browserify-preprocessor')

module.exports = (on, config) => {
require('../../../../task')(on, config)

const options = browserify.defaultOptions
// options.browserifyOptions.transform[1][1].babelrc = true
options.browserifyOptions.extensions.push('.ts')
// options.browserifyOptions.transform.push([
// require.resolve('browserify-istanbul'),
// {}
// ])
options.typescript = require.resolve('typescript')
// on('file:preprocessor', require('../../../../use-babelrc'))
console.log('browserify options')
console.log(JSON.stringify(options, null, 2))

on('file:preprocessor', browserify(options))
return config
}

// using webpack
/// <reference types="cypress" />
// const webpack = require('@cypress/webpack-preprocessor')

// /**
// * @type {Cypress.PluginConfig}
// */
// module.exports = (on, config) => {
// const options = {
// // use the same Webpack options to bundle spec files as your app does "normally"
// // which should instrument the spec files in this project
// webpackOptions: require('../../webpack.config'),
// watchOptions: {}
// }
// on('file:preprocessor', webpack(options))

// require('../../../../task')(on, config)
// return config
// }
1 change: 1 addition & 0 deletions examples/unit-tests-ts/cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../../../../support'
7 changes: 7 additions & 0 deletions examples/unit-tests-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "example-unit-tests-ts",
"description": "Run unit tests written using TypeScript",
"scripts": {
"cy:open": "../../node_modules/.bin/cypress open"
}
}
11 changes: 11 additions & 0 deletions examples/unit-tests-ts/src/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Very simple check, returns true for arrays as well
*/
export function isObject(object: any) {
return object != null && typeof object === 'object'
}

/**
* Adds two numbers together
*/
export const add = (a: number, b: number) => a + b
8 changes: 8 additions & 0 deletions examples/unit-tests-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node"
},
"files": ["./src/**/*.ts"]
}
Loading