Skip to content

Commit

Permalink
Combine enabled properties into a single isTrackingState
Browse files Browse the repository at this point in the history
This will become important in order to allow generating JSON / HTML
reports *without* enabling messages. As explained in #1140 [1], we want
to avoid touching the file system unless necessary. For pretty output,
this is definitely not necessary. For JSON / HTML reports, this means a
seemingly unrelated and implicit report appearing.

[1] #1140
  • Loading branch information
badeball committed Dec 19, 2023
1 parent 0b88784 commit fda0b90
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 116 deletions.
23 changes: 8 additions & 15 deletions lib/browser-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ interface CompositionContext {
specEnvelopes: messages.Envelope[];
testFilter: Node;
omitFiltered: boolean;
prettyEnabled: boolean;
messagesEnabled: boolean;
isTrackingState: boolean;
stepDefinitionHints: {
stepDefinitions: string | string[];
stepDefinitionPatterns: string[];
Expand Down Expand Up @@ -131,12 +130,8 @@ function retrieveInternalSuiteProperties():
return Cypress.env(INTERNAL_SUITE_PROPERTIES);
}

function shouldPropagateMessages(context: CompositionContext) {
return context.prettyEnabled || context.messagesEnabled;
}

function taskSpecEnvelopes(context: CompositionContext) {
if (shouldPropagateMessages(context)) {
if (context.isTrackingState) {
cy.task(
TASK_SPEC_ENVELOPES,
{ messages: context.specEnvelopes } satisfies ITaskSpecEnvelopes,
Expand All @@ -151,7 +146,7 @@ function taskTestCaseStarted(
context: CompositionContext,
testCaseStarted: messages.TestCaseStarted
) {
if (shouldPropagateMessages(context)) {
if (context.isTrackingState) {
cy.task(
TASK_TEST_CASE_STARTED,
testCaseStarted satisfies ITaskTestCaseStarted,
Expand All @@ -166,7 +161,7 @@ function taskTestCaseFinished(
context: CompositionContext,
testCasefinished: messages.TestCaseFinished
) {
if (shouldPropagateMessages(context)) {
if (context.isTrackingState) {
cy.task(
TASK_TEST_CASE_FINISHED,
testCasefinished satisfies ITaskTestCaseFinished,
Expand All @@ -181,7 +176,7 @@ function taskTestStepStarted(
context: CompositionContext,
testStepStarted: messages.TestStepStarted
) {
if (shouldPropagateMessages(context)) {
if (context.isTrackingState) {
cy.task(
TASK_TEST_STEP_STARTED,
testStepStarted satisfies ITaskTestStepStarted,
Expand All @@ -196,7 +191,7 @@ function taskTestStepFinished(
context: CompositionContext,
testStepfinished: messages.TestStepFinished
) {
if (shouldPropagateMessages(context)) {
if (context.isTrackingState) {
cy.task(
TASK_TEST_STEP_FINISHED,
testStepfinished satisfies ITaskTestStepFinished,
Expand Down Expand Up @@ -1006,8 +1001,7 @@ export default function createTests(
source: string,
gherkinDocument: messages.GherkinDocument,
pickles: messages.Pickle[],
prettyEnabled: boolean,
messagesEnabled: boolean,
isTrackingState: boolean,
omitFiltered: boolean,
stepDefinitionHints: {
stepDefinitions: string | string[];
Expand Down Expand Up @@ -1151,8 +1145,7 @@ export default function createTests(
specEnvelopes,
testFilter,
omitFiltered,
prettyEnabled,
messagesEnabled,
isTrackingState,
stepDefinitionHints,
};

Expand Down
67 changes: 18 additions & 49 deletions lib/plugin-event-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,9 @@ const createStateError = (stateHandler: string, currentState: State["state"]) =>
export async function beforeRunHandler(config: Cypress.PluginConfigOptions) {
debug("beforeRunHandler()");

if (!config.isTextTerminal) {
return;
}

const preprocessor = await resolve(config, config.env, "/");

if (!preprocessor.messages.enabled) {
if (!preprocessor.isTrackingState) {
return;
}

Expand Down Expand Up @@ -224,17 +220,9 @@ export async function beforeRunHandler(config: Cypress.PluginConfigOptions) {
export async function afterRunHandler(config: Cypress.PluginConfigOptions) {
debug("afterRunHandler()");

if (!config.isTextTerminal) {
return;
}

const preprocessor = await resolve(config, config.env, "/");

if (
!preprocessor.messages.enabled &&
!preprocessor.json.enabled &&
!preprocessor.html.enabled
) {
if (!preprocessor.isTrackingState) {
return;
}

Expand All @@ -249,7 +237,11 @@ export async function afterRunHandler(config: Cypress.PluginConfigOptions) {
return;
}

if (preprocessor.messages.enabled) {
if (
preprocessor.messages.enabled ||
preprocessor.json.enabled ||
preprocessor.html.enabled
) {
const testRunFinished: messages.Envelope = {
testRunFinished: {
/**
Expand Down Expand Up @@ -324,13 +316,13 @@ export async function beforeSpecHandler(
) {
debug("beforeSpecHandler()");

if (!config.isTextTerminal || !isFeature(spec)) {
if (!isFeature(spec)) {
return;
}

const preprocessor = await resolve(config, config.env, "/");

if (!preprocessor.messages.enabled && !preprocessor.pretty.enabled) {
if (!preprocessor.isTrackingState) {
return;
}

Expand Down Expand Up @@ -381,7 +373,7 @@ export async function afterSpecHandler(
) {
debug("afterSpecHandler()");

if (!config.isTextTerminal || !isFeature(spec)) {
if (!isFeature(spec)) {
return;
}

Expand All @@ -392,8 +384,13 @@ export async function afterSpecHandler(
preprocessor.messages.output
);

const reportingEnabled =
preprocessor.messages.enabled ||
preprocessor.json.enabled ||
preprocessor.html.enabled;

// `results` is undefined when running via `cypress open`.
if (preprocessor.messages.enabled && results) {
if (reportingEnabled && results) {
const wasRemainingSkipped = results.tests.some((test) =>
test.displayError?.match(HOOK_FAILURE_EXPR)
);
Expand Down Expand Up @@ -431,13 +428,9 @@ export async function afterScreenshotHandler(
) {
debug("afterScreenshotHandler()");

if (!config.isTextTerminal) {
return details;
}

const preprocessor = await resolve(config, config.env, "/");

if (!preprocessor.messages.enabled) {
if (!preprocessor.isTrackingState) {
return details;
}

Expand Down Expand Up @@ -478,10 +471,6 @@ export async function specEnvelopesHandler(
) {
debug("specEnvelopesHandler()");

if (!config.isTextTerminal) {
return true;
}

switch (state.state) {
case "before-spec":
break;
Expand Down Expand Up @@ -557,10 +546,6 @@ export function testCaseStartedHandler(
) {
debug("testCaseStartedHandler()");

if (!config.isTextTerminal) {
return true;
}

switch (state.state) {
case "received-envelopes":
case "test-finished":
Expand Down Expand Up @@ -591,10 +576,6 @@ export function testStepStartedHandler(
) {
debug("testStepStartedHandler()");

if (!config.isTextTerminal) {
return true;
}

switch (state.state) {
case "test-started":
case "step-finished":
Expand Down Expand Up @@ -639,10 +620,6 @@ export async function testStepFinishedHandler(
) {
debug("testStepFinishedHandler()");

if (!config.isTextTerminal) {
return true;
}

switch (state.state) {
case "step-started":
break;
Expand Down Expand Up @@ -766,10 +743,6 @@ export function testCaseFinishedHandler(
) {
debug("testCaseFinishedHandler()");

if (!config.isTextTerminal) {
return true;
}

switch (state.state) {
case "test-started":
case "step-finished":
Expand Down Expand Up @@ -799,13 +772,9 @@ export async function createStringAttachmentHandler(
) {
debug("createStringAttachmentHandler()");

if (!config.isTextTerminal) {
return true;
}

const preprocessor = await resolve(config, config.env, "/");

if (!preprocessor.messages.enabled) {
if (!preprocessor.isTrackingState) {
return true;
}

Expand Down
Loading

0 comments on commit fda0b90

Please sign in to comment.