parent
016668ce83
commit
b01c4e9032
File diff suppressed because it is too large
Load Diff
@ -1,10 +1,11 @@ |
||||
import { Module } from '@nestjs/common'; |
||||
import { HelloWorldController } from './hello-world/controller'; |
||||
import { HelloWorldService } from './hello-world/service'; |
||||
import { ScreenshotterService } from './screenshotter/screenshotter.service'; |
||||
|
||||
@Module({ |
||||
imports: [], |
||||
controllers: [HelloWorldController], |
||||
providers: [HelloWorldService], |
||||
providers: [HelloWorldService, ScreenshotterService], |
||||
}) |
||||
export class AppModule {} |
||||
|
After Width: | Height: | Size: 6.2 KiB |
@ -0,0 +1,26 @@ |
||||
import { Test, TestingModule } from '@nestjs/testing'; |
||||
import { ScreenshotterService } from './screenshotter.service'; |
||||
|
||||
describe('ScreenshotterService', () => { |
||||
let service: ScreenshotterService; |
||||
|
||||
beforeEach(async () => { |
||||
const module: TestingModule = await Test.createTestingModule({ |
||||
providers: [ScreenshotterService], |
||||
}).compile(); |
||||
|
||||
service = module.get<ScreenshotterService>(ScreenshotterService); |
||||
}); |
||||
|
||||
it('should be defined', () => { |
||||
expect(service).toBeDefined(); |
||||
}); |
||||
|
||||
it('should return "Hello World!" screenshot', async () => { |
||||
const image = await service.takeScreenshot({ |
||||
imageType: 'png', |
||||
url: new URL('data:text/html,%3Ch1%3EHello%2C%20World%21%3C%2Fh1%3E'), |
||||
}) |
||||
expect(image).toMatchImageSnapshot() |
||||
}); |
||||
}); |
@ -0,0 +1,26 @@ |
||||
import { Injectable } from '@nestjs/common'; |
||||
import puppeteer from 'puppeteer' |
||||
|
||||
type TakeScreenshotOptions = { |
||||
imageType: 'jpeg' | 'png' |
||||
url: URL |
||||
} |
||||
|
||||
@Injectable() |
||||
export class ScreenshotterService { |
||||
async takeScreenshot(options: TakeScreenshotOptions): Promise<Buffer> { |
||||
const browser = await puppeteer.launch() |
||||
try { |
||||
const page = await browser.newPage() |
||||
await page.goto(options.url.toString()) |
||||
const image = await page.screenshot({ |
||||
type: options.imageType, |
||||
encoding: 'binary' |
||||
}) as Buffer |
||||
|
||||
return image |
||||
} finally { |
||||
browser.close() |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,3 @@ |
||||
const { toMatchImageSnapshot } = require('jest-image-snapshot'); |
||||
|
||||
expect.extend({ toMatchImageSnapshot }); |
@ -1,4 +1,4 @@ |
||||
{ |
||||
"extends": "./tsconfig.json", |
||||
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"] |
||||
"exclude": ["node_modules", "test", "dist", "**/*spec.ts", "**/*jest.ts"] |
||||
} |
||||
|
Loading…
Reference in new issue