facebook-atom / jest-electron-runner

custom test runner for Jest that allows tests to be run in Electron environment
MIT License
189 stars 33 forks source link

Sets consistant render scale #33

Closed ralexmatthews closed 5 years ago

ralexmatthews commented 5 years ago

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

aaronabramov commented 5 years ago

that looks good! thank you! how do you do image snapshot tests with it?

aaronabramov commented 5 years ago

published @jest-runner/electron@1.1.1

ralexmatthews commented 5 years ago

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();
  });
});
chadwatson commented 5 years ago

👏

aaronabramov commented 5 years ago

@amatthews4851 that is awesome! it'd be nice to have it in open source. or maybe even make it part of this runner