aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-css-plugin/css-bundle/tests/server-mock-stylelint.test.ts
blob: f7df176e5fee23ae4001ad9ef64ce69db4c61943 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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");
  });
});