Skip to content

Commit

Permalink
Throw error on this.slow() informing users about slowTestThreshold in…
Browse files Browse the repository at this point in the history
…side tests, hooks and suites.
  • Loading branch information
BlueWinds committed Oct 6, 2021
1 parent be844b2 commit 1496833
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/driver/src/cypress/error_messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,11 @@ export default {
https://on.cypress.io/test-retries
`,
manually_set_slow: stripIndent`\
Mocha \`this.slow()\` syntax is not supported.
The slow threshold can only be configured globally by \`slowTestThreshold\`. See https://on.cypress.io/configuration#Global
`,
hook_registered_late: stripIndent`\
Cypress detected you registered a(n) \`{{hookTitle}}\` hook while a test was running (possibly a hook nested inside another hook). All hooks must be registered before a test begins executing.
Expand Down
36 changes: 36 additions & 0 deletions packages/driver/src/cypress/mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const suiteAddTest = Suite.prototype.addTest
const suiteAddSuite = Suite.prototype.addSuite
const suiteRetries = Suite.prototype.retries
const hookRetries = Hook.prototype.retries
const suiteSlow = Suite.prototype.slow
const hookSlow = Suite.prototype.slow
const suiteBeforeAll = Suite.prototype.beforeAll
const suiteBeforeEach = Suite.prototype.beforeEach
const suiteAfterAll = Suite.prototype.afterAll
Expand Down Expand Up @@ -200,6 +202,14 @@ const restoreSuiteRetries = () => {
Suite.prototype.retries = suiteRetries
}

const restoreSuiteSlow = () => {
Suite.prototype.slow = suiteSlow
}

const restoreHookSlow = () => {
Suite.prototype.slow = hookSlow
}

const restoreTestClone = () => {
Test.prototype.clone = testClone
}
Expand Down Expand Up @@ -264,6 +274,24 @@ const patchHookRetries = () => {
}
}

const patchSuiteSlow = () => {
Suite.prototype.slow = function (...args) {
// Mocha calls this.slow() internally to set the default value of 75ms
// Any other value, we assume it's a user and inform them that this is doesn't actually do anything
if (args[0] !== undefined && args[0] !== 75) {
throw $errUtils.cypressErrByPath('mocha.manually_set_slow')
}
}
}

const patchHookSlow = () => {
Hook.prototype.slow = function (...args) {
if (args[0] !== undefined && args[0] !== 75) {
throw $errUtils.cypressErrByPath('mocha.manually_set_slow')
}
}
}

// matching the current Runner.prototype.fail except
// changing the logic for determing whether this is a valid err
const patchRunnerFail = () => {
Expand Down Expand Up @@ -378,6 +406,10 @@ const patchSuiteAddTest = (specWindow, config) => {
return testRetries.apply(this, args)
}

test.slow = function () {
throw $errUtils.cypressErrByPath('mocha.manually_set_slow')
}

return ret
}
}
Expand Down Expand Up @@ -468,6 +500,8 @@ const restore = () => {
restoreRunnableResetTimeout()
restoreSuiteRetries()
restoreHookRetries()
restoreSuiteSlow()
restoreHookSlow()
restoreRunnerRunTests()
restoreTestClone()
restoreSuiteAddTest()
Expand All @@ -482,6 +516,8 @@ const override = (specWindow, Cypress, config) => {
patchRunnableResetTimeout()
patchSuiteRetries()
patchHookRetries()
patchSuiteSlow()
patchHookSlow()
patchRunnerRunTests()
patchTestClone()
patchSuiteAddTest(specWindow, config)
Expand Down

0 comments on commit 1496833

Please sign in to comment.