Repository: Azure/azure-rest-api-specs
File: .github/shared/test/swagger.test.js (JavaScript)

Code before:
    const swagger = new Swagger(resolve(folder, "invalid.json"), {
      content: invalidRefContent,
      tag: new Tag("test-tag", [], { readme: new Readme("/fake/readme.md") }),
    });

    await expect(swagger.getRefs()).rejects.toThrowErrorMatchingInlineSnapshot(
      `
      [SpecModelError: Failed to resolve file for swagger: ${resolve("/fake/invalid.json")}
        Problem File: ${resolve("/fake/invalid.json")}
        Readme: ${resolve("/fake/readme.md")}
        Tag: test-tag
        Cause: ResolverError: Error reading file "${resolve("/does/not/exist.json").replace(/\\/g, "/").toLowerCase()}"]
    `,
    );
  });

  it("sorts refs in toJSONAsync", async () => {
    // a.json has 2+ refs (nesting/b.json, c.json, etc.) so the sort comparator gets invoked
    const swagger = new Swagger(
      resolve(__dirname, "fixtures/getAffectedSwaggers/specification/1/data-plane/a.json"),
    );
    const json = /** @type {import('../src/swagger.js').SwaggerJSON} */ (
      await swagger.toJSONAsync({ includeRefs: true })
    );
    const refs = /** @type {import('../src/swagger.js').SwaggerJSON[]} */ (json.refs);
    expect(refs.length).toBe(2);
    expect(refs[0].path.localeCompare(refs[1].path)).toBeLessThan(0);
  });

  // TODO: Test that path is resolved against backpointer

  it("excludes example files", async () => {
    const swagger = new Swagger(resolve(__dirname, "fixtures/swagger/ignoreExamples/swagger.json"));
    const refs = await swagger.getRefs();

    const expectedIncludedPath = resolve(
      __dirname,
      "fixtures/swagger/ignoreExamples/included.json",
    );
    expect(refs).toMatchObject(
      new Map([
        [
          expectedIncludedPath,
          expect.objectContaining({
            path: /** @type {unknown} */ (expect.stringContaining(expectedIncludedPath)),
          }),
        ],
      ]),
    );
  });


Review comment (suggestion):
```suggestion
    expect(refs.length).toBe(4);
```

Code after:
    const swagger = new Swagger(resolve(folder, "invalid.json"), {
      content: invalidRefContent,
      tag: new Tag("test-tag", [], { readme: new Readme("/fake/readme.md") }),
    });

    await expect(swagger.getRefs()).rejects.toThrowErrorMatchingInlineSnapshot(
      `
      [SpecModelError: Failed to resolve file for swagger: ${resolve("/fake/invalid.json")}
        Problem File: ${resolve("/fake/invalid.json")}
        Readme: ${resolve("/fake/readme.md")}
        Tag: test-tag
        Cause: ResolverError: Error reading file "${resolve("/does/not/exist.json").replace(/\\/g, "/").toLowerCase()}"]
    `,
    );
  });

  it("sorts refs in toJSONAsync", async () => {
    // a.json has 2+ refs (nesting/b.json, c.json, etc.) so the sort comparator gets invoked
    const swagger = new Swagger(
      resolve(__dirname, "fixtures/getAffectedSwaggers/specification/1/data-plane/a.json"),
    );
    const json = /** @type {import('../src/swagger.js').SwaggerJSON} */ (
      await swagger.toJSONAsync({ includeRefs: true })
    );
    const refs = /** @type {import('../src/swagger.js').SwaggerJSON[]} */ (json.refs);
    expect(refs.length).toBe(4);
    // ensure at least first two elements are sorted correctly
    expect(refs[0].path.localeCompare(refs[1].path)).toBeLessThan(0);
  });

  // TODO: Test that path is resolved against backpointer

  it("excludes example files", async () => {
    const swagger = new Swagger(resolve(__dirname, "fixtures/swagger/ignoreExamples/swagger.json"));
    const refs = await swagger.getRefs();

    const expectedIncludedPath = resolve(
      __dirname,
      "fixtures/swagger/ignoreExamples/included.json",
    );
    expect(refs).toMatchObject(
      new Map([
        [
          expectedIncludedPath,
          expect.objectContaining({
            path: /** @type {unknown} */ (expect.stringContaining(expectedIncludedPath)),
          }),
        ],
      ]),
    );
  });