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: add yarn v2 pnp support to default webpack processor #17335

Merged
merged 10 commits into from
Jul 21, 2021
11 changes: 11 additions & 0 deletions cli/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,20 @@ const util = {
.mapValues((value) => { // stringify to 1 or 0
return value ? '1' : '0'
})
.extend(util.getOriginalNodeOptions(options))
.value()
},

getOriginalNodeOptions (options) {
if (process.env.NODE_OPTIONS) {
return {
ORIGINAL_NODE_OPTIONS: process.env.NODE_OPTIONS,
}
}

return {}
},

getForceTty () {
return {
FORCE_STDIN_TTY: util.isTty(process.stdin.fd),
Expand Down
22 changes: 22 additions & 0 deletions cli/test/lib/util_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require('../spec_helper')
const os = require('os')
const tty = require('tty')
const snapshot = require('../support/snapshot')
const mockedEnv = require('mocked-env')
const supportsColor = require('supports-color')
const proxyquire = require('proxyquire')
const hasha = require('hasha')
Expand Down Expand Up @@ -254,6 +255,27 @@ describe('util', () => {
})
})

context('.getOriginalNodeOptions', () => {
let restoreEnv

afterEach(() => {
if (restoreEnv) {
restoreEnv()
restoreEnv = null
}
})

it('copy NODE_OPTIONS to ORIGINAL_NODE_OPTIONS', () => {
restoreEnv = mockedEnv({
NODE_OPTIONS: '--require foo.js',
})

expect(util.getOriginalNodeOptions({})).to.deep.eq({
ORIGINAL_NODE_OPTIONS: '--require foo.js',
})
})
})

context('.exit', () => {
it('calls process.exit', () => {
process.exit.withArgs(2).withArgs(0)
Expand Down
4 changes: 4 additions & 0 deletions packages/server/lib/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ const init = (config, options) => {
const childArguments = ['--file', pluginsFile, '--projectRoot', options.projectRoot]
const childOptions = {
stdio: 'pipe',
env: {
...process.env,
NODE_OPTIONS: process.env.ORIGINAL_NODE_OPTIONS || '',
},
}

if (config.resolvedNodePath) {
Expand Down
9 changes: 2 additions & 7 deletions packages/server/lib/util/resolve.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const resolve = require('resolve')
const env = require('./env')
const debug = require('debug')('cypress:server:plugins')

Expand All @@ -15,13 +14,9 @@ module.exports = {
}

try {
const options = {
basedir: projectRoot,
}
debug('resolving typescript with projectRoot %o', projectRoot)

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

const resolved = resolve.sync('typescript', options)
const resolved = require.resolve('typescript', { paths: [projectRoot] })

debug('resolved typescript %s', resolved)

Expand Down
59 changes: 59 additions & 0 deletions packages/server/test/e2e/4_yarn_v2_pnp_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import fs from 'fs-extra'
import os from 'os'
import path from 'path'
import cp from 'child_process'
import util from 'util'
import e2e from '../support/helpers/e2e'
import Fixtures from '../support/helpers/fixtures'

const exec = async (cmd, ...args) => {
console.log(`Running "${cmd}"...`)
const ret = await util.promisify(cp.exec)(cmd, ...args)
.catch((err) => {
console.error('Error:', err)

return err
})

console.log('stdout:', ret.stdout)
ret.stderr && console.log('stderr:', ret.stderr)

return ret
}

const fixtureDir = Fixtures.path('projects/yarn-v2-pnp')
const cypressCli = path.join(__dirname, '../../../../cli/bin/cypress')

describe('e2e yarn v2', () => {
let projectDir

beforeEach(async function () {
this.timeout(240000)

// copy yarn-v2 to tmpdir so node_modules resolution won't fall back to project root
projectDir = path.join(os.tmpdir(), `cy-yarn-v2-pnp-${Date.now()}`)
console.log(`projectDir`, projectDir)

await fs.mkdir(projectDir)
await fs.copy(fixtureDir, projectDir)

const projectExec = (cmd) => exec(cmd, { cwd: projectDir })

await projectExec('yarn')
})

e2e.it('can compile plugin and test specs', {
snapshot: false,
command: 'yarn',
browser: 'electron',
onRun: async (run) => {
await run({
args: `node ${cypressCli} run --dev --project=./`.split(' '),
spawnOpts: {
cwd: projectDir,
shell: true,
},
})
},
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.yarn
.pnp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: "./yarn-berry.cjs"
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,7 @@
import * as head from 'lodash/head'

describe('yarn-v2-pnp', () => {
it('can load package from pnp runtime', () => {
expect(head([1, 2, 3])).to.equal(1)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const wp = require('@cypress/webpack-preprocessor')
const PnpWebpackPlugin = require('pnp-webpack-plugin')

const options = {
webpackOptions: {
module: {
rules: [
{
test: /\.(j|t)sx?$/,
use: [{
loader: require.resolve('babel-loader'),
options: {
presets: [require.resolve('@babel/preset-env'), require.resolve('@babel/preset-typescript')],
},
}],
},
],
},
resolve: {
extensions: ['.js', '.ts'],
plugins: [
PnpWebpackPlugin,
],
},
resolveLoader: {
plugins: [
PnpWebpackPlugin.moduleLoader(module),
],
},

},
}

export default (on, config) => {
on('file:preprocessor', wp(options))

return config
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "yarn-v2-pnp",
"version": "1.0.0",
"dependencies": {
"lodash": "^4.17.21"
},
"devDependencies": {
"@babel/core": "^7.14.6",
"@babel/preset-env": "^7.14.7",
"@babel/preset-typescript": "^7.14.5",
"@cypress/webpack-preprocessor": "^5.9.1",
"babel-loader": "^8.0.6",
"pnp-webpack-plugin": "^1.6.4",
"typescript": "^4.2.4",
"webpack": "^4"
},
"license": "MIT"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"allowJs": true,
"moduleResolution": "node"
},
"include": ["**/*.ts"]
}

Large diffs are not rendered by default.

Loading