Skip to content

Commit

Permalink
Modified globalSetup.ts to make reason and origin of throwBetterError…
Browse files Browse the repository at this point in the history
…Message more clear (#9336)

* Modified globalSetup.ts to make reason and origin of throwBetterErrorMessage more clear

* Abstracyed the common error message into a formatted string.

* Update scripts/test/globalSetup.ts

* Update scripts/test/globalSetup.ts

* Update scripts/test/globalSetup.ts

* Update scripts/test/globalSetup.ts

* Update scripts/test/globalSetup.ts

Co-authored-by: Christian Bromann <git@bromann.dev>
  • Loading branch information
RossVertizan and christian-bromann committed Dec 6, 2022
1 parent 3dc4b9d commit 99d788e
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions scripts/test/globalSetup.ts
@@ -1,20 +1,39 @@
import fs from 'node:fs/promises'
import util from 'node:util'
import url from 'node:url'
import path from 'node:path'

const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
const nodeModulesPath = path.join(__dirname, '..', '..', 'packages', 'webdriver', 'node_modules')
const gotPath = path.join(nodeModulesPath, 'got')
const tmpGotPath = path.join(nodeModulesPath, 'tmp_got')
const ERROR_MESSAGE = `Renaming "got" dependency failed!
function throwBetterErrorMessage (err: Error) {
WebdriverIO needs to hide the "got" dependency (at "packages/webdriver/node_modules/got")
during the test to force Vitest to use our mocked version. WebdriverIO does this by renaming:
"packages/webdriver/node_modules/got"
to
"packages/webdriver/node_modules/tmp_got"
during test setup and back again during test tear-down.
'%s has failed. Maybe because you are already running unit tests in a different
'terminal.
`

function throwBetterErrorMessageSetup (err: Error) {
throw new Error(
'Renaming "got" dependency failed!\n' +
'We need to remove rename the got dependency (at "packages/webdriver/node_modules/got") ' +
'during the test to force Vitest to use our mocked version. You might already run unit ' +
'tests in a different terminal.\n\n' +
util.format(ERROR_MESSAGE, 'Setup') +
err.stack +
'\n\nPlease run:\n mv packages/webdriver/node_modules/tmp_got packages/webdriver/node_modules/got'
'\n\nTo correct this error please run:\n\tmv packages/webdriver/node_modules/tmp_got packages/webdriver/node_modules/got\n'
)
}

function throwBetterErrorMessageTearDown (err: Error) {
throw new Error(
util.format(ERROR_MESSAGE, 'Tear-down') +
err.stack
)
}

Expand All @@ -25,9 +44,9 @@ function throwBetterErrorMessage (err: Error) {
* the deps.inline option. This is a workaround for this.
*/
export const setup = async () => {
await fs.rename(gotPath, tmpGotPath).catch(throwBetterErrorMessage)
await fs.rename(gotPath, tmpGotPath).catch(throwBetterErrorMessageSetup)
}

export const teardown = async () => {
await fs.rename(tmpGotPath, gotPath).catch(throwBetterErrorMessage)
await fs.rename(tmpGotPath, gotPath).catch(throwBetterErrorMessageTearDown)
}

0 comments on commit 99d788e

Please sign in to comment.