This repository was archived by the owner on Sep 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cypress] Add synchronous config wrapper for CommonJS
- Loading branch information
Showing
22 changed files
with
295 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) 2023 Developer Innovations, LLC | ||
|
||
import { wrapCypressConfig } from "./index"; | ||
import _debug from "debug"; | ||
import { loadUserConfigSync } from "./load-user-config"; | ||
|
||
const debug = _debug("unflakable:config-wrapper-sync"); | ||
|
||
const userConfig = loadUserConfigSync(); | ||
debug("Loaded user config %o", userConfig); | ||
|
||
export default wrapCypressConfig(userConfig); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2023 Developer Innovations, LLC | ||
|
||
import _debug from "debug"; | ||
import { require } from "./utils"; | ||
import { | ||
ENV_VAR_USER_CONFIG_JSON, | ||
ENV_VAR_USER_CONFIG_PATH, | ||
} from "./config-env-vars"; | ||
import path from "path"; | ||
|
||
const debug = _debug("unflakable:load-user-config"); | ||
|
||
export type LoadedConfig = | ||
| { | ||
default: Cypress.ConfigOptions<unknown>; | ||
} | ||
| (Cypress.ConfigOptions<unknown> & { default: undefined }); | ||
|
||
export const loadUserConfigSync = (): Cypress.ConfigOptions<unknown> => { | ||
if (ENV_VAR_USER_CONFIG_JSON.value !== undefined) { | ||
debug(`Parsing inline user config ${ENV_VAR_USER_CONFIG_JSON.value}`); | ||
|
||
return JSON.parse( | ||
ENV_VAR_USER_CONFIG_JSON.value | ||
) as Cypress.ConfigOptions<unknown>; | ||
} else if (ENV_VAR_USER_CONFIG_PATH.value === undefined) { | ||
throw new Error("No user config to load"); | ||
} | ||
|
||
debug(`Loading user config from ${ENV_VAR_USER_CONFIG_PATH.value}`); | ||
|
||
// Relative paths from the user's config need to resolve relative to the location of their | ||
// cypress.config.js, not ours. This affects things like webpack for component testing. | ||
const configPathDir = path.dirname(ENV_VAR_USER_CONFIG_PATH.value); | ||
debug(`Changing working directory to ${configPathDir}`); | ||
process.chdir(configPathDir); | ||
|
||
// For CommonJS projects, we need to use require(), at least for TypeScript config files. | ||
// Dynamic import() doesn't support TypeScript imports in CommonJS projects, at least the way | ||
// Cypress sets up the environment before loading the config. | ||
const config = require(ENV_VAR_USER_CONFIG_PATH.value) as LoadedConfig; | ||
return config.default ?? config; | ||
}; | ||
|
||
export const loadUserConfig = async (): Promise< | ||
Cypress.ConfigOptions<unknown> | ||
> => { | ||
// For CommonJS projects, we need to use require(), at least for TypeScript config files. | ||
// Dynamic import() doesn't support TypeScript imports in CommonJS projects, at least the way | ||
// Cypress sets up the environment before loading the config. | ||
try { | ||
return loadUserConfigSync(); | ||
} catch (e) { | ||
// require() can't import ES modules, so now we try a dynamic import(). This is what gets used | ||
// for ESM projects. | ||
debug(`require() failed; attempting dynamic import(): ${e as string}`); | ||
const config = (await import( | ||
ENV_VAR_USER_CONFIG_PATH.value as string | ||
)) as LoadedConfig; | ||
return config.default ?? config; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.