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

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

Merged
29 changes: 21 additions & 8 deletions scripts/test/globalSetup.ts
@@ -1,20 +1,33 @@
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!\n' +
'WebdriverIO needs to hide the "got" dependency (at "packages/webdriver/node_modules/got")\n' +
'during the test to force Vitest to use our mocked version. WebdriverIO does this by\n' +
'renaming "packages/webdriver/node_modules/got" to "packages/webdriver/node_modules/tmp_got"\n' +
'during test setup and back again during test tear-down. \n\n' +
'%s has failed. Maybe because you are already running unit tests in a different\n' +
'terminal.\n\n'
christian-bromann marked this conversation as resolved.
Show resolved Hide resolved

function throwBetterErrorMessage (err: Error) {
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 mv packages/webdriver/node_modules/tmp_got packages/webdriver/node_modules/got\n'
christian-bromann marked this conversation as resolved.
Show resolved Hide resolved
)
}

function throwBetterErrorMessageTearDown (err: Error) {
throw new Error(
util.format(ERROR_MESSAGE, 'Tear-down') +
err.stack +
christian-bromann marked this conversation as resolved.
Show resolved Hide resolved
'\n\nTo correct this error please check you have the file: "packages/webdriver/node_modules/got"\n'
christian-bromann marked this conversation as resolved.
Show resolved Hide resolved
)
}

Expand All @@ -25,9 +38,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)
}