From 92cffc39cd271789a37e9bcdcdbf9390f3366b31 Mon Sep 17 00:00:00 2001 From: "NAKAMURA, Tomohiro" Date: Sun, 1 Sep 2024 00:22:10 +0900 Subject: [PATCH] Fix test's coding (#75) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- .eslintrc | 4 ++++ e2e/cases/main.test.js | 26 ++++++++++++++------------ e2e/cases/setting.test.js | 28 ++++++++++++---------------- package.json | 2 +- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/.eslintrc b/.eslintrc index 2753bb3..3405e18 100644 --- a/.eslintrc +++ b/.eslintrc @@ -4,6 +4,10 @@ "node": true, "browser": true }, + "globals": { + "fixture": "readonly", + "test": "readonly" + }, "extends": "airbnb-base", "parserOptions": { "sourceType": "module" diff --git a/e2e/cases/main.test.js b/e2e/cases/main.test.js index 7b470cc..5306955 100644 --- a/e2e/cases/main.test.js +++ b/e2e/cases/main.test.js @@ -1,11 +1,12 @@ 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'); @@ -13,16 +14,16 @@ 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, '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 }); @@ -30,16 +31,16 @@ 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_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); @@ -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 }); }); diff --git a/e2e/cases/setting.test.js b/e2e/cases/setting.test.js index f9f0c4a..8727d62 100644 --- a/e2e/cases/setting.test.js +++ b/e2e/cases/setting.test.js @@ -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'); @@ -34,8 +33,7 @@ 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'); @@ -43,37 +41,35 @@ test('should care environment variables', async t => { 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(); diff --git a/package.json b/package.json index b20b47c..d3caa82 100644 --- a/package.json +++ b/package.json @@ -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" },