Skip to content

Commit

Permalink
fix(testing): omit indexHtmlFile option for cy >12.17.0 for ng ct (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens authored Aug 29, 2023
1 parent 86c21c6 commit 829076d
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions packages/angular/plugins/component-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

0 comments on commit 829076d

Please sign in to comment.