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

feat(cypress): add support for cypress-multi-reporter #726

Merged
merged 10 commits into from
Jul 16, 2024
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { nxE2EPreset } from '@nrwl/cypress/plugins/cypress-preset';
import { defineConfig } from 'cypress';

const cypressJsonConfig = {};

export default defineConfig({
reporter: "cypress-multi-reporters",
reporterOptions: {
configFile: 'reporter-config.json',
},
e2e: {
...nxE2EPreset(__dirname, {}),
...cypressJsonConfig,
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { faker } from '@faker-js/faker';

function login() {
return faker;
}

Cypress.Commands.add('login', login);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import '@testing-library/cypress/add-commands';
import './commands';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@fixtures/cypress",
"devDependencies": {
"cypress": "*",
"cypress-multi-reporters": "*",
"mocha-junit-reporter": "*",
"mochawesome": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"reporterEnabled": "mochawesome, mocha-junit-reporter , @testing-library/my-fake-reporter"
}

37 changes: 37 additions & 0 deletions packages/knip/src/plugins/cypress/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { PluginOptions } from '#p/types/plugins.js';
import { isInternal, toAbsolute } from '#p/util/path.js';
import { load, resolveEntry } from '#p/util/plugin.js';
import type { CypressConfig } from './types.js';

interface ReporterConfig {
reporterEnabled: string;
}

export const resolveDependencies = async (config: CypressConfig, options: PluginOptions) => {
const { reporter } = config;
const { configFileDir } = options;

const resolve = (specifier: string) => resolveEntry(options, specifier);

// Initialize the array of reporters with the initial reporter if present.
const reporters = reporter ? new Set(reporter) : new Set();
webpro marked this conversation as resolved.
Show resolved Hide resolved
// https://github.com/YOU54F/cypress-plugins/tree/master/cypress-multi-reporters#configuring-reporters
if (reporter === 'cypress-multi-reporters' && config.reporterOptions?.configFile) {
// Try to resolve the config file if present and attach the reporters listed in it.
const { configFile } = config.reporterOptions;
const configFilePath = toAbsolute(configFile, configFileDir);
if (isInternal(configFilePath)) {
const reporterConfig: ReporterConfig = await load(configFilePath);
if (typeof reporterConfig === 'object' && reporterConfig.reporterEnabled) {
const { reporterEnabled: reporterConcatenatedNames } = reporterConfig;
// Pulled from the reporter source code, https://github.com/YOU54F/cypress-plugins/blob/master/cypress-multi-reporters/lib/MultiReporters.js#L50-L58
// Not sure why they allow for extra whitespace characters, but let's handle it the same as them.
const reporterNames = reporterConcatenatedNames.split(',');
for (const reporterName of reporterNames) {
reporters.add(resolve(reporterName.trim()));
}
}
}
}
return [...reporters];
aramissennyeydd marked this conversation as resolved.
Show resolved Hide resolved
};
9 changes: 8 additions & 1 deletion packages/knip/src/plugins/cypress/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { IsPluginEnabled, Plugin, ResolveEntryPaths } from '#p/types/plugins.js';
import type { IsPluginEnabled, Plugin, ResolveConfig, ResolveEntryPaths } from '#p/types/plugins.js';
import { hasDependency } from '#p/util/plugin.js';
import { toEntryPattern } from '../../util/protocols.js';
import { resolveDependencies } from './helpers.js';
import type { CypressConfig } from './types.js';

// https://docs.cypress.io/guides/references/configuration

Expand Down Expand Up @@ -31,11 +33,16 @@ const resolveEntryPaths: ResolveEntryPaths = async localConfig => {
].map(toEntryPattern);
};

const resolveConfig: ResolveConfig<CypressConfig> = (config, options) => {
return resolveDependencies(config, options);
};

export default {
title,
enablers,
isEnabled,
config,
entry,
resolveEntryPaths,
resolveConfig,
} satisfies Plugin;
4 changes: 4 additions & 0 deletions packages/knip/src/plugins/cypress/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface CypressConfig {
aramissennyeydd marked this conversation as resolved.
Show resolved Hide resolved
reporter: string;
reporterOptions?: { configFile?: string };
}
28 changes: 28 additions & 0 deletions packages/knip/test/plugins/cypress-multi-reporter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { test } from 'bun:test';
import assert from 'node:assert/strict';
import { main } from '../../src/index.js';
import { resolve } from '../../src/util/path.js';
import baseArguments from '../helpers/baseArguments.js';
import baseCounters from '../helpers/baseCounters.js';

const cwd = resolve('fixtures/plugins/cypress-multi-reporter');

test('Find dependencies with the cypress-multi-reporter plugin', async () => {
const { issues, counters } = await main({
...baseArguments,
cwd,
});

assert(issues.unlisted['cypress.config.ts']['@nrwl/cypress/plugins/cypress-preset']);
assert(issues.unlisted['cypress/support/commands.ts']['@faker-js/faker']);
assert(issues.unlisted['cypress/support/e2e.ts']['@testing-library/cypress/add-commands']);
assert(issues.unlisted['cypress.config.ts']['@testing-library/my-fake-reporter']);
aramissennyeydd marked this conversation as resolved.
Show resolved Hide resolved

assert.deepEqual(counters, {
...baseCounters,
devDependencies: 0,
unlisted: 4,
processed: 3,
total: 3,
});
});
Loading