Skip to content

Commit 4a97a52

Browse files
fix: Do not screenshot or trigger the failed event when tests are skipped (#19331)
1 parent 09bcc5b commit 4a97a52

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const { Screenshot } = Cypress
2+
3+
let failedEventFired = false
4+
5+
Cypress.on('fail', (error) => {
6+
failedEventFired = true
7+
throw new Error(error)
8+
})
9+
10+
let screenshotTaken = false
11+
12+
Screenshot.defaults({ onAfterScreenshot: () => {
13+
screenshotTaken = true
14+
} })
15+
16+
const pendingTests = []
17+
const passedTests = []
18+
19+
Cypress.on('test:after:run', (test) => {
20+
if (test.state === 'pending') {
21+
return pendingTests.push(test)
22+
}
23+
24+
if (test.state === 'passed') {
25+
return passedTests.push(test)
26+
}
27+
})
28+
29+
beforeEach(() => {
30+
// Set isInteractive to false to ensure that screenshots will be
31+
// triggered in both run and open mode
32+
Cypress.config('isInteractive', false)
33+
})
34+
35+
describe('skipped test', () => {
36+
it('does not fail', function () {
37+
cy.then(() => {
38+
this.skip()
39+
}).then(() => {
40+
expect(true).to.be.false
41+
})
42+
})
43+
44+
it('does not prevent subsequent tests from running', () => {
45+
expect(true).to.be.true
46+
})
47+
})
48+
49+
describe('skipped test side effects', () => {
50+
it('does not have a screenshot taken', () => {
51+
expect(screenshotTaken).to.be.false
52+
})
53+
54+
it('does not fire failed event', () => {
55+
expect(failedEventFired).to.be.false
56+
})
57+
58+
it('does still mark all tests with the correct state', () => {
59+
expect(pendingTests).to.have.length(1)
60+
expect(passedTests).to.have.length(3)
61+
})
62+
})
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const { Screenshot } = Cypress
2+
3+
let failedEventFired = false
4+
5+
Cypress.on('fail', (error) => {
6+
failedEventFired = true
7+
throw new Error(error)
8+
})
9+
10+
let screenshotTaken = false
11+
12+
Screenshot.defaults({ onAfterScreenshot: () => {
13+
screenshotTaken = true
14+
} })
15+
16+
const pendingTests = []
17+
const passedTests = []
18+
19+
Cypress.on('test:after:run', (test) => {
20+
if (test.state === 'pending') {
21+
return pendingTests.push(test)
22+
}
23+
24+
if (test.state === 'passed') {
25+
return passedTests.push(test)
26+
}
27+
})
28+
29+
beforeEach(() => {
30+
// Set isInteractive to false to ensure that screenshots will be
31+
// triggered in both run and open mode
32+
Cypress.config('isInteractive', false)
33+
})
34+
35+
describe('generally skipped test', () => {
36+
before(function () {
37+
this.skip()
38+
})
39+
40+
it('does not fail', function () {
41+
expect(true).to.be.false
42+
})
43+
})
44+
45+
describe('individually skipped tests', () => {
46+
it('does not fail when using this.skip', function () {
47+
this.skip()
48+
expect(true).to.be.false
49+
})
50+
51+
// NOTE: We are skipping this test in order to test skip functionality
52+
it.skip('does not fail when using it.skip', () => {
53+
expect(true).to.be.false
54+
})
55+
})
56+
57+
describe('skipped test side effects', () => {
58+
it('does not have a screenshot taken', () => {
59+
expect(screenshotTaken).to.be.false
60+
})
61+
62+
it('does not fire failed event', () => {
63+
expect(failedEventFired).to.be.false
64+
})
65+
66+
it('does still mark all tests with the correct state', () => {
67+
expect(pendingTests).to.have.length(3)
68+
expect(passedTests).to.have.length(2)
69+
})
70+
})

packages/driver/src/cypress/command_queue.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,14 @@ export class CommandQueue extends Queue<Command> {
343343
}
344344

345345
const onError = (err: Error | string) => {
346+
// If the runnable was marked as pending, this test was skipped
347+
// go ahead and just return
348+
const runnable = this.state('runnable')
349+
350+
if (runnable.isPending()) {
351+
return
352+
}
353+
346354
if (this.state('onCommandFailed')) {
347355
return this.state('onCommandFailed')(err, this, next)
348356
}

packages/driver/src/cypress/cy.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,12 @@ export class $Cy implements ITimeouts, IStability, IAssertions, IRetries, IJQuer
942942
// else just return ret
943943
return ret
944944
} catch (err) {
945+
// If the runnable was marked as pending, this test was skipped
946+
// go ahead and just return
947+
if (runnable.isPending()) {
948+
return
949+
}
950+
945951
// if runnable.fn threw synchronously, then it didnt fail from
946952
// a cypress command, but we should still teardown and handle
947953
// the error

0 commit comments

Comments
 (0)