Skip to content

Commit

Permalink
Merge pull request #1 from diplodoc-platform/tests
Browse files Browse the repository at this point in the history
infra: add snapshot tests
  • Loading branch information
yndx-birman authored Nov 8, 2023
2 parents 24b3d27 + 9af3f42 commit f06e64c
Show file tree
Hide file tree
Showing 24 changed files with 7,584 additions and 4,574 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
22 changes: 22 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Tests

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

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: sudo apt-get update
- run: sudo apt-get install ghostscript
- run: sudo apt-get install graphicsmagick
- uses: actions/setup-node@v3
with:
node-version: 18
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- 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
3 changes: 1 addition & 2 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
registry=https://registry.npmjs.org
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = true
registry=https://registry.npmjs.org
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 f06e64c

Please sign in to comment.