Skip to content

Commit

Permalink
Fix resolve config path warning when multiple configs found
Browse files Browse the repository at this point in the history
  • Loading branch information
jeparlefrancais committed Apr 2, 2024
1 parent 2b5dec4 commit 6e61571
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
36 changes: 25 additions & 11 deletions src/jest-config/src/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local exports = {}
-- ROBLOX deviation END
local JEST_CONFIG_BASE_NAME = "jest.config"
exports.JEST_CONFIG_BASE_NAME = JEST_CONFIG_BASE_NAME
-- ROBLOX deviation START: not needed
-- ROBLOX deviation START: not supported
-- local JEST_CONFIG_EXT_CJS = ".cjs"
-- exports.JEST_CONFIG_EXT_CJS = JEST_CONFIG_EXT_CJS
-- local JEST_CONFIG_EXT_MJS = ".mjs"
Expand All @@ -30,16 +30,30 @@ exports.JEST_CONFIG_BASE_NAME = JEST_CONFIG_BASE_NAME
-- exports.JEST_CONFIG_EXT_JS = JEST_CONFIG_EXT_JS
-- local JEST_CONFIG_EXT_TS = ".ts"
-- exports.JEST_CONFIG_EXT_TS = JEST_CONFIG_EXT_TS
-- local JEST_CONFIG_EXT_JSON = ".json"
-- exports.JEST_CONFIG_EXT_JSON = JEST_CONFIG_EXT_JSON
-- local JEST_CONFIG_EXT_ORDER = Object.freeze({
-- JEST_CONFIG_EXT_JS,
-- JEST_CONFIG_EXT_TS,
-- JEST_CONFIG_EXT_MJS,
-- JEST_CONFIG_EXT_CJS,
-- JEST_CONFIG_EXT_JSON,
-- })
-- exports.JEST_CONFIG_EXT_ORDER = JEST_CONFIG_EXT_ORDER
-- ROBLOX deviation END
local JEST_CONFIG_EXT_JSON = ".json"
exports.JEST_CONFIG_EXT_JSON = JEST_CONFIG_EXT_JSON
-- ROBLOX deviation START: support Lua and Luau extensions
local JEST_CONFIG_EXT_LUA = ".lua"
exports.JEST_CONFIG_EXT_LUA = JEST_CONFIG_EXT_LUA
local JEST_CONFIG_EXT_LUAU = ".luau"
exports.JEST_CONFIG_EXT_LUAU = JEST_CONFIG_EXT_LUAU
-- ROBLOX deviation END
local JEST_CONFIG_EXT_ORDER = {
-- ROBLOX deviation START: support Lua and Luau extensions
JEST_CONFIG_EXT_LUA,
JEST_CONFIG_EXT_LUAU,
-- ROBLOX deviation END
-- ROBLOX deviation START: not supported
-- JEST_CONFIG_EXT_JS,
-- JEST_CONFIG_EXT_TS,
-- JEST_CONFIG_EXT_MJS,
-- JEST_CONFIG_EXT_CJS,
-- ROBLOX deviation END
-- ROBLOX deviation START: not supported (but for now)
-- JEST_CONFIG_EXT_JSON,
-- ROBLOX deviation END
}
exports.JEST_CONFIG_EXT_ORDER = JEST_CONFIG_EXT_ORDER

return exports
45 changes: 19 additions & 26 deletions src/jest-config/src/resolveConfigPath.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ local LuauPolyfill = require("@pkg/@jsdotlua/luau-polyfill")
local Array = LuauPolyfill.Array
local Error = LuauPolyfill.Error
local String = LuauPolyfill.String
local console = LuauPolyfill.console
type Array<T> = LuauPolyfill.Array<T>

local exports = {}
Expand All @@ -23,15 +22,18 @@ local chalk = require("@pkg/@jsdotlua/chalk")
-- local fs = require("@pkg/graceful-fs")
-- local slash = require("@pkg/slash")
-- ROBLOX deviation END
local jestValidateModule = require("@pkg/@jsdotlua/jest-validate")
local ValidationError = jestValidateModule.ValidationError
local typesModule = require("@pkg/@jsdotlua/jest-types")
type Config_Path = typesModule.Config_Path
local constantsModule = require("./constants")
local JEST_CONFIG_BASE_NAME = constantsModule.JEST_CONFIG_BASE_NAME
local JEST_CONFIG_EXT_ORDER = constantsModule.JEST_CONFIG_EXT_ORDER
-- ROBLOX deviation START: not needed
-- local JEST_CONFIG_EXT_ORDER = constantsModule.JEST_CONFIG_EXT_ORDER
-- local PACKAGE_JSON = constantsModule.PACKAGE_JSON
-- ROBLOX deviation END
local utilsModule = require("./utils")
local BULLET = utilsModule.BULLET
local DOCUMENTATION_NOTE = utilsModule.DOCUMENTATION_NOTE

-- ROBLOX deviation START: predefine functions
Expand Down Expand Up @@ -127,7 +129,7 @@ function resolveConfigPathByTraversing(
end) :: Array<any>
) :: Array<ModuleScript>
if not skipMultipleConfigWarning and #configFiles > 1 then
console.warn(makeMultipleConfigsWarning(configFiles))
error(ValidationError.new(makeMultipleConfigsWarning(configFiles)))
end
if #configFiles > 0 then
return configFiles[1]
Expand Down Expand Up @@ -175,26 +177,18 @@ function makeResolutionErrorMessage(
cwd: Instance
): string
return "Could not find a config file based on provided values:\n"
.. ('path: "%s"\n'):format(tostring(initialPath))
.. ('cwd: "%s"\n'):format(tostring(cwd))
.. `path: "{initialPath}"\n`
.. `cwd: "{cwd}"\n`
-- ROBLOX deviation START: align message to make more sense in Luau context
.. "Config paths must be specified by either a direct path to a config script\n"
.. "or a path to a directory. If directory is given, Jest will try to\n"
-- ROBLOX deviation END
.. ("traverse directory tree up, until it finds one of those files in exact order: %s."):format(
Array.join(
Array.map(
-- ROBLOX deviation START: only support Lua config files
-- JEST_CONFIG_EXT_ORDER
{ ".lua" },
-- ROBLOX deviation END
function(ext)
return ('"%s"'):format((getConfigFilename(ext)))
end
),
" or "
)
)
.. `traverse directory tree up, until it finds one of those files in exact order: {Array.join(
Array.map(JEST_CONFIG_EXT_ORDER, function(ext)
return `"{getConfigFilename(ext)}"`
end),
" or "
)}.`
end

local function extraIfPackageJson(configPath: Config_Path)
Expand All @@ -206,14 +200,14 @@ local function extraIfPackageJson(configPath: Config_Path)
return ""
end

function makeMultipleConfigsWarning(configPaths: Array<ModuleScript>)
return chalk.yellow({
function makeMultipleConfigsWarning(configPaths: Array<ModuleScript>): (string, string, string)
return `{BULLET}{chalk.bold("Multiple configurations found")}`,
Array.join(
Array.concat(
{},
{ chalk.bold("\u{25cf} Multiple configurations found:") },
Array.map(configPaths, function(configPath)
return (" * %s%s"):format(tostring(extraIfPackageJson(configPath)), tostring(configPath))
-- ROBLOX deviation START: replace slash command with GetFullName
return ` * {extraIfPackageJson(configPath)}{configPath:GetFullName()}`
-- ROBLOX deviation END
end),
{
"",
Expand All @@ -223,8 +217,7 @@ function makeMultipleConfigsWarning(configPaths: Array<ModuleScript>)
),
"\n"
),
DOCUMENTATION_NOTE,
})
DOCUMENTATION_NOTE
end

return exports

0 comments on commit 6e61571

Please sign in to comment.