Skip to content

Commit

Permalink
fix: check image size betweenness, add argument tests (#74)
Browse files Browse the repository at this point in the history
* fix: tests to care windows/others display resolution diff

* fix: testing import cjs to esm

* fix: check image size betweenness, add argument tests

* fix: move call fn to test's lib

---------

Co-authored-by: Alvaro Cabrera Durán <[email protected]>
  • Loading branch information
tomoh1r and pateketrueke authored Aug 31, 2024
1 parent 7834d1c commit 14d44dd
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 17 deletions.
56 changes: 56 additions & 0 deletions e2e/cases/cli.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { existsSync } from 'node:fs';
import { join } from 'node:path';

import sizeOf from 'image-size';
import { rimrafSync } from 'rimraf';

import {
getBaseUrl, fixedSize, fixedFile, callTC,
} from '../lib';

fixture('Testing cli --take-snapshot').page(getBaseUrl());

test('should care arg without name', async t => {
const basePath = join('e2e', 'screens', 'Testing_cli', 'assert__it');
rimrafSync(basePath);

await callTC('e2e/file/cases/takesnapshot.test.js', ['--take-snapshot', 'base', '-t', 'takesnapshot']);

await t.expect(existsSync(join(basePath, fixedFile('base.png')))).ok();
});

test('should care arg with name', async t => {
const basePath = join('e2e', 'screens', 'Testing_cli', 'assert__it');
rimrafSync(basePath);

const fpath = 'e2e/file/cases/takesnapshot.test.js';
await callTC(fpath, ['--take-snapshot', 'base', '-t', 'takesnapshot']);
await callTC(fpath, ['--take-snapshot', 'actual', '-t', 'takesnapshot']);
await callTC(fpath, ['--take-snapshot', 'foo', '-t', 'takesnapshot']);

await t.expect(existsSync(join(basePath, fixedFile('base.png')))).ok();
await t.expect(existsSync(join(basePath, fixedFile('actual.png')))).ok();
await t.expect(existsSync(join(basePath, fixedFile('foo.png')))).ok();
await t.expect(existsSync(join(basePath, fixedFile('bar.png')))).notOk();
});

fixture('Testing cli --full-page').page(getBaseUrl());

test('should care arg', async t => {
const basePath = join('e2e', 'screens', 'Testing_cli');
rimrafSync(basePath);

const fpath = 'e2e/file/cases/takesnapshot.test.js';
await callTC(fpath, ['--take-snapshot', '--full-page', '-t', 'fullpage']);
await callTC(fpath, ['--take-snapshot', '-t', 'nofullpage']);

const pngFull = sizeOf(join(basePath, 'assert__it__with__fullpage', fixedFile('base.png')));
await t.expect(pngFull.width).eql(fixedSize(1290));
await t.expect(pngFull.height).gte(fixedSize(1356));

const pngNoFull = sizeOf(join(basePath, 'assert__it__without__fullpage', fixedFile('base.png')));
await t.expect(pngNoFull.width).gte(fixedSize(623));
await t.expect(pngNoFull.width).lte(fixedSize(640));
await t.expect(pngNoFull.height).gte(fixedSize(463));
await t.expect(pngNoFull.height).lte(fixedSize(480));
});
22 changes: 5 additions & 17 deletions e2e/cases/setting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,12 @@ import { join } from 'node:path';
import sizeOf from 'image-size';
import { rimrafSync } from 'rimraf';

import { fixedSize, fixedFile } from '../lib';
import { takeSnapshot } from '../../lib';

fixture('Testing set FULL_PAGE')
.page(`${process.env.BASE_URL}/`);

// this enforces a particular DPI to allow
// tests to behave in a deterministic way!!
process.env.WINDOW_DPI = process.env.WINDOW_DPI || "2";

function fixedSize(value) {
return value * process.env.WINDOW_DPI;
}

function fixedFile(name) {
if (process.env.WINDOW_DPI > 1) {
return name.replace('.', `@${process.env.WINDOW_DPI}.`);
}
return name;
}

test('should care environment variables', async t => {
process.env.TAKE_SNAPSHOT = "1";

Expand All @@ -42,8 +28,10 @@ test('should care environment variables', async t => {
await t.expect(pngWithEnv.height).gte(fixedSize(1356));

const pngWithOutEnv = sizeOf(join(basePath, 'after__del__env', fixedFile('base.png')));
await t.expect(pngWithOutEnv.width).gte(fixedSize(625));
await t.expect(pngWithOutEnv.height).gte(fixedSize(465));
await t.expect(pngWithOutEnv.width).gte(fixedSize(623));
await t.expect(pngWithOutEnv.width).lte(fixedSize(640));
await t.expect(pngWithOutEnv.height).gte(fixedSize(463));
await t.expect(pngWithOutEnv.height).lte(fixedSize(480));
});

fixture('Testing set TAKE_SNAPSHOT')
Expand Down
18 changes: 18 additions & 0 deletions e2e/file/cases/takesnapshot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getBaseUrl } from '../../lib';
import { takeSnapshot } from '../../../lib';

fixture('Testing cli').page(getBaseUrl());

test('takesnapshot', async t => {
await takeSnapshot(t, 'assert_it');
});

test('fullpage', async t => {
await t.resizeWindow(640, 480);
await takeSnapshot(t, 'assert_it_with_fullpage');
});

test('nofullpage', async t => {
await t.resizeWindow(640, 480);
await takeSnapshot(t, 'assert_it_without_fullpage');
});
43 changes: 43 additions & 0 deletions e2e/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { spawn } from 'node:child_process';
import process from 'node:process';

// this enforces a particular DPI to allow
// tests to behave in a deterministic way!!
process.env.WINDOW_DPI = process.env.WINDOW_DPI || '2';

function getBaseUrl() {
return `${process.env.BASE_URL}/`;
}

function fixedSize(value) {
return value * process.env.WINDOW_DPI;
}

function fixedFile(name) {
if (process.env.WINDOW_DPI > 1) {
return name.replace('.', `@${process.env.WINDOW_DPI}.`);
}
return name;
}

function callNpx(args) {
const npx = process.platform === 'win32' ? 'npx.cmd' : 'npx';

return new Promise(function fn(resolve) {
const cmd = spawn(npx, args, { shell: true, stdio: 'inherit' });

cmd.on('close', function onClose() {
resolve();
});
});
}

function callTC(targetTest, additionalArgs) {
const browser = process.env.BROWSER != null ? process.env.BROWSER : 'chrome:headless';
const baseArgs = ['testcafe', browser, targetTest, '-s', 'path=e2e/screens', '-q'];
return callNpx(baseArgs.concat(additionalArgs));
}

export {
getBaseUrl, fixedSize, fixedFile, callTC,
};

0 comments on commit 14d44dd

Please sign in to comment.