igor-dv / jest-specific-snapshot

Jest matcher for multiple snapshot files per test
MIT License
56 stars 15 forks source link

test.concurrent() issue? #49

Open nutchanon-c opened 1 year ago

nutchanon-c commented 1 year ago

In one test file, I have a few describe() and a few test.concurrent() under each describe. In each test.concurrent(), I have it save to different snapshot files.

For example, for 10 total test, there will be 10 snapshot files.

However, I see that the snapshots are being saved to incorrect files with also incorrect names despite passing in the hint as well (they get mixed up with the test names).

Are there any workarounds to this issue?

igor-dv commented 1 year ago

I never tested it with concurrency, so I may assume there is some race condition there. The workaround depends on the reason you need a concurrency, you can do it from outside rather with using jest.

Would you like to provide a reproduction of the problem ? Either adding a test here , or creating a project that reproduces it ? If I will have a bit of time I can take a look if it's solvable.

nutchanon-c commented 1 year ago

I can't seem to really reproduce it in my own personal project. However, it happens with some internal tests that I probably could not share the code here but the structure is something like this. The actual project is written with TypeScript.

The actual tests between expecting toMatchSpecificSnapshot() includes multiple get/post requests and a test usually takes around 20 seconds or so to complete.

The snapshot files are created correctly, but it would jump around from file to file with incorrect snapshot name (describe + it/test name + hint). The faulty behavior is the same is if you're running tests concurrently with toMatchSnapshot().

describe("test", () => {
  it.concurrent("test 1", async () => {
    // sending http requests and getting the result
    expect(2).toMatchSpecificSnapshot("./snapshots/one.snp", "test1");
  });
  it.concurrent("test 2", async () => {
    // sending http requests and getting the result
    expect({ foo: 2, bar: 3 }).toMatchSpecificSnapshot(
      "./snapshots/two.snp",
      { foo: expect.any(Number) },
      "test2"
    );
  });
  it.concurrent("test 3", async () => {
    // sending http requests and getting the result
    expect({ baz: 3 }).toMatchSpecificSnapshot(
      "./snapshots/three.snp",
      { baz: expect.any(Number) },
      "test3"
    );
  });
});