Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
BugDiver committed Mar 4, 2020
1 parent cdf37ce commit 388a287
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"uuid": "^7.0.1"
},
"devDependencies": {
"@types/fs-extra": "^8.0.1",
"@types/jest": "^24.9.1",
"@types/klaw-sync": "^6.0.0",
"@types/uuid": "^7.0.0",
Expand Down
3 changes: 1 addition & 2 deletions src/screenshot/Screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ export class Screenshot {

public static async capture(): Promise<string> {
try {

if (this.customScreenshotWriter != null) {
if (Util.isAsync(this.customScreenshotWriter)) {
return await this.customScreenshotWriter();
} else {
return Promise.resolve(this.customScreenshotWriter());
}
} else if (this.customScreenshotWriter != null) {
} else if (this.customScreenGrabber != null) {
let data: Uint8Array;
if (Util.isAsync(this.customScreenshotWriter)) {
data = await this.customScreenGrabber();
Expand Down
2 changes: 1 addition & 1 deletion tests/processors/ExecutionEndingProcessorTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('ExecutionEndingProcessor', () => {
let pRes = res.executionResult as gauge.messages.ProtoExecutionResult;
expect(pRes.failed).toBe(true);
expect(pRes.errorMessage).toBe("failed");
expect(pRes.screenShot.length).toBe(10);
expect(pRes.failureScreenshotFile).toBeTruthy();
})
})

Expand Down
10 changes: 5 additions & 5 deletions tests/processors/StepExecutionProcessorTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('StepExecutionProcessor', () => {
})

describe('.process', () => {
it('should process stepex exection request when step is unimplemeted', async () => {
it('should process step execution request when step is unimplemented', async () => {
let message = new gauge.messages.Message({
messageId: 0,
messageType: gauge.messages.Message.MessageType.ExecuteStep,
Expand All @@ -32,7 +32,7 @@ describe('StepExecutionProcessor', () => {
expect(result.errorMessage).toBe('Step Implementation not found');
})

it('should process stepex exection request when there is param lenght mismatch', async () => {
it('should process step execution request when there is param lenght mismatch', async () => {
let capture = jest.spyOn(Screenshot, "capture");
registry.isImplemented = jest.fn().mockReturnValue(true);
registry.get = jest.fn().mockReturnValue(new StepRegistryEntry('hello', 'hello', 'StepImpl.ts', (a: any) => { }))
Expand All @@ -48,11 +48,11 @@ describe('StepExecutionProcessor', () => {
let response = resMess.executionStatusResponse as gauge.messages.ExecutionStatusResponse;
let result = response.executionResult as gauge.messages.ProtoExecutionResult;
expect(result.failed).toBe(true);
expect(result.errorMessage).toBe('Argument length mismatch for `hello`. Actual Count: [1], Exepected Count: [0]');
expect(result.errorMessage).toBe('Argument length mismatch for `hello`. Actual Count: [1], Expected Count: [0]');
expect(capture).toBeCalled();
})

it('should process stepex exection request', async () => {
it('should process step execution request', async () => {
registry.isImplemented = jest.fn().mockReturnValue(true);
registry.get = jest.fn().mockReturnValue(new StepRegistryEntry('hello <world> to <table>',
'hello {} to {}',
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('StepExecutionProcessor', () => {
expect(result.errorMessage).toBe('');
})

it('should process stepex exection request when step is recoverable', async () => {
it('should process step execution request when step is recoverable', async () => {
let capture = jest.spyOn(Screenshot, "capture");
process.env.screenshot_on_failure = 'false';

Expand Down
40 changes: 27 additions & 13 deletions tests/screenshot/ScreenshotTest.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
import { existsSync, unlinkSync } from 'fs';
import { Screenshot } from '../../src/screenshot/Screenshot';
import { Util } from '../../src/utils/Util';

describe('Screenshot', () => {

let screenshotFile: string;

describe('.capture', () => {

afterEach(() => {
if (existsSync(screenshotFile)) {
unlinkSync(screenshotFile);
}
})

it('should capture screen shot and return the buffer', async () => {
process.env.gauge_screenshots_dir = "";
Util.readFileBuffer = jest.fn().mockReturnValue(new ArrayBuffer(10));
Util.spawn = jest.fn();
let data = await Screenshot.capture();
expect(data.length).toBe(10);
screenshotFile = await Screenshot.capture();
expect(screenshotFile.endsWith('.png')).toBe(true);
})

it('should return empty buffer if fails to capture screenshot', async () => {
Util.readFileBuffer = jest.fn().mockReturnValue(new ArrayBuffer(10));
Util.spawn = jest.fn().mockImplementation(() => { throw new Error('faild to spawn') });
Util.spawn = jest.fn().mockImplementation(() => { throw new Error('failed to spawn') });
console.log = jest.fn();
let data = await Screenshot.capture();
expect(data.length).toBe(0);
let file = await Screenshot.capture();
expect(file).toBe("");
})

it('should capture screenshot using customscreengrabber', async () => {
Screenshot.setCustomScreenGrabber(() => { return new Uint8Array(new ArrayBuffer(10))})
let data = await Screenshot.capture();
expect(data.length).toBe(10);
it('should capture screenshot using custom screen grabber and write data to file', async () => {
process.env.gauge_screenshots_dir = process.cwd();
Screenshot.setCustomScreenGrabber(() => { return new Uint8Array(new ArrayBuffer(10)) })
screenshotFile = await Screenshot.capture();
expect(screenshotFile.endsWith('png')).toBeTruthy();
})

it('should capture screenshot using async customscreengrabber', async () => {
Screenshot.setCustomScreenGrabber(async () => { return new Uint8Array(new ArrayBuffer(5))})
let data = await Screenshot.capture();
expect(data.length).toBe(5);
it('should capture screenshot using async custom screen grabber and write data to file', async () => {
process.env.gauge_screenshots_dir = "";
Screenshot.setCustomScreenGrabber(async () => { return new Uint8Array(new ArrayBuffer(5)) })
screenshotFile = await Screenshot.capture();
expect(screenshotFile.endsWith('png')).toBeTruthy();
})
})
})

0 comments on commit 388a287

Please sign in to comment.