fix(testing): omit indexHtmlFile option for cy >12.17.0 for ng ct (#18839)

This commit is contained in:
Caleb Ukle 2023-08-29 10:55:26 -05:00 committed by GitHub
parent 86c21c6064
commit 829076d0e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -104,15 +104,17 @@ ${e.stack ? e.stack : e}`
...nxBaseCypressPreset(pathToConfig, { testingType: 'component' }),
// NOTE: cannot use a glob pattern since it will break cypress generated tsconfig.
specPattern: ['src/**/*.cy.ts', 'src/**/*.cy.js'],
// cypress defaults to a relative path from the workspaceRoot instead of projectRoot
// set as absolute path in case this changes internally to cypress, this path isn't OS dependent
indexHtmlFile: joinPathFragments(
ctContext.root,
ctProjectConfig.root,
'cypress',
'support',
'component-index.html'
),
// Cy v12.17.0+ does not work with aboslute paths for index file
// but does with relative pathing, since relative path is the default location, we can omit it
indexHtmlFile: requiresAbsolutePath()
? joinPathFragments(
ctContext.root,
ctProjectConfig.root,
'cypress',
'support',
'component-index.html'
)
: undefined,
devServer: {
// cypress uses string union type,
// need to use const to prevent typing to string
@ -376,9 +378,7 @@ function isOffsetNeeded(
ctProjectConfig: ProjectConfiguration
) {
try {
const { version = null } = require('cypress/package.json');
const supportsWorkspaceRoot = !!version && gte(version, '12.9.0');
const supportsWorkspaceRoot = isCyVersionGreaterThanOrEqual('12.9.0');
// if using cypress <v12.9.0 then we require the offset
if (!supportsWorkspaceRoot) {
@ -413,3 +413,34 @@ function isOffsetNeeded(
return true;
}
}
/**
* check if the cypress version is able to understand absolute paths to the indexHtmlFile option
* this is required for nx to work with cypress <v12.17.0 since the relative pathing is causes issues
* with invalid pathing.
* v12.17.0+ works with relative pathing
*
* if there is an error thrown then we assume it is an older version of cypress and use the absolute path
* as that was supported for longer.
*
* */
function requiresAbsolutePath() {
try {
return !isCyVersionGreaterThanOrEqual('12.17.0');
} catch (e) {
if (process.env.NX_VERBOSE_LOGGING === 'true') {
logger.error(e);
}
return true;
}
}
/**
* Checks if the install cypress version is greater than or equal to the provided version.
* Does not catch errors as any custom logic for error handling is required on consumer side.
* */
function isCyVersionGreaterThanOrEqual(version: string) {
const { version: cyVersion = null } = require('cypress/package.json');
return !!cyVersion && gte(cyVersion, version);
}