Skip to content

Commit

Permalink
fix: Use mock in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dgusakov committed Dec 27, 2023
1 parent 8fec062 commit b843c19
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ exports[`utils should merge findings 1`] = `
"protocol": "ethereum",
"severity": 2,
"source": {},
"timestamp": 2023-12-31T00:00:00.000Z,
"type": 3,
"uniqueKey": "",
},
Expand Down Expand Up @@ -93,6 +94,7 @@ exports[`utils should merge findings 1`] = `
"protocol": "ethereum",
"severity": 2,
"source": {},
"timestamp": 2023-12-31T00:00:00.000Z,
"type": 3,
"uniqueKey": "",
},
Expand Down Expand Up @@ -140,6 +142,7 @@ exports[`utils should merge findings 1`] = `
"protocol": "ethereum",
"severity": 2,
"source": {},
"timestamp": 2023-12-31T00:00:00.000Z,
"type": 3,
"uniqueKey": "",
},
Expand Down
16 changes: 14 additions & 2 deletions ethereum-governance/src/common/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { Finding, FindingSeverity, FindingType } from "forta-agent";
import { mergeFindings, removeTimestamp } from "./utils";
import { mergeFindings } from "./utils";

describe("utils", () => {
let timeSpy: jest.SpyInstance;

beforeAll(() => {
timeSpy = jest.spyOn(Date, "now");
timeSpy.mockImplementation(() => new Date("2023-12-31"));
});

afterAll(() => {
jest.resetAllMocks();
});


it("should merge findings", () => {
const ids = ["SOME-CUTE-ALERT", "SOME-FAKE-ALERT", "SOME-SOSO-ALERT"];
const wordsList = ["🌷🌷🌷", "🍯🍯🍯", "🌸🌸🌸"];
Expand All @@ -17,7 +29,7 @@ describe("utils", () => {
});

const reducedFindings = mergeFindings(findings);
expect(removeTimestamp(reducedFindings)).toMatchSnapshot();
expect(reducedFindings).toMatchSnapshot();
expect(reducedFindings.length).toBe(3);
});
});
11 changes: 0 additions & 11 deletions ethereum-governance/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,3 @@ export function eventSig(abi: string) {
argsRaw.map((arg) => args.push(arg.trim().split(" ")[0]));
return `${name}(${args.join(",")})`;
}

/**
* Remove timestamp property from findings
*/

export function removeTimestamp(findings: Array<object>): Array<object> {
findings.map((item: any) => {
delete item.timestamp;
});
return findings;
}
3 changes: 3 additions & 0 deletions ethereum-steth/src/common/__snapshots__/utils.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ exports[`utils should merge findings 1`] = `
"protocol": "ethereum",
"severity": 2,
"source": {},
"timestamp": 2023-12-31T00:00:00.000Z,
"type": 3,
"uniqueKey": "",
},
Expand Down Expand Up @@ -93,6 +94,7 @@ exports[`utils should merge findings 1`] = `
"protocol": "ethereum",
"severity": 2,
"source": {},
"timestamp": 2023-12-31T00:00:00.000Z,
"type": 3,
"uniqueKey": "",
},
Expand Down Expand Up @@ -140,6 +142,7 @@ exports[`utils should merge findings 1`] = `
"protocol": "ethereum",
"severity": 2,
"source": {},
"timestamp": 2023-12-31T00:00:00.000Z,
"type": 3,
"uniqueKey": "",
},
Expand Down
15 changes: 13 additions & 2 deletions ethereum-steth/src/common/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { Finding, FindingSeverity, FindingType } from "forta-agent";
import { mergeFindings, removeTimestamp } from "./utils";
import { mergeFindings } from "./utils";

describe("utils", () => {
let timeSpy: jest.SpyInstance;

beforeAll(() => {
timeSpy = jest.spyOn(Date, "now");
timeSpy.mockImplementation(() => new Date("2023-12-31"));
});

afterAll(() => {
jest.resetAllMocks();
});

it("should merge findings", () => {
const ids = ["SOME-CUTE-ALERT", "SOME-FAKE-ALERT", "SOME-SOSO-ALERT"];
const wordsList = ["🌷🌷🌷", "🍯🍯🍯", "🌸🌸🌸"];
Expand All @@ -17,7 +28,7 @@ describe("utils", () => {
});

const reducedFindings = mergeFindings(findings);
expect(removeTimestamp(reducedFindings)).toMatchSnapshot();
expect(reducedFindings).toMatchSnapshot();
expect(reducedFindings.length).toBe(3);
});
});
11 changes: 0 additions & 11 deletions ethereum-steth/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,3 @@ export function eventSig(abi: string) {
argsRaw.map((arg) => args.push(arg.trim().split(" ")[0]));
return `${name}(${args.join(",")})`;
}

/**
* Remove timestamp property from findings
*/

export function removeTimestamp(findings: Array<object>): Array<object> {
findings.map((item: any) => {
delete item.timestamp;
});
return findings;
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ exports[`utils should merge findings 1`] = `
"protocol": "ethereum",
"severity": 2,
"source": {},
"timestamp": 2023-12-31T00:00:00.000Z,
"type": 3,
"uniqueKey": "",
},
Expand Down Expand Up @@ -93,6 +94,7 @@ exports[`utils should merge findings 1`] = `
"protocol": "ethereum",
"severity": 2,
"source": {},
"timestamp": 2023-12-31T00:00:00.000Z,
"type": 3,
"uniqueKey": "",
},
Expand Down Expand Up @@ -140,6 +142,7 @@ exports[`utils should merge findings 1`] = `
"protocol": "ethereum",
"severity": 2,
"source": {},
"timestamp": 2023-12-31T00:00:00.000Z,
"type": 3,
"uniqueKey": "",
},
Expand Down
15 changes: 13 additions & 2 deletions ethereum-validators-set/src/common/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { Finding, FindingSeverity, FindingType } from "forta-agent";
import { mergeFindings, removeTimestamp } from "./utils";
import { mergeFindings } from "./utils";

describe("utils", () => {
let timeSpy: jest.SpyInstance;

beforeAll(() => {
timeSpy = jest.spyOn(Date, "now");
timeSpy.mockImplementation(() => new Date("2023-12-31"));
});

afterAll(() => {
jest.resetAllMocks();
});

it("should merge findings", () => {
const ids = ["SOME-CUTE-ALERT", "SOME-FAKE-ALERT", "SOME-SOSO-ALERT"];
const wordsList = ["🌷🌷🌷", "🍯🍯🍯", "🌸🌸🌸"];
Expand All @@ -17,7 +28,7 @@ describe("utils", () => {
});

const reducedFindings = mergeFindings(findings);
expect(removeTimestamp(reducedFindings)).toMatchSnapshot();
expect(reducedFindings).toMatchSnapshot();
expect(reducedFindings.length).toBe(3);
});
});
11 changes: 0 additions & 11 deletions ethereum-validators-set/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,3 @@ export function eventSig(abi: string) {
argsRaw.map((arg) => args.push(arg.trim().split(" ")[0]));
return `${name}(${args.join(",")})`;
}

/**
* Remove timestamp property from findings
*/

export function removeTimestamp(findings: Array<object>): Array<object> {
findings.map((item: any) => {
delete item.timestamp;
});
return findings;
}

0 comments on commit b843c19

Please sign in to comment.