diff options
Diffstat (limited to 'sonar-css-plugin/css-bundle/tests/server-mock-stylelint.test.ts')
| -rw-r--r-- | sonar-css-plugin/css-bundle/tests/server-mock-stylelint.test.ts | 51 | 
1 files changed, 51 insertions, 0 deletions
| diff --git a/sonar-css-plugin/css-bundle/tests/server-mock-stylelint.test.ts b/sonar-css-plugin/css-bundle/tests/server-mock-stylelint.test.ts new file mode 100644 index 0000000..f7df176 --- /dev/null +++ b/sonar-css-plugin/css-bundle/tests/server-mock-stylelint.test.ts @@ -0,0 +1,51 @@ +import { start, setLogHandlersForTests } from "../src/server"; +import { Server } from "http"; +import * as path from "path"; +import { promisify } from "util"; +import * as stylelint from "stylelint"; +import { postToServer } from "./utils"; + +const filePath = path.join(__dirname, "fixtures", "file.css"); + +const request = JSON.stringify({ +  filePath, +  configFile: path.join(__dirname, "fixtures", "stylelintconfig.json") +}); + +jest.mock("stylelint"); + +describe("server", () => { +  let server: Server; +  let close: () => Promise<void>; +  const logSpy = jest.fn(); +  const errorSpy = jest.fn(); + +  beforeAll(async () => { +    setLogHandlersForTests(logSpy, errorSpy); +    server = await start(); +    close = promisify(server.close.bind(server)); +  }); + +  afterAll(async () => { +    jest.restoreAllMocks(); +    await close(); +  }); + +  it("should not return issues for not original file", async () => { +    (stylelint.lint as any).mockResolvedValue({ +      results: [{ source: "foo.bar" }] +    }); +    const response = await postToServer(request, "/analyze", server); +    expect(JSON.parse(response)).toEqual([]); +    expect(logSpy).toHaveBeenCalledWith( +      `DEBUG For file [${filePath}] received issues with [foo.bar] as a source. They will not be reported.` +    ); +  }); + +  it("should not return issues when failed promise returned", async () => { +    (stylelint.lint as any).mockRejectedValue("some reason"); +    const response = await postToServer(request, "/analyze", server); +    expect(JSON.parse(response)).toEqual([]); +    expect(errorSpy).toHaveBeenCalledWith("some reason"); +  }); +}); | 
