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

Diffs returning errors instead of fails #26

Closed
ruxiang05 opened this issue Oct 31, 2017 · 5 comments
Closed

Diffs returning errors instead of fails #26

ruxiang05 opened this issue Oct 31, 2017 · 5 comments

Comments

@ruxiang05
Copy link

When there is a diff between the images, the tests throw an error instead of returning a failed test. Is that a normal behaviour or how shall I handle failed test?

@anescobar1991
Copy link
Member

@ruxiang05 can you provide an example or sample repo showing how you are using it and tell me what version you are using? I have not seen that issue. The matcher is supposed to fail the test just the same as any other matcher.

@ruxiang05
Copy link
Author

const puppeteer = require('puppeteer');
const { toMatchImageSnapshot } = require('jest-image-snapshot');
expect.extend({ toMatchImageSnapshot });

test('visual regression', async (done) => {
const selector ='a[href*="selectedStory="]';
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://localhost:8080');

await page.evaluate( () => {
const components = Array.from(document.querySelectorAll('div[data-name]'));
for(let i = 1; i < components.length; i++) {
components[i].addEventListener('click',() => {});
components[i].click();
}
})
let iframe = await page.$('#storybook-preview-iframe');
const clip = Object.assign({}, await iframe.boundingBox());

let storyLinks = await page.evaluate((selector) => {
const stories = Array.from(document.querySelectorAll(selector));
const links = stories.map(story => {
let href = story.href;
let name = story.text.replace(/[^A-Z0-9]/ig, '-').replace(/-{2,}/,'-');
let component = href.match(/selectedKind=(.*?)&/).pop();
return {href: href, name: component + '-' + name};
});
return links;
}, selector)
.then(async (storyLinks) => {
for(let i = 0; i < storyLinks.length; i++){
const storyLink = storyLinks[i];
await page.goto(storyLink.href);
const screen = await page.screenshot({clip: {x:clip.x, y: clip.y, width: clip.width, height: clip.height}});
await expect(screen).toMatchImageSnapshot({customSnapshotIdentifier: storyLink.name});
done();
}
}).catch((err) => {
console.log(err);
});

await browser.close();
});

I'm using version 1.0.1 because of #22

@anescobar1991
Copy link
Member

The issue is that you are unnecessarily using await on toMatchImageSnapshot() which then turns the assertion into a promise that would need to be handled:

Instead of
await expect(something).toMatchImageSnapshot();

You just need to do
expect(something).toMatchImageSnapshot();

@ruxiang05
Copy link
Author

Doesn't change anything, tried it.

@anescobar1991
Copy link
Member

It is hard to read your code without it being formatted but it seems to me (from a very quick glance) that you are not handling your async code properly in the test.

Read up on testing asynchronous code in the jest docs. If I have time later I will look into this with more detail but it does not seem to be a jest-image-snapshot issue.

anescobar1991 added a commit that referenced this issue May 29, 2018
…for-custom-snapshots-dir to master

* commit 'cb529963b7f00c6c1be2a43bf4a7a815e611a0f0':
  fix(configureToMatchImageSnapshot): add ability to have common customSnapshotsDir
goverdhan07 pushed a commit to goverdhan07/jest-image-snapshot that referenced this issue Jul 23, 2023
… feature/allow-for-custom-snapshots-dir to master

* commit 'cb529963b7f00c6c1be2a43bf4a7a815e611a0f0':
  fix(configureToMatchImageSnapshot): add ability to have common customSnapshotsDir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants