Closed ralexmatthews closed 5 years ago
that looks good! thank you! how do you do image snapshot tests with it?
published @jest-runner/electron@1.1.1
In our jest.electron-runner.setup.js
file, we define the function like
global.getComponentSnapshot = component =>
new Promise((resolve, reject) => {
const win = remote.getCurrentWindow();
const { container } = render(component);
container.setAttribute("style", "width: fit-content");
const { x, y, height, width } = container.getBoundingClientRect();
delay(100)
.then(() => {
win.webContents.capturePage({ x, y, height, width }, image => {
resolve(image.toPNG());
});
})
.catch(reject);
});
so then in our tests we can
describe("Foo", () => {
it("matches the snapshot", async () => {
const image = await getComponentSnapshot(<Foo />);
expect(image).toMatchImageSnapshot();
});
});
👏
@amatthews4851 that is awesome! it'd be nice to have it in open source. or maybe even make it part of this runner
One and only change for this pull request. Our team here was running into an issue while using this library with image snapshot tests.
Essentially the
render
function will render the components at different sizes if you have the scale set differently from device to device, causing image snapshot tests to fail depending on what device you run the tests on.This change forces the electron environment to keep a consistent scale factor, regardless of what scale the os is set to.
Thank you for your time. Please let me know if you have any questions, concerns, or comments