Skip to content

Commit

Permalink
Merge pull request #961 from gemini-testing/TESTPLANE-107.snippet_scr
Browse files Browse the repository at this point in the history
fix: don't add error snippets on screenshot error
  • Loading branch information
KuznetsovRoman authored Jun 27, 2024
2 parents 145321b + a5d7842 commit f9239c9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/error-snippets/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { findRelevantStackFrame, resolveLocationWithStackFrame } from "./frames";
import { extractSourceMaps, resolveLocationWithSourceMap } from "./source-maps";
import { getSourceCodeFile, formatErrorSnippet } from "./utils";
import { getSourceCodeFile, formatErrorSnippet, shouldNotAddCodeSnippet } from "./utils";
import logger from "../utils/logger";
import type { ResolvedFrame, SufficientStackFrame, WithSnippetError } from "./types";

Expand All @@ -14,11 +14,11 @@ const stackFrameLocationResolver = async (stackFrame: SufficientStackFrame): Pro
};

export const extendWithCodeSnippet = async (err: WithSnippetError): Promise<WithSnippetError> => {
if (!err) {
return err;
}

try {
if (shouldNotAddCodeSnippet(err)) {
return err;
}

const relevantStackFrame = findRelevantStackFrame(err);

if (!relevantStackFrame) {
Expand Down
12 changes: 12 additions & 0 deletions src/error-snippets/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import fs from "fs-extra";
import { codeFrameColumns } from "@babel/code-frame";
import { getErrorTitle } from "../browser/stacktrace/utils";
import { SNIPPET_LINES_ABOVE, SNIPPET_LINES_BELOW, SOURCE_MAP_URL_COMMENT } from "./constants";
import { AssertViewError } from "../browser/commands/assert-view/errors/assert-view-error";
import { BaseStateError } from "../browser/commands/assert-view/errors/base-state-error";

interface FormatFileNameHeaderOpts {
line: number;
Expand All @@ -17,6 +19,16 @@ interface FormatErrorSnippetOpts {
location: { line: number; column: number };
}

export const shouldNotAddCodeSnippet = (err: Error): boolean => {
if (!err) {
return true;
}

const isScreenshotError = [AssertViewError, BaseStateError].some(errorClass => err instanceof errorClass);

return isScreenshotError;
};

export const softFileURLToPath = (fileName: string): string => {
if (!fileName.startsWith("file://")) {
return fileName;
Expand Down
18 changes: 18 additions & 0 deletions test/src/error-snippets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import fs from "fs-extra";
import sinon, { type SinonStub } from "sinon";
import proxyquire from "proxyquire";

import { AssertViewError } from "../../../src/browser/commands/assert-view/errors/assert-view-error";
import { BaseStateError } from "../../../src/browser/commands/assert-view/errors/base-state-error";

describe("error-snippets", () => {
const cloneError = (err: Error): Error => {
const newError = err;
Expand Down Expand Up @@ -41,6 +44,21 @@ describe("error-snippets", () => {

afterEach(() => sandbox.restore());

[AssertViewError, BaseStateError].forEach(ErrorClass => {
it(`should not add error snippet for ${ErrorClass.name}`, async () => {
const error = new ErrorClass(
"foo",
{ path: "/some/path", size: { width: 800, height: 600 } },
{ path: "/some/path", relativePath: "../path", size: { width: 800, height: 600 } },
);
const originalStack = error.stack;

await extendWithCodeSnippet(error);

assert.equal(error.stack, originalStack);
});
});

it("should not modify error, if it is falsy", async () => {
await assert.eventually.isUndefined(extendWithCodeSnippet());
await assert.eventually.isNull(extendWithCodeSnippet(null));
Expand Down

0 comments on commit f9239c9

Please sign in to comment.