Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recreate golden with this before each test #41

Merged
merged 5 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/browser/vdiff.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { chai, expect } from '@open-wc/testing';
import { executeServerCommand } from '@web/test-runner-commands';

chai.Assertion.addMethod('golden', ScreenshotAndCompare);
let test;

async function ScreenshotAndCompare(name, opts) {
const elem = this._obj;
const rect = elem.getBoundingClientRect();
chai.Assertion.addMethod('golden', function(...args) {
return ScreenshotAndCompare.call({ test, elem: this._obj }, ...args); // eslint-disable-line no-invalid-this
});
mocha.setup({ // eslint-disable-line no-undef
rootHooks: {
beforeEach() {
test = this.currentTest;
}
}
});

async function ScreenshotAndCompare(opts) {
const name = this.test.fullTitle();
const rect = this.elem.getBoundingClientRect();
const { pass, message } = await executeServerCommand('brightspace-visual-diff', { name, rect, opts });
if (!pass) {
expect.fail(message);
Expand Down
3 changes: 1 addition & 2 deletions src/server/visual-diff-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ export function visualDiff({ updateGoldens = false, runSubset = false } = {}) {
}
}

const opts = payload.opts || {};
opts.margin = opts.margin || DEFAULT_MARGIN;
const opts = { margin: DEFAULT_MARGIN, ...payload.opts };

const page = session.browser.getPage(session.id);
await page.screenshot({
Expand Down
8 changes: 4 additions & 4 deletions test/browser/element.vdiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ describe('element-matches', () => {
{ name: 'rtl', rtl: true },
{ name: 'transition', action: elem => elem.style.opacity = '0.2' }
].forEach(({ name, rtl, action }) => {
it(name, async function() {
it(name, async() => {
const elem = await fixture(`<${elementTag} text="Visual Difference"></${elementTag}>`, { rtl: rtl });
if (action) await action(elem);
await expect(elem).to.be.golden(this.test.fullTitle());
await expect(elem).to.be.golden();
});
});
});
Expand All @@ -65,7 +65,7 @@ describe('element-different', () => {
elem.style.height = '70px';
} }*/
].forEach(({ name, action }) => {
it(name, async function() {
it(name, async() => {
const elem = await fixture(`<${elementTag} text="Visual Difference"></${elementTag}>`);
const isGolden = await executeServerCommand('vdiff-get-golden-flag');
if (!isGolden) {
Expand All @@ -75,7 +75,7 @@ describe('element-different', () => {

let fail = false;
try {
await expect(elem).to.be.golden(this.test.fullTitle());
await expect(elem).to.be.golden();
} catch (ex) {
fail = true;
}
Expand Down