Skip to content

Commit

Permalink
fix(testing): run cypress install if cypress is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens committed Oct 14, 2022
1 parent 4b0bff7 commit fbb9b3e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/cypress/src/executors/cypress/cypress.impl.spec.ts
Expand Up @@ -9,6 +9,12 @@ let devkit = require('@nrwl/devkit');

jest.mock('../../utils/cypress-version');
jest.mock('../../utils/ct-helpers');
jest.mock('child_process', () => {
return {
...jest.requireActual('child_process'),
execSync: jest.fn(),
};
});
const Cypress = require('cypress');

describe('Cypress builder', () => {
Expand All @@ -33,6 +39,9 @@ describe('Cypress builder', () => {
(devkit as any).readTargetOptions = jest.fn().mockReturnValue({
watch: true,
});
(devkit as any).getPackageManagerCommand = jest.fn().mockReturnValue({
exec: 'npx',
});
let runExecutor: any;
let mockGetTailwindPath: jest.Mock<ReturnType<typeof getTempTailwindPath>> =
getTempTailwindPath as any;
Expand Down Expand Up @@ -411,6 +420,23 @@ A generator to migrate from v8 to v10 is provided. See https://nx.dev/cypress/v1
);
});

it('should call cypress verify', async () => {
const cp = require('child_process');
const execSpy = jest.spyOn(cp, 'execSync');
await cypressExecutor(cypressOptions, mockContext);
expect(execSpy).toHaveBeenCalledWith('npx cypress verify');
});

it('should make sure cypress is installed', async () => {
const cp = require('child_process');
const execSpy = jest.spyOn(cp, 'execSync').mockImplementationOnce(() => {
throw new Error('blah');
});
await cypressExecutor(cypressOptions, mockContext);
expect(execSpy).toHaveBeenNthCalledWith(1, 'npx cypress verify');
expect(execSpy).toHaveBeenNthCalledWith(2, 'npx cypress install');
});

describe('Component Testing', () => {
beforeEach(() => {
mockGetTailwindPath.mockReturnValue(undefined);
Expand Down
17 changes: 17 additions & 0 deletions packages/cypress/src/executors/cypress/cypress.impl.ts
@@ -1,11 +1,13 @@
import {
ExecutorContext,
getPackageManagerCommand,
logger,
parseTargetString,
readTargetOptions,
runExecutor,
stripIndents,
} from '@nrwl/devkit';
import { execSync } from 'child_process';
import 'dotenv/config';
import { existsSync, unlinkSync } from 'fs';
import { basename, dirname, join } from 'path';
Expand Down Expand Up @@ -49,6 +51,7 @@ export default async function cypressExecutor(
options: CypressExecutorOptions,
context: ExecutorContext
) {
assertCypressInstalled();
options = normalizeOptions(options, context);
// this is used by cypress component testing presets to build the executor contexts with the correct configuration options.
process.env.NX_CYPRESS_TARGET_CONFIGURATION = context.configurationName;
Expand Down Expand Up @@ -144,6 +147,20 @@ A generator to migrate from v8 to v10 is provided. See https://nx.dev/cypress/v1
}
}

function assertCypressInstalled() {
const pm = getPackageManagerCommand();
try {
execSync(`${pm.exec} cypress verify`);
} catch (e) {
if (e.stdout) {
logger.debug(e.stdout.toString());
}

logger.info(`NX Attempting to install Cypress.`);
execSync(`${pm.exec} cypress install`);
}
}

async function* startDevServer(
opts: CypressExecutorOptions,
context: ExecutorContext
Expand Down

0 comments on commit fbb9b3e

Please sign in to comment.