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

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}