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: try to only add commonjs when running Cypress config #27484

Merged
merged 13 commits into from
Aug 20, 2023
8 changes: 8 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<!-- See the ../guides/writing-the-cypress-changelog.md for details on writing the changelog. -->
## 12.17.5

_Released 08/29/2023 (PENDING)_

**Bugfixes:**

- Only force CommonJS when running `ts-node` with a `TS_NODE_COMPILER` environment variable, such as when Cypress uses `ts-node` internally. This solves an issue where Cypress' internal `tsconfig` conflicts with properties set in the user's `tsconfig.json` such as `module` and `moduleResolution`. Fixes [#26308](https://github.com/cypress-io/cypress/issues/26308) and [#27448](https://github.com/cypress-io/cypress/issues/27448).

## 12.17.4

_Released 08/15/2023_
Expand Down
3 changes: 3 additions & 0 deletions packages/server/lib/plugins/child/ts_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const getTsNodeOptions = (tsPath, registeredFile) => {

if (process.env.TS_NODE_COMPILER) {
try {
// @ts-expect-error - compilerOptions is an object we can assign properties on.
// It's the 'tsconfig.compilerOptions'.
compilerOptions.moduleResolution = 'node'
compiler = require.resolve(process.env.TS_NODE_COMPILER, { paths: [path.dirname(registeredFile)] })
} catch {
// ts-node compiler not installed in project directory
Expand Down
24 changes: 24 additions & 0 deletions packages/server/test/unit/plugins/child/ts_node_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@ describe('lib/plugins/child/ts_node', () => {
})
})

it('registers ts-node with commonjs and node moduleResolution when process.env.TS_NODE_COMPILER is set', () => {
process.env.TS_NODE_COMPILER = true
sinon.stub(typescriptObject, 'version').value('1.1.1')
tsNodeUtil.register('proj-root', '/path/to/plugins/file.js')

expect(tsnode.register).to.be.calledWith({
transpileOnly: true,
compiler: 'typescript/lib/typescript.js',
dir: '/path/to/plugins',
compilerOptions: {
module: 'commonjs',
moduleResolution: 'node',
},
ignore: [
'(?:^|/)node_modules/',
'/packages/telemetry/dist/span-exporters/ipc-span-exporter',
'/packages/telemetry/dist/span-exporters/console-trace-link-exporter',
'/packages/telemetry/dist/processors/on-start-span-processor',
],
})

delete process.env.TS_NODE_COMPILER
})

it('does not register ts-node if typescript is not installed', () => {
resolve.typescript.returns(null)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { defineConfig } = require('cypress')

module.exports = defineConfig({
e2e: {
supportFile: false,
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"devDependencies": {
"typescript": "4.7.3"
},
"projectFixtureDirectory": "simple_passing"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "es2015",
"moduleResolution": "bundler",
"types": [
"cypress"
],
},
"include": [
"src"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


typescript@4.7.3:
version "4.7.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.3.tgz#8364b502d5257b540f9de4c40be84c98e23a129d"
integrity sha512-WOkT3XYvrpXx4vMMqlD+8R8R37fZkjyLGlxavMc4iB8lrl8L0DeTcHbYgw/v0N/z9wAFsgBhcsF0ruoySS22mA==
1 change: 1 addition & 0 deletions system-tests/test/config_modules_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('compiles config files using the native node import', () => {
'config-cjs-and-esm/config-with-js-tsconfig-es5',
'config-cjs-and-esm/config-with-js-tsconfig-es3',
'config-cjs-and-esm/config-with-js-tsconfig-es2015',
'config-cjs-and-esm/config-with-module-resolution-bundler',
].forEach((project) => {
systemTests.it(`${project}`, {
project,
Expand Down