Skip to content

Commit

Permalink
infra: add snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yndx-birman committed Oct 27, 2023
1 parent 24b3d27 commit 23f9c87
Show file tree
Hide file tree
Showing 24 changed files with 1,878 additions and 751 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
build
tests
integration-output
34 changes: 34 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Tests

on:
push:
branches: [master]
pull_request:
branches: ['**']

jobs:
test:
name: Node v${{ matrix.node-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
node-version: [18.x]
steps:
- name: Checkout
uses: actions/checkout@v2
- run: sudo apt-get update
- run: sudo apt-get install ghostscript
- run: sudo apt-get install graphicsmagick
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
os: ${{ matrix.os }}
cache: 'npm'
- name: Install packages for project
run: npm ci
- name: Run tests
run: |
npm run test
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ coverage
.DS_Store

# integration
integration-output
integration-output

# tests
__tests__/stubs
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
registry=https://registry.npmjs.org
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = true
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = true
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# Build
build
integration-output

#node
node_modules
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,22 @@ Run the `@diplodoc/docs2pdf` command. This will create PDF files right next to t

```
npx -- @diplodoc/docs2pdf@latest -i ./docs-output
```

## Development

### Prerequisites

* node >= 18.x
* graphicsmagick
* ghostscript

#### Don't have graphicsmagick and ghostscript yet?

Follow [this](https://github.com/yakovmeister/pdf2image/blob/HEAD/docs/gm-installation.md) guide to install the required dependencies.

### Run test

```
npm run test
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {dirname, join, resolve} from 'path';

import {toMatchImageSnapshot} from 'jest-image-snapshot';
import {fromPath} from 'pdf2pic';
import puppeteer from 'puppeteer';
import {Browser} from 'puppeteer-core';

import {generatePdf} from '../src';

expect.extend({toMatchImageSnapshot});

describe('integration', () => {
let browser: Browser;

const pdfFile = resolve(__dirname, '../integration-output/en/single-page.pdf');
const options = {
density: 100,
format: 'png',
width: 794,
height: 1123,
};
const convertPdf2Pic = fromPath(pdfFile, options);
const numberOfPages = 7;

beforeAll(async () => {
// @ts-ignore
browser = await puppeteer.launch({headless: true});

await generatePdf({
singlePagePath: join(dirname(pdfFile), 'single-page.json'),
browser,
});
});

it('works', async () => {
for (let currentPage = 1; currentPage <= numberOfPages; currentPage++) {
const {buffer: expectedImage} = await convertPdf2Pic(currentPage, {
responseType: 'buffer',
});

expect(expectedImage).toMatchImageSnapshot({
failureThreshold: 0.1,
failureThresholdType: 'percent',
});
}
});

afterAll(async () => {
if (!browser) {
return;
}

await browser.close();
});
});
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/?(*.)+(test).[tj]s?(x)'],
testTimeout: 1000000,
};
Loading

0 comments on commit 23f9c87

Please sign in to comment.