Skip to content

Commit

Permalink
fix(component-testing): correct imports for relative paths in cypress…
Browse files Browse the repository at this point in the history
….json (#16056)
  • Loading branch information
lmiller1990 authored Apr 21, 2021
1 parent 6a32ee3 commit 10b89f8
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion npm/react/cypress/component/advanced/timers/card-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ it('should select null after timing out', () => {
const onSelect = cy.stub().as('selected')

mount(<Card onSelect={onSelect} />)
cy.get('@selected', { timeout: 5500 }).should('have.been.calledWith', null)
cy.get('@selected', { timeout: 6000 }).should('have.been.calledWith', null)
})

it('should accept selections', () => {
Expand Down
8 changes: 6 additions & 2 deletions npm/webpack-dev-server/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* global Cypress */
/// <reference types="cypress" />

import debugFn from 'debug'
import * as path from 'path'
import { CypressCTWebpackContext } from './plugin'
const debug = debugFn('cypress:webpack-dev-server:webpack')

/**
* @param {ComponentSpec} file spec to create import string from.
Expand All @@ -15,8 +17,8 @@ const makeImport = (file: Cypress.Cypress['spec'], filename: string, chunkName:
const magicComments = chunkName ? `/* webpackChunkName: "${chunkName}" */` : ''

return `"${filename}": {
shouldLoad: () => document.location.pathname.includes(${JSON.stringify(file.relative)}),
load: () => import(${JSON.stringify(path.resolve(projectRoot, file.relative))} ${magicComments}),
shouldLoad: () => document.location.pathname.includes("${file.absolute}"),
load: () => import("${file.absolute}" ${magicComments}),
chunkName: "${chunkName}",
}`
}
Expand All @@ -39,6 +41,8 @@ const makeImport = (file: Cypress.Cypress['spec'], filename: string, chunkName:
function buildSpecs (projectRoot: string, files: Cypress.Cypress['spec'][] = []): string {
if (!Array.isArray(files)) return `{}`

debug(`projectRoot: ${projectRoot}, files: ${files.map((f) => f.absolute).join(',')}`)

return `{${files.map((f, i) => {
return makeImport(f, f.name, `spec-${i}`, projectRoot)
}).join(',')}}`
Expand Down
4 changes: 2 additions & 2 deletions npm/webpack-dev-server/src/startServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ export async function start ({ webpackConfig: userWebpackConfig, options, ...use

debug('starting webpack dev server')

const webpackDevServerConfig = {
const webpackDevServerConfig: WebpackDevServer.Configuration = {
...userWebpackConfig.devServer,
hot: false,
inline: false,
publicPath: devServerPublicPathRoute,
noInfo: true,
noInfo: false,
}

return new WebpackDevServer(compiler, webpackDevServerConfig)
Expand Down
7 changes: 6 additions & 1 deletion packages/runner-ct/src/app/RunnerCt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ const buildNavItems = (eventManager: typeof EventManager, toggleIsSetListOpen: (
},
]

const removeRelativeRegexp = /\.\.\//gi

const RunnerCt = namedObserver('RunnerCt',
(props: RunnerCtProps) => {
const searchRef = React.useRef<HTMLInputElement>(null)
Expand All @@ -92,7 +94,10 @@ const RunnerCt = namedObserver('RunnerCt',

const runSpec = React.useCallback((file: FileNode) => {
setActiveIndex(0)
const selectedSpec = props.state.specs.find((spec) => spec.absolute.includes(file.relative))
// We request an absolute path from the dev server but the spec list displays relative paths
// For this reason to match the spec we remove leading relative paths. Eg ../../foo.js -> foo.js.
const filePath = file.relative.replace(removeRelativeRegexp, '')
const selectedSpec = props.state.specs.find((spec) => spec.absolute.includes(filePath))

if (!selectedSpec) {
throw Error(`Could not find spec matching ${file.relative}.`)
Expand Down
2 changes: 1 addition & 1 deletion packages/runner-ct/src/iframe/iframes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import styles from '../app/RunnerCt.module.scss'
import './iframes.scss'

export function getSpecUrl ({ namespace, spec }, prefix = '') {
return spec ? `${prefix}/${namespace}/iframes/${spec.relative}` : ''
return spec ? `${prefix}/${namespace}/iframes/${spec.absolute}` : ''
}

@observer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function greet() {
const el = document.createElement('div')
el.textContent = 'Hello world'
document.body.appendChild(el)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { greet } from './greet'

it('greets', () => {
greet()
cy.get('div').contains('Hello world')
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"component": {
"componentFolder": "../component-files",
"testFiles": "**/*.spec.js"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { startDevServer } = require('@cypress/webpack-dev-server')

module.exports = (on, config) => {
on('dev-server:start', (options) => startDevServer({ options, webpackConfig: {} }))

return config
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"scripts": {
"cy:run": "node ../../../../../../scripts/cypress.js run-ct --project configuration",
"cy:open": "node ../../../../../../scripts/cypress.js open-ct --project configuration"
}
}

4 comments on commit 10b89f8

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 10b89f8 Apr 21, 2021

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.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.2.0/circle-develop-10b89f8d587d331256549c3ab7662f119df7a0f1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 10b89f8 Apr 21, 2021

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.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.2.0/appveyor-develop-10b89f8d587d331256549c3ab7662f119df7a0f1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 10b89f8 Apr 21, 2021

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.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.2.0/appveyor-develop-10b89f8d587d331256549c3ab7662f119df7a0f1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 10b89f8 Apr 21, 2021

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.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.2.0/circle-develop-10b89f8d587d331256549c3ab7662f119df7a0f1/cypress.tgz

Please sign in to comment.