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

chore(data-context): update runSpec mutation to support absolute spec path #26973

Merged
merged 8 commits into from
Jun 13, 2023
2 changes: 1 addition & 1 deletion packages/app/cypress/e2e/cypress-in-cypress.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ describe('Cypress in Cypress', { viewportWidth: 1500, defaultCommandTimeout: 100

cy.withCtx(async (ctx) => {
const url = `http://127.0.0.1:${ctx.gqlServerPort}/__launchpad/graphql?`
const payload = `{"query":"mutation{\\nrunSpec(specPath:\\"cypress/e2e/dom-content.spec.js\\"){\\n__typename\\n... on RunSpecResponse{\\ntestingType\\nbrowser{\\nid\\nname\\n}\\nspec{\\nid\\nname\\n}\\n}\\n}\\n}","variables":null}`
const payload = `{"query":"mutation{\\nrunSpec(specPath:\\"${ctx.currentProject}/cypress/e2e/dom-content.spec.js\\"){\\n__typename\\n... on RunSpecResponse{\\ntestingType\\nbrowser{\\nid\\nname\\n}\\nspec{\\nid\\nname\\n}\\n}\\n}\\n}","variables":null}`

ctx.coreData.app.browserStatus = 'open'

Expand Down
21 changes: 11 additions & 10 deletions packages/data-context/src/actions/ProjectActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,26 +512,27 @@ export class ProjectActions {

let targetTestingType: TestingType

// Get relative path from the specPath to determine which testing type from the specPattern
const relativeSpecPath = path.relative(this.ctx.currentProject, specPath)

// Check to see whether input specPath matches the specPattern for one or the other testing type
// If it matches neither then we can't run the spec and we should error
if (await this.ctx.project.matchesSpecPattern(specPath, 'e2e')) {
if (await this.ctx.project.matchesSpecPattern(relativeSpecPath, 'e2e')) {
targetTestingType = 'e2e'
} else if (await this.ctx.project.matchesSpecPattern(specPath, 'component')) {
} else if (await this.ctx.project.matchesSpecPattern(relativeSpecPath, 'component')) {
targetTestingType = 'component'
} else {
throw new RunSpecError('NO_SPEC_PATTERN_MATCH', 'Unable to determine testing type, spec does not match any configured specPattern')
}

debug(`Spec %s matches '${targetTestingType}' pattern`, specPath)

const absoluteSpecPath = this.ctx.path.resolve(this.ctx.currentProject, specPath)

debug('Attempting to launch spec %s', absoluteSpecPath)
debug('Attempting to launch spec %s', specPath)

// Look to see if there's actually a file at the target location
// This helps us avoid switching testingType *then* finding out the spec doesn't exist
if (!this.ctx.fs.existsSync(absoluteSpecPath)) {
throw new RunSpecError('SPEC_NOT_FOUND', `No file exists at path ${absoluteSpecPath}`)
if (!this.ctx.fs.existsSync(specPath)) {
throw new RunSpecError('SPEC_NOT_FOUND', `No file exists at path ${specPath}`)
}

// We now know what testingType we need to be in - if we're already there, great
Expand Down Expand Up @@ -609,11 +610,11 @@ export class ProjectActions {
// a matching file exists above it may not end up loading as a valid spec so we validate that here
//
// Have to use toPosix here to align windows absolute paths with how the absolute path is storied in the data context
const spec = this.ctx.project.getCurrentSpecByAbsolute(toPosix(absoluteSpecPath))
const spec = this.ctx.project.getCurrentSpecByAbsolute(toPosix(specPath))

if (!spec) {
debug(`Spec not found with path: ${absoluteSpecPath}`)
throw new RunSpecError('SPEC_NOT_FOUND', `Unable to find matching spec with path ${absoluteSpecPath}`)
debug(`Spec not found with path: ${specPath}`)
throw new RunSpecError('SPEC_NOT_FOUND', `Unable to find matching spec with path ${specPath}`)
}

const browser = this.ctx.coreData.activeBrowser!
Expand Down
14 changes: 7 additions & 7 deletions packages/data-context/test/unit/actions/ProjectActions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('ProjectActions', () => {
describe('runSpec', () => {
context('no project', () => {
it('should fail with `NO_PROJECT`', async () => {
const result = await ctx.actions.project.runSpec({ specPath: 'e2e/abc.cy.ts' })
const result = await ctx.actions.project.runSpec({ specPath: '/Users/blah/Desktop/application/cypress/e2e/abc.cy.ts' })

sinon.assert.match(result, {
code: 'NO_PROJECT',
Expand Down Expand Up @@ -138,7 +138,7 @@ describe('ProjectActions', () => {
})

it('should fail with `NO_SPEC_PATTERN_MATCH`', async () => {
const result = await ctx.actions.project.runSpec({ specPath: 'e2e/abc.cy.ts' })
const result = await ctx.actions.project.runSpec({ specPath: '/Users/blah/Desktop/application/e2e/abc.cy.ts' })

sinon.assert.match(result, {
code: 'NO_SPEC_PATTERN_MATCH',
Expand All @@ -155,7 +155,7 @@ describe('ProjectActions', () => {
})

it('should fail with `SPEC_NOT_FOUND`', async () => {
const result = await ctx.actions.project.runSpec({ specPath: 'e2e/abc.cy.ts' })
const result = await ctx.actions.project.runSpec({ specPath: '/Users/blah/Desktop/application/e2e/abc.cy.ts' })

sinon.assert.match(result, {
code: 'SPEC_NOT_FOUND',
Expand All @@ -174,7 +174,7 @@ describe('ProjectActions', () => {
})

it('should fail with `TESTING_TYPE_NOT_CONFIGURED`', async () => {
const result = await ctx.actions.project.runSpec({ specPath: 'e2e/abc.cy.ts' })
const result = await ctx.actions.project.runSpec({ specPath: '/Users/blah/Desktop/application/e2e/abc.cy.ts' })

sinon.assert.match(result, {
code: 'TESTING_TYPE_NOT_CONFIGURED',
Expand Down Expand Up @@ -207,7 +207,7 @@ describe('ProjectActions', () => {
})

it('should succeed', async () => {
const result = await ctx.actions.project.runSpec({ specPath: 'e2e/abc.cy.ts' })
const result = await ctx.actions.project.runSpec({ specPath: '/Users/blah/Desktop/application/e2e/abc.cy.ts' })

sinon.assert.match(result, {
testingType: 'e2e',
Expand All @@ -228,7 +228,7 @@ describe('ProjectActions', () => {
})

it('should succeed', async () => {
const result = await ctx.actions.project.runSpec({ specPath: 'e2e/abc.cy.ts' })
const result = await ctx.actions.project.runSpec({ specPath: '/Users/blah/Desktop/application/e2e/abc.cy.ts' })

sinon.assert.match(result, {
testingType: 'e2e',
Expand All @@ -248,7 +248,7 @@ describe('ProjectActions', () => {
})

it('should succeed', async () => {
const result = await ctx.actions.project.runSpec({ specPath: 'e2e/abc.cy.ts' })
const result = await ctx.actions.project.runSpec({ specPath: '/Users/blah/Desktop/application/e2e/abc.cy.ts' })

sinon.assert.match(result, {
testingType: 'e2e',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ export const mutation = mutationType({
type: RunSpecResult,
args: {
specPath: nonNull(stringArg({
description: 'Relative path of spec to run from Cypress project root - must match e2e or component specPattern',
description: 'Absolute path of the spec to run - must match e2e or component specPattern',
})),
},
resolve: async (source, args, ctx) => {
Expand Down