Skip to content

Commit

Permalink
Fix test's coding (#75)
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

* fix: update .eslintrc to ignore fixture/test of test case code

* fix: format test's codes with eslint

---------

Co-authored-by: Alvaro Cabrera Durán <[email protected]>
  • Loading branch information
tomoh1r and pateketrueke authored Aug 31, 2024
1 parent 14d44dd commit 92cffc3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"node": true,
"browser": true
},
"globals": {
"fixture": "readonly",
"test": "readonly"
},
"extends": "airbnb-base",
"parserOptions": {
"sourceType": "module"
Expand Down
26 changes: 14 additions & 12 deletions e2e/cases/main.test.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
import { Selector } from 'testcafe';

import { getBaseUrl } from '../lib';
import { takeSnapshot } from '../../lib';

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

test('should render "It works!"', async t => {
process.env.TAKE_SNAPSHOT = "1";
process.env.TAKE_SNAPSHOT = '1';

await t.wait(1000);
await takeSnapshot(t, 'before_assert_it_works');

await t
.expect(Selector('h1').exists).ok()
.expect(Selector('h1').visible).ok()
.expect(Selector('h1').innerText).contains('It works!');
.expect(Selector('h1').innerText)
.contains('It works!');

await takeSnapshot(t, 'after_assert_it_works');
});

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

test('should render "It works!"', async t => {
process.env.TAKE_SNAPSHOT = "1";
process.env.TAKE_SNAPSHOT = '1';

await t.wait(1000);
await takeSnapshot(t, { label: 'before_assert_fullpage', fullPage: true });

await t
.expect(Selector('h1').exists).ok()
.expect(Selector('h1').visible).ok()
.expect(Selector('h1').innerText).contains('It works!');
.expect(Selector('h1').innerText)
.contains('It works!');

await takeSnapshot(t, { label: 'after_assert_fullpage', fullPage: true });
});

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

test('should render "It works!"', async t => {
process.env.TAKE_SNAPSHOT = "1";
process.env.TAKE_SNAPSHOT = '1';

await t.wait(1000);

Expand All @@ -52,7 +53,8 @@ test('should render "It works!"', async t => {
await t
.expect(Selector('h1').exists).ok()
.expect(Selector('h1').visible).ok()
.expect(Selector('h1').innerText).contains('It works!');
.expect(Selector('h1').innerText)
.contains('It works!');

await takeSnapshot(t, { label: 'after_assert_blockout', blockOut: elements, fullPage: true });
});
28 changes: 12 additions & 16 deletions e2e/cases/setting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import { join } from 'node:path';
import sizeOf from 'image-size';
import { rimrafSync } from 'rimraf';

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

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

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

const basePath = join('e2e', 'screens', 'Testing_set_FULL__PAGE');
rimrafSync(basePath);

process.env.FULL_PAGE = "1";
process.env.FULL_PAGE = '1';
await t.resizeWindow(640, 480);
await takeSnapshot(t, 'after_set_env');

Expand All @@ -34,46 +33,43 @@ test('should care environment variables', async t => {
await t.expect(pngWithOutEnv.height).lte(fixedSize(480));
});

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

test('should care environment variables', async t => {
const basePath = join('e2e', 'screens', 'Testing_set_TAKE__SNAPSHOT');
rimrafSync(basePath);

await t.wait(1000);

process.env.TAKE_SNAPSHOT = "1";
process.env.TAKE_SNAPSHOT = '1';
await takeSnapshot(t, 'after_set_env');

delete process.env.TAKE_SNAPSHOT
delete process.env.TAKE_SNAPSHOT;
await takeSnapshot(t, 'after_del_env');

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

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

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

const basePath = join('e2e', 'screens', 'Testing_set_SNAPSHOT__NAME');
rimrafSync(basePath);

await t.wait(1000);

process.env.SNAPSHOT_NAME = "base";
process.env.SNAPSHOT_NAME = 'base';
await takeSnapshot(t, 'after_set_env_base');

process.env.SNAPSHOT_NAME = "actual";
process.env.SNAPSHOT_NAME = 'actual';
await takeSnapshot(t, 'after_set_env_actual');

process.env.SNAPSHOT_NAME = "foo";
process.env.SNAPSHOT_NAME = 'foo';
await takeSnapshot(t, 'after_set_env_foo');


await t.expect(existsSync(join(basePath, 'after__set__env__base', fixedFile('base.png')))).ok();
await t.expect(existsSync(join(basePath, 'after__set__env__actual', fixedFile('actual.png')))).ok();
await t.expect(existsSync(join(basePath, 'after__set__env__foo', fixedFile('foo.png')))).ok();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"build": "bili --format umd-min --module-name reporter --minimal --file-name reporter.umd.js",
"serve": "sirv e2e/public --port 3000",
"prepublish": "npm run dist",
"pretest": "eslint src lib",
"pretest": "eslint src lib e2e",
"test": "npm run test:e2e -- -a 'npm run serve' e2e/cases",
"test:e2e": "BASE_URL=http://localhost:3000 testcafe ${BROWSER:-chrome:headless} --screenshots-full-page -s e2e/screens -q"
},
Expand Down

0 comments on commit 92cffc3

Please sign in to comment.