Skip to content

Commit

Permalink
fix: Recreate golden with this before each test (#41)
Browse files Browse the repository at this point in the history
* Recreate golden with this before each test

* Remove unnecessary closure

* Clean up command opts

* Put test and elem in this

* Avoid setting assertion method multiple times
  • Loading branch information
bearfriend authored Jun 26, 2023
1 parent 7355448 commit 8906eb7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
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

0 comments on commit 8906eb7

Please sign in to comment.