Skip to content

Commit

Permalink
Set CommonJS module format when transpiling TS code (#7197)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed May 4, 2020
1 parent a904d46 commit aae90bb
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 31 deletions.
17 changes: 17 additions & 0 deletions packages/server/__snapshots__/1_typescript_support_spec.ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,20 @@ exports['e2e typescript project passes 1'] = `
`

exports['typescript with tsconfig run'] = `
(Run Finished)
Spec Tests Passing Failing Pending Skipped
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ ✔ app_spec.ts XX:XX 1 1 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ js-spec.js XX:XX 2 2 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ math.ts XX:XX - - - - - │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
✔ All specs passed! XX:XX 3 3 - - -
`
22 changes: 12 additions & 10 deletions packages/server/lib/plugins/child/run_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const preprocessor = require('./preprocessor')
const task = require('./task')
const util = require('../util')
const validateEvent = require('./validate_event')
const tsNodeOptions = require('../../util/ts-node-options')

const ARRAY_METHODS = ['concat', 'push', 'unshift', 'slice', 'pop', 'shift', 'slice', 'splice', 'filter', 'map', 'forEach', 'reduce', 'reverse', 'splice', 'includes']

Expand Down Expand Up @@ -160,6 +161,10 @@ let tsRegistered = false

module.exports = (ipc, pluginsFile, projectRoot) => {
debug('pluginsFile:', pluginsFile)
debug('project root:', projectRoot)
if (!projectRoot) {
throw new Error('Unexpected: projectRoot should be a string')
}

process.on('uncaughtException', (err) => {
debug('uncaught exception:', util.serializeError(err))
Expand All @@ -180,21 +185,18 @@ module.exports = (ipc, pluginsFile, projectRoot) => {
if (!tsRegistered) {
try {
const tsPath = resolve.sync('typescript', {
basedir: this.projectRoot,
basedir: projectRoot,
})

debug('typescript path: %s', tsPath)
const tsOptions = tsNodeOptions.getTsNodeOptions(tsPath)

tsnode.register({
compiler: tsPath,
transpileOnly: true,
compilerOptions: {
debug('typescript path: %s', tsPath)
debug('registering plugins TS with options %o', tsOptions)

esModuleInterop: true,
},
})
tsnode.register(tsOptions)
} catch (e) {
debug(`typescript doesn't exist. ts-node setup passed.`)
debug(`typescript doesn't exist. ts-node setup failed.`)
debug('error message: %s', e.message)
}

// ensure typescript is only registered once
Expand Down
4 changes: 4 additions & 0 deletions packages/server/lib/plugins/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ module.exports = {
basedir: config.projectRoot,
}

if (!config.projectRoot) {
throw new Error('Config is missing projet root')

This comment has been minimized.

Copy link
@Saibamen

Saibamen May 4, 2020

Contributor

project

}

debug('resolving typescript with options %o', options)

const resolved = resolve.sync('typescript', options)
Expand Down
12 changes: 7 additions & 5 deletions packages/server/lib/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const keys = require('./util/keys')
const settings = require('./util/settings')
const specsUtil = require('./util/specs')
const { escapeFilenameInUrl } = require('./util/escape_filename')
const tsNodeOptions = require('./util/ts-node-options')

const localCwd = cwd()

Expand Down Expand Up @@ -107,14 +108,15 @@ class Project extends EE {
basedir: this.projectRoot,
})

const tsOptions = tsNodeOptions.getTsNodeOptions(tsPath)

debug('typescript path: %s', tsPath)
debug('registering project TS with options %o', tsOptions)

tsnode.register({
compiler: tsPath,
transpileOnly: true,
})
tsnode.register(tsOptions)
} catch (e) {
debug(`typescript doesn't exist. ts-node setup passed.`)
debug(`typescript doesn't exist. ts-node setup failed.`)
debug('error message %s', e.message)
}

return cfg
Expand Down
29 changes: 29 additions & 0 deletions packages/server/lib/util/ts-node-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// returns options for ts-node registration
// https://github.com/TypeStrong/ts-node
const _ = require('lodash')

/**
* Default ts - node options.We want to output CommonJS modules.
* And we want to run fast - thus transpile only mode (no type checking)
*/
const tsOptions = {
transpileOnly: true,
compilerOptions: {
module: 'CommonJS',
esModuleInterop: true,
},
}

/**
* Returns combined object with ts-node options.
* @param {string} tsPath Path to TypeScript
*/
function getTsNodeOptions (tsPath) {
const merged = _.cloneDeep(tsOptions)

merged.compiler = tsPath

return merged
}

module.exports = { getTsNodeOptions }
17 changes: 17 additions & 0 deletions packages/server/test/e2e/1_typescript_support_spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const e2e = require('../support/helpers/e2e')
const Fixtures = require('../support/helpers/fixtures')

const snapshot = require('snap-shot-it')

describe('e2e typescript', function () {
e2e.setup({ npmInstall: true })

Expand All @@ -27,4 +29,19 @@ describe('e2e typescript', function () {
snapshot: true,
})
})

it('handles tsconfig with module other than commonjs', function () {
const projPath = Fixtures.projectPath('ts-proj-with-own-tsconfig')

return e2e.exec(this, {
project: projPath,
config: {
video: false,
},
}).then((result) => {
const runSummary = e2e.leaveRunFinishedTable(e2e.normalizeStdout(result.stdout))

snapshot('typescript with tsconfig run', runSummary)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { add } from './math'

it('is true', () => {
expect(add(1, 2)).to.eq(3)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { add } from './math'

describe('JS spec', () => {
it('adds 2 and 2 together', () => {
expect(add(2, 2)).to.equal(4)
})

it('calls task', () => {
cy.task('hello', 'TS').should('equal', 'Hello, TS!')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const add = (a: number, b: number) => {
return a + b
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const asyncGreeting = async (greeting) => {
return Promise.resolve(`Hello, ${greeting}!`)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference types="cypress" />
import { asyncGreeting } from './greeting'

export default (on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions) => {
on('task', {
hello: asyncGreeting,
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// <reference types="cypress" />

// Copied an example command from https://on.cypress.io/custom-commands
Cypress.Commands.add('clickLink', (label: string | number | RegExp) => {
cy.get('a').contains(label).click()
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"module": "esnext",
"importHelpers": true
}
}
4 changes: 4 additions & 0 deletions packages/server/test/unit/project_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ This option will not have an effect in Some-other-name. Tests that rely on web s
expect(register).to.be.calledWith({
transpileOnly: true,
compiler: projTsPath,
compilerOptions: {
module: 'CommonJS',
esModuleInterop: true,
},
})
})
})
Expand Down
38 changes: 22 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5483,9 +5483,9 @@ aws-sdk@2.447.0:
xml2js "0.4.19"

aws-sdk@^2.389.0:
version "2.666.0"
resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.666.0.tgz#4cd4f6bea920e3ef3192bf113aa9c606a307a709"
integrity sha512-m4m4eHs/F7SRW0OnvxRWyrAyqcQE7kyVnfwyrhA7P0w92FOmmu+tw6JKI5LZNVBsaj2VBAfPn72V6nWzP3IIlw==
version "2.667.0"
resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.667.0.tgz#9d45b5fe90038bfba88a892112251d87af19b2f5"
integrity sha512-CDeMyqblfGlj/xUSp1/G1b9Tb4LDTid5CYrBW9FyBqMg6JIiXkaEN3b4oK2VjS18Xv1eth5LJAbYNMxjfHfKjw==
dependencies:
buffer "4.9.1"
events "1.1.1"
Expand Down Expand Up @@ -10227,9 +10227,9 @@ electron-publish@22.3.6:
mime "^2.4.4"

electron-to-chromium@^1.3.413:
version "1.3.425"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.425.tgz#96b7b5aa9115e42baf59be88d2432c9f406128c4"
integrity sha512-JTEOWiqCY4snuKuQAaFy0z6LK2Gdb8Lojkd/csQwpNHgMUF8I6QRjGVKk44IH46dHQhUFKzr4o6zxZrtDBjc2Q==
version "1.3.426"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.426.tgz#d7301de9e40df83a40fe1f51b4463cfe170d1153"
integrity sha512-sdQ7CXQbFflKY5CU63ra+kIYq9F7d1OqI33856qJZxTrwo0sLASdmoRl9lWpGrQDS9Nk/RFliQWd3PPDrZ+Meg==

electron@8.2.3:
version "8.2.3"
Expand Down Expand Up @@ -13252,12 +13252,13 @@ has@^1.0.0, has@^1.0.3:
function-bind "^1.1.1"

hash-base@^3.0.0:
version "3.0.4"
resolved "https://registry.yarnpkg.com/hash-base/-/hash-base-3.0.4.tgz#5fc8686847ecd73499403319a6b0a3f3f6ae4918"
integrity sha1-X8hoaEfs1zSZQDMZprCj8/auSRg=
version "3.1.0"
resolved "https://registry.yarnpkg.com/hash-base/-/hash-base-3.1.0.tgz#55c381d9e06e1d2997a883b4a3fddfe7f0d3af33"
integrity sha512-1nmYp/rhMDiE7AYkDw+lLwlAzz0AntGIe51F3RfFfEqyQ3feY2eI/NcwC6umIQVOASPMsWJLJScWKSSvzL9IVA==
dependencies:
inherits "^2.0.1"
safe-buffer "^5.0.1"
inherits "^2.0.4"
readable-stream "^3.6.0"
safe-buffer "^5.2.0"

hash.js@^1.0.0, hash.js@^1.0.3:
version "1.1.7"
Expand Down Expand Up @@ -17531,11 +17532,16 @@ module-not-found-error@^1.0.0:
resolved "https://registry.yarnpkg.com/module-not-found-error/-/module-not-found-error-1.0.1.tgz#cf8b4ff4f29640674d6cdd02b0e3bc523c2bbdc0"
integrity sha1-z4tP9PKWQGdNbN0CsOO8UjwrvcA=

moment@2.24.0, moment@^2.18.1, moment@^2.19.1, moment@^2.9.0:
moment@2.24.0:
version "2.24.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b"
integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==

moment@^2.18.1, moment@^2.19.1, moment@^2.9.0:
version "2.25.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.25.0.tgz#e961ab9a5848a1cf2c52b1af4e6c82a8401e7fe9"
integrity sha512-vbrf6kJGpevOxmDRvCCvGuCSXvRj93264WcFzjm3Z3pV4lfjrXll8rvSP+EbmCte64udj1LkJMILxQnjXAQBzg==

moo@^0.5.0:
version "0.5.1"
resolved "https://registry.yarnpkg.com/moo/-/moo-0.5.1.tgz#7aae7f384b9b09f620b6abf6f74ebbcd1b65dbc4"
Expand Down Expand Up @@ -20541,7 +20547,7 @@ read@1, read@~1.0.1:
string_decoder "~1.1.1"
util-deprecate "~1.0.1"

"readable-stream@2 || 3", readable-stream@^3.0.2, readable-stream@^3.0.6, readable-stream@^3.1.1, readable-stream@^3.4.0:
"readable-stream@2 || 3", readable-stream@^3.0.2, readable-stream@^3.0.6, readable-stream@^3.1.1, readable-stream@^3.4.0, readable-stream@^3.6.0:
version "3.6.0"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.6.0.tgz#337bbda3adc0706bd3e024426a286d4b4b2c9198"
integrity sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==
Expand Down Expand Up @@ -23992,9 +23998,9 @@ typescript@^3.0.3, typescript@^3.6.4:
integrity sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==

typescript@next:
version "4.0.0-dev.20200430"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.0-dev.20200430.tgz#4316bdb47f9c7339f3c6155a7430b461bc62b595"
integrity sha512-cF8I0/7zTJqcgh2Xo8D85qKXHJb5Sy74Y6emU6tKvydqhey0BhEwd4BgnOqW9vP8+ZMGioRYfED1lIzZN4g9dg==
version "4.0.0-dev.20200501"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.0-dev.20200501.tgz#9a54727bd719102d947a5ecf584ccc985dccb94b"
integrity sha512-RQvw3/R3//3hNxzeTJ4sbiuX9qjrpoIU6VxD+QquHHaJXf67P3yfIcV9hNFqd2QvrUiFPGM3KN08ahWkEN56bA==

ua-parser-js@^0.7.18:
version "0.7.21"
Expand Down

4 comments on commit aae90bb

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on aae90bb May 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.5.1/linux-x64/circle-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-313621/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.5.1/circle-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-313633/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on aae90bb May 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

Instructions are included below, depending on the shell you are using.

In Command Prompt (cmd.exe):

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.5.1/win32-x64/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.5.1/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.tgz

In PowerShell:

$env:CYPRESS_INSTALL_BINARY = https://cdn.cypress.io/beta/binary/4.5.1/win32-x64/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.5.1/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.tgz

In Git Bash:

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.5.1/win32-x64/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.5.1/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.tgz

Using cross-env:

If the above commands do not work for you, you can also try using cross-env:

npm i -g cross-env
cross-env CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.5.1/win32-x64/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.zip npm install https://cdn.cypress.io/beta/npm/4.5.1/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on aae90bb May 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

Instructions are included below, depending on the shell you are using.

In Command Prompt (cmd.exe):

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.5.1/win32-ia32/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.5.1/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.tgz

In PowerShell:

$env:CYPRESS_INSTALL_BINARY = https://cdn.cypress.io/beta/binary/4.5.1/win32-ia32/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.5.1/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.tgz

In Git Bash:

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.5.1/win32-ia32/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.5.1/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.tgz

Using cross-env:

If the above commands do not work for you, you can also try using cross-env:

npm i -g cross-env
cross-env CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.5.1/win32-ia32/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.zip npm install https://cdn.cypress.io/beta/npm/4.5.1/appveyor-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-32633932/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on aae90bb May 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.5.1/darwin-x64/circle-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-313641/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.5.1/circle-develop-aae90bbbe06fc2d4de4b14f5b724b254731f9065-313622/cypress.tgz

Please sign in to comment.