Skip to content

Commit 76f816f

Browse files
authored
Merge pull request #55 from Visual-Regression-Tracker/64-build-start
Build returned after start
2 parents 9c3c040 + ec032fe commit 76f816f

File tree

4 files changed

+69
-63
lines changed

4 files changed

+69
-63
lines changed

lib/playwrightVisualRegressionTracker.spec.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import {
1111
import { mocked } from "ts-jest/utils";
1212
import { MaybeMocked } from "ts-jest/dist/utils/testing";
1313

14+
jest.mock("@visual-regression-tracker/sdk-js");
15+
1416
let browserType = chromium;
1517
let browser: Browser;
1618
let context: BrowserContext;
1719
let page: Page;
18-
let vrt: PlaywrightVisualRegressionTracker;
20+
let playwrightVrt: PlaywrightVisualRegressionTracker;
1921

2022
const config: Config = {
2123
apiUrl: "http://localhost:4200",
@@ -34,41 +36,40 @@ beforeAll(async () => {
3436
},
3537
});
3638
page = await context.newPage();
37-
vrt = new PlaywrightVisualRegressionTracker(browserType, config);
3839
});
3940

4041
afterAll(async () => {
4142
await browser.close();
4243
});
4344

4445
describe("playwright", () => {
45-
it("start", async () => {
46-
const startMock = jest.fn();
47-
VisualRegressionTracker.prototype.start = startMock;
46+
beforeEach(() => {
47+
playwrightVrt = new PlaywrightVisualRegressionTracker(browserType, config);
48+
});
49+
50+
it("constructor", async () => {
51+
expect(playwrightVrt["browser"]).toBe(browserType.name());
52+
expect(VisualRegressionTracker).toHaveBeenCalledWith(config);
53+
});
4854

49-
await vrt.start();
55+
it("start", async () => {
56+
await playwrightVrt.start();
5057

51-
expect(startMock).toHaveBeenCalled();
58+
expect(VisualRegressionTracker.prototype.start).toHaveBeenCalled();
5259
});
5360

5461
it("stop", async () => {
55-
const stopMock = jest.fn();
56-
VisualRegressionTracker.prototype.stop = stopMock;
62+
await playwrightVrt.stop();
5763

58-
await vrt.stop();
59-
60-
expect(stopMock).toHaveBeenCalled();
64+
expect(VisualRegressionTracker.prototype.stop).toHaveBeenCalled();
6165
});
6266

6367
describe("track", () => {
64-
let trackMock: jest.Mock<any, any>;
6568
let pageMocked: MaybeMocked<Page>;
6669
const screenshot: Buffer = Buffer.from("image mocked");
6770

6871
beforeEach(() => {
69-
vrt["vrt"]["isStarted"] = jest.fn().mockReturnValueOnce(true);
70-
trackMock = jest.fn();
71-
VisualRegressionTracker.prototype.track = trackMock;
72+
playwrightVrt["vrt"]["isStarted"] = jest.fn().mockReturnValueOnce(true);
7273
pageMocked = mocked(page);
7374
pageMocked.screenshot = jest.fn().mockResolvedValueOnce(screenshot);
7475
});
@@ -102,12 +103,12 @@ describe("playwright", () => {
102103
},
103104
};
104105

105-
await vrt.trackPage(page, imageName, trackOptions);
106+
await playwrightVrt.trackPage(page, imageName, trackOptions);
106107

107108
expect(pageMocked.screenshot).toHaveBeenCalledWith(
108109
trackOptions.screenshotOptions
109110
);
110-
expect(trackMock).toHaveBeenCalledWith({
111+
expect(VisualRegressionTracker.prototype.track).toHaveBeenCalledWith({
111112
name: imageName,
112113
imageBase64: screenshot.toString("base64"),
113114
browser: browserType.name(),
@@ -123,10 +124,10 @@ describe("playwright", () => {
123124
const imageName = "test name";
124125
pageMocked.viewportSize = jest.fn().mockReturnValueOnce(null);
125126

126-
await vrt.trackPage(page, imageName);
127+
await playwrightVrt.trackPage(page, imageName);
127128

128129
expect(pageMocked.screenshot).toHaveBeenCalledWith(undefined);
129-
expect(trackMock).toHaveBeenCalledWith({
130+
expect(VisualRegressionTracker.prototype.track).toHaveBeenCalledWith({
130131
name: imageName,
131132
imageBase64: screenshot.toString("base64"),
132133
browser: browserType.name(),
@@ -168,12 +169,16 @@ describe("playwright", () => {
168169
.fn()
169170
.mockResolvedValueOnce(screenshot);
170171

171-
await vrt.trackElementHandle(elementHandle, imageName, trackOptions);
172+
await playwrightVrt.trackElementHandle(
173+
elementHandle,
174+
imageName,
175+
trackOptions
176+
);
172177

173178
expect(elementHandleMocked!.screenshot).toHaveBeenCalledWith(
174179
trackOptions.screenshotOptions
175180
);
176-
expect(trackMock).toHaveBeenCalledWith({
181+
expect(VisualRegressionTracker.prototype.track).toHaveBeenCalledWith({
177182
name: imageName,
178183
imageBase64: screenshot.toString("base64"),
179184
browser: browserType.name(),
@@ -194,10 +199,10 @@ describe("playwright", () => {
194199
.fn()
195200
.mockResolvedValueOnce(screenshot);
196201

197-
await vrt.trackElementHandle(elementHandle, imageName);
202+
await playwrightVrt.trackElementHandle(elementHandle, imageName);
198203

199204
expect(elementHandleMocked!.screenshot).toHaveBeenCalledWith(undefined);
200-
expect(trackMock).toHaveBeenCalledWith({
205+
expect(VisualRegressionTracker.prototype.track).toHaveBeenCalledWith({
201206
name: imageName,
202207
imageBase64: screenshot.toString("base64"),
203208
browser: browserType.name(),
@@ -214,7 +219,7 @@ describe("playwright", () => {
214219
const elementHandle = await page.$("#test");
215220

216221
await expect(
217-
vrt.trackElementHandle(elementHandle, imageName)
222+
playwrightVrt.trackElementHandle(elementHandle, imageName)
218223
).rejects.toThrowError(new Error("ElementHandle is null"));
219224
});
220225
});

lib/playwrightVisualRegressionTracker.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
VisualRegressionTracker,
33
Config,
4+
BuildResponse,
45
} from "@visual-regression-tracker/sdk-js";
56
import { Page, Browser, BrowserType, ElementHandle } from "playwright";
67
import {
@@ -10,14 +11,14 @@ import {
1011

1112
export class PlaywrightVisualRegressionTracker {
1213
private vrt: VisualRegressionTracker;
13-
private browser: BrowserType<Browser>;
14+
private browser: string;
1415

15-
constructor(browser: BrowserType<Browser>, config?: Config) {
16+
constructor(browserType: BrowserType<Browser>, config?: Config) {
1617
this.vrt = new VisualRegressionTracker(config);
17-
this.browser = browser;
18+
this.browser = browserType.name();
1819
}
1920

20-
async start() {
21+
async start(): Promise<BuildResponse> {
2122
return this.vrt.start();
2223
}
2324

@@ -32,7 +33,7 @@ export class PlaywrightVisualRegressionTracker {
3233
imageBase64: (await page.screenshot(options?.screenshotOptions)).toString(
3334
"base64"
3435
),
35-
browser: this.browser.name(),
36+
browser: this.browser,
3637
viewport: viewportSize
3738
? `${viewportSize.width}x${viewportSize.height}`
3839
: undefined,
@@ -56,7 +57,7 @@ export class PlaywrightVisualRegressionTracker {
5657
imageBase64: (
5758
await elementHandle.screenshot(options?.screenshotOptions)
5859
).toString("base64"),
59-
browser: this.browser.name(),
60+
browser: this.browser,
6061
viewport: options?.agent?.viewport,
6162
os: options?.agent?.os,
6263
device: options?.agent?.device,

package-lock.json

Lines changed: 25 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"scripts": {
8-
"test": "jest",
9-
"test:cov": "jest --collectCoverage",
8+
"test": "jest --detectOpenHandles --forceExit",
9+
"test:cov": "jest --collectCoverage --detectOpenHandles --forceExit",
1010
"build": "./node_modules/.bin/tsc"
1111
},
1212
"repository": {
@@ -25,14 +25,14 @@
2525
},
2626
"homepage": "https://github.com/Visual-Regression-Tracker/agent-playwright#readme",
2727
"dependencies": {
28-
"@visual-regression-tracker/sdk-js": "^4.6.2"
28+
"@visual-regression-tracker/sdk-js": "^4.6.3"
2929
},
3030
"devDependencies": {
31-
"@types/jest": "^26.0.16",
32-
"@types/node": "^14.14.10",
31+
"@types/jest": "^26.0.19",
32+
"@types/node": "^14.14.16",
3333
"jest": "^26.6.2",
34-
"playwright": "^1.6.2",
34+
"playwright": "^1.7.1",
3535
"ts-jest": "^26.4.3",
36-
"typescript": "^4.1.2"
36+
"typescript": "^4.1.3"
3737
}
3838
}

0 commit comments

Comments
 (0)