Skip to content

Commit

Permalink
fix(testing): omit indexHtmlFile option for cy >12.17.0
Browse files Browse the repository at this point in the history
cypress currently reappends the cwd to the absolute path resulting in invalid paths
switch to use a relative path based from the cypress config location to the support file
but since the file is in the default locaiton it can be omitted for later cypress verions
  • Loading branch information
barbados-clemens committed Aug 29, 2023
1 parent 61d9022 commit a0b956c
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 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 = isCyVersionGreaterThan('12.9.0');

// if using cypress <v12.9.0 then we require the offset
if (!supportsWorkspaceRoot) {
Expand Down Expand Up @@ -413,3 +413,35 @@ 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 !isCyVersionGreaterThan('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 isCyVersionGreaterThan(version: string) {
const { version: cyVersion = null } = require('cypress/package.json');

console.log({ cyVersion });
return !!cyVersion && gte(cyVersion, version);
}

0 comments on commit a0b956c

Please sign in to comment.