screenshotter implemented

main
Inga 🏳‍🌈 2 years ago
parent 016668ce83
commit b01c4e9032
  1. 808
      package-lock.json
  2. 4
      package.json
  3. 3
      src/app.module.ts
  4. BIN
      src/screenshotter/__image_snapshots__/screenshotter-service-spec-ts-screenshotter-service-should-return-hello-world-screenshot-1-snap.png
  5. 26
      src/screenshotter/screenshotter.service.spec.ts
  6. 26
      src/screenshotter/screenshotter.service.ts
  7. 3
      src/setup.jest.ts
  8. 2
      tsconfig.build.json

808
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -24,6 +24,7 @@
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"puppeteer": "^15.3.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0"
@ -34,6 +35,7 @@
"@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.13",
"@types/jest": "28.1.4",
"@types/jest-image-snapshot": "^5.1.0",
"@types/node": "^16.0.0",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^5.0.0",
@ -42,6 +44,7 @@
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "28.1.2",
"jest-image-snapshot": "^5.1.0",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
@ -65,6 +68,7 @@
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"setupFilesAfterEnv": ["<rootDir>/setup.jest.ts"],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}

@ -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 {}

@ -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…
Cancel
Save