-
Notifications
You must be signed in to change notification settings - Fork 3.3k
include CLI TS types in a way to avoid clashing with Jest #7352
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ab5bb6b
add job testing ts types against Jest
bahmutov 59be74f
tsc hello.ts
bahmutov 4bf7a4f
Remove type dependencies from cli. (#7194)
sainthkh 3e65d5d
Merge branch 'develop' into add-cy-and-jest-test-job
bahmutov 0a8037f
run test job outside cypress folder
bahmutov 62c0e87
refactor the job a little
bahmutov 4d51646
add enzyme too
bahmutov ac5acf8
factor out common types helper
bahmutov 6a3be65
put index.d.ts back the way it was before splitting
bahmutov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,70 @@ | ||
const fs = require('../lib/fs') | ||
const path = require('path') | ||
#!/usr/bin/env node | ||
|
||
// @ts-check | ||
/* eslint-disable no-console */ | ||
|
||
const { includeTypes } = require('./utils') | ||
const shell = require('shelljs') | ||
const { join } = require('path') | ||
const resolvePkg = require('resolve-pkg') | ||
|
||
shell.set('-v') // verbose | ||
shell.set('-e') // any error is fatal | ||
|
||
// We include the TypeScript definitions for the bundled 3rd party tools | ||
// thus we need to copy them from "dev" dependencies into our types folder | ||
// and we need to sometimes tweak these types files to use relative paths | ||
// This ensures that globals like Cypress.$, Cypress._ etc are property typed | ||
// yet we do not install "@types/.." packages with "npm install cypress" | ||
// because they can conflict with user's own libraries | ||
|
||
includeTypes.forEach((folder) => { | ||
const source = resolvePkg(`@types/${folder}`, { cwd: join(__dirname, '..', '..') }) | ||
|
||
shell.cp('-R', source, 'types') | ||
}) | ||
|
||
// jQuery v3.3.x includes "dist" folder that just references back to itself | ||
// causing dtslint to think there are double definitions. Remove that folder. | ||
const typesJqueryDistFolder = join('types', 'jquery', 'dist') | ||
|
||
shell.rm('-rf', typesJqueryDistFolder) | ||
|
||
/** | ||
* https://github.com/cypress-io/cypress/pull/5780 | ||
* Folder names in "node_modules/@types" that were copied to cli/types to generate index.d.ts. | ||
* They cause type errors in type checker. So, they should be removed. | ||
* Replaces "reference types=<name>" comment with "reference path=..." line. | ||
* @param {string} typeName - like "chai" or "jquery" | ||
* @param {string} relativeTypesFilePath - relative path to .d.ts file like "../chai/index.d.ts" | ||
* @param {string} filename - the source file to change | ||
*/ | ||
const includeTypes = [ | ||
'blob-util', | ||
'bluebird', | ||
'lodash', | ||
'mocha', | ||
'minimatch', | ||
'sinon', | ||
'sinon-chai', | ||
'chai', | ||
'chai-jquery', | ||
'jquery', | ||
] | ||
|
||
includeTypes.forEach((t) => { | ||
const dir = path.join(__dirname, '../types', t) | ||
|
||
if (fs.existsSync(dir)) { | ||
fs.removeSync(dir) | ||
} | ||
}) | ||
function makeReferenceTypesCommentRelative (typeName, relativeTypesFilePath, filename) { | ||
console.log('in file %s changing reference for types %s to relative path %s', | ||
filename, typeName, relativeTypesFilePath) | ||
|
||
const referenceTypes = `<reference types="${typeName}" />` | ||
const relativeTypes = `<reference path="${relativeTypesFilePath}" />` | ||
|
||
shell.sed( | ||
'-i', | ||
referenceTypes, | ||
relativeTypes, | ||
filename, | ||
) | ||
} | ||
|
||
// fix paths to Chai, jQuery and other types to be relative | ||
makeReferenceTypesCommentRelative('chai', '../chai/index.d.ts', | ||
join('types', 'chai-jquery', 'index.d.ts')) | ||
|
||
makeReferenceTypesCommentRelative('jquery', '../jquery/index.d.ts', | ||
join('types', 'chai-jquery', 'index.d.ts')) | ||
|
||
const sinonChaiFilename = join('types', 'sinon-chai', 'index.d.ts') | ||
|
||
makeReferenceTypesCommentRelative('chai', '../chai/index.d.ts', sinonChaiFilename) | ||
|
||
// also use relative import via path for sinon-chai | ||
// there is reference comment line we need to fix to be relative | ||
makeReferenceTypesCommentRelative('sinon', '../sinon/index.d.ts', sinonChaiFilename) | ||
|
||
// and an import sinon line to be changed to relative path | ||
shell.sed('-i', 'from \'sinon\';', 'from \'../sinon\';', sinonChaiFilename) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Folder names in "node_modules/@types" that we should include | ||
* when we bundle Cypress NPM package. These folder have ".d.ts" | ||
* definition files that we will need to include with our NPM package. | ||
*/ | ||
const includeTypes = [ | ||
'blob-util', | ||
'bluebird', | ||
'lodash', | ||
'mocha', | ||
'minimatch', | ||
'sinon', | ||
'sinon-chai', | ||
'chai', | ||
'chai-jquery', | ||
'jquery', | ||
] | ||
|
||
module.exports = { includeTypes } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Shim definition to export a namespace. Cypress is actually a global module | ||
// so import/export isn't allowed there. We import here and define a global module | ||
// so that Cypress can get and use the Blob type | ||
|
||
// tslint:disable-next-line:no-implicit-dependencies | ||
import * as blobUtil from './blob-util' | ||
|
||
export = BlobUtil | ||
export as namespace BlobUtil | ||
|
||
declare namespace BlobUtil { | ||
type BlobUtilStatic = typeof blobUtil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Shim definition to export a namespace. Cypress is actually a global module | ||
// so import/export isn't allowed there. We import here and define a global module | ||
// so that Cypress can get and use the Blob type | ||
import BluebirdStatic = require('./bluebird') | ||
|
||
export = Bluebird | ||
export as namespace Bluebird | ||
|
||
declare namespace Bluebird { | ||
type BluebirdStatic = typeof BluebirdStatic | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Shim definition to export a namespace. Cypress is actually a global module | ||
// so import/export isn't allowed there. We import here and define a global module | ||
/// <reference path="./chai/index.d.ts" /> | ||
declare namespace Chai { | ||
interface Include { | ||
html(html: string): Assertion | ||
text(text: string): Assertion | ||
value(text: string): Assertion | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// I was trying to avoid relying on "import" of actual module from "minimatch" | ||
// because it would not work in test project, and the only reliable way | ||
// to get around type errors finally was to copy the minimal minimatch function | ||
// definition from "minimatch/index.d.ts" here and just keep it in our code | ||
|
||
export = Minimatch | ||
export as namespace Minimatch | ||
|
||
interface MinimatchOptions { | ||
/** | ||
* Dump a ton of stuff to stderr. | ||
* | ||
* @default false | ||
*/ | ||
debug?: boolean | ||
|
||
/** | ||
* Do not expand {a,b} and {1..3} brace sets. | ||
* | ||
* @default false | ||
*/ | ||
nobrace?: boolean | ||
|
||
/** | ||
* Disable ** matching against multiple folder names. | ||
* | ||
* @default false | ||
*/ | ||
noglobstar?: boolean | ||
|
||
/** | ||
* Allow patterns to match filenames starting with a period, | ||
* even if the pattern does not explicitly have a period in that spot. | ||
* | ||
* @default false | ||
*/ | ||
dot?: boolean | ||
|
||
/** | ||
* Disable "extglob" style patterns like +(a|b). | ||
* | ||
* @default false | ||
*/ | ||
noext?: boolean | ||
|
||
/** | ||
* Perform a case-insensitive match. | ||
* | ||
* @default false | ||
*/ | ||
nocase?: boolean | ||
|
||
/** | ||
* When a match is not found by minimatch.match, | ||
* return a list containing the pattern itself if this option is set. | ||
* Otherwise, an empty list is returned if there are no matches. | ||
* | ||
* @default false | ||
*/ | ||
nonull?: boolean | ||
|
||
/** | ||
* If set, then patterns without slashes will be matched against | ||
* the basename of the path if it contains slashes. | ||
* | ||
* @default false | ||
*/ | ||
matchBase?: boolean | ||
|
||
/** | ||
* Suppress the behavior of treating # | ||
* at the start of a pattern as a comment. | ||
* | ||
* @default false | ||
*/ | ||
nocomment?: boolean | ||
|
||
/** | ||
* Suppress the behavior of treating a leading ! character as negation. | ||
* | ||
* @default false | ||
*/ | ||
nonegate?: boolean | ||
|
||
/** | ||
* Returns from negate expressions the same as if they were not negated. | ||
* (Ie, true on a hit, false on a miss.) | ||
* | ||
* @default false | ||
*/ | ||
flipNegate?: boolean | ||
} | ||
|
||
declare namespace Minimatch { | ||
function minimatch(target: string, pattern: string, options?: MinimatchOptions): boolean | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import moment = require('moment') | ||
export = Moment | ||
export as namespace Moment | ||
|
||
declare namespace Moment { | ||
type MomentStatic = typeof moment | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.