Skip to content
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

fix: Resolve tsconfig.json for plugins file from the plugins directory #8377

Merged
merged 11 commits into from
Aug 24, 2020
2 changes: 1 addition & 1 deletion packages/server/lib/plugins/child/run_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ module.exports = (ipc, pluginsFile, projectRoot) => {
})

if (!tsRegistered) {
registerTsNode(projectRoot)
registerTsNode(projectRoot, pluginsFile)

// ensure typescript is only registered once
tsRegistered = true
Expand Down
2 changes: 1 addition & 1 deletion packages/server/lib/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Project extends EE {
return scaffold.plugins(path.dirname(cfg.pluginsFile), cfg)
}
}).then((cfg) => {
registerTsNode(this.projectRoot)
registerTsNode(this.projectRoot, cfg.pluginsFile)

return cfg
}).then((cfg) => {
Expand Down
10 changes: 7 additions & 3 deletions packages/server/lib/util/ts-node.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
const debug = require('debug')('cypress:server:ts-node')
const path = require('path')
const tsnode = require('ts-node')
const resolve = require('resolve')

const getTsNodeOptions = (tsPath) => {
const getTsNodeOptions = (tsPath, pluginsFile) => {
return {
compiler: tsPath, // use the user's installed typescript
compilerOptions: {
module: 'CommonJS',
},
// resolves tsconfig.json starting from the plugins directory
// instead of the cwd (the project root)
dir: path.dirname(pluginsFile),
kuceb marked this conversation as resolved.
Show resolved Hide resolved
transpileOnly: true, // transpile only (no type-check) for speed
}
}

const registerTsNode = (projectRoot) => {
const registerTsNode = (projectRoot, pluginsFile) => {
try {
const tsPath = resolve.sync('typescript', {
basedir: projectRoot,
})
const tsOptions = getTsNodeOptions(tsPath)
const tsOptions = getTsNodeOptions(tsPath, pluginsFile)

debug('typescript path: %s', tsPath)
debug('registering project TS with options %o', tsOptions)
Expand Down
6 changes: 6 additions & 0 deletions packages/server/test/e2e/1_typescript_plugins_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ describe('e2e typescript in plugins file', function () {
project: Fixtures.projectPath('ts-proj-esmoduleinterop-true'),
})
})

chrisbreiding marked this conversation as resolved.
Show resolved Hide resolved
it('loads tsconfig.json from plugins directory', function () {
return e2e.exec(this, {
project: Fixtures.projectPath('ts-proj-tsconfig-in-plugins'),
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"supportFile": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it('passes', () => {
expect(true).to.be.true
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// this tests that the tsconfig.json is loaded from the plugins directory.
// if it isn't, the lack of "downlevelIteration" support will cause this to
// fail at runtime with "RangeError: Invalid array length"
[...Array(100).keys()].map((x) => `${x}`)

export default () => {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"downlevelIteration": true
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="cypress" />

import fn from './commonjs-export-function'
import * as fn from './commonjs-export-function'

// if esModuleInterop is forced to be true, this will error // with 'fn is
// not a function'. instead, we allow the tsconfig.json to determine the value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
chrisbreiding marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 3 additions & 1 deletion packages/server/test/unit/project_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,15 @@ This option will not have an effect in Some-other-name. Tests that rely on web s
const { proj, register } = setupProject('default', tsProjPath)

return proj.open().then(() => {
expect(register).to.be.calledWith({
expect(register).to.be.calledWithMatch({
transpileOnly: true,
compiler: projTsPath,
compilerOptions: {
module: 'CommonJS',
},
})

expect(register.lastCall.args[0].dir).to.include('ts-installed/cypress/plugins')
})
})

Expand Down