|
1 |
| -import * as child_process from "child_process"; |
| 1 | +jest.mock("./git", () => ({ |
| 2 | + ...jest.requireActual<typeof git>("./git"), |
| 3 | + getUntrackedFileList: jest.fn(), |
| 4 | + getDiffFileList: jest.fn(), |
| 5 | + getDiffForFile: jest.fn(), |
| 6 | + hasCleanIndex: jest.fn(), |
| 7 | +})); |
| 8 | + |
2 | 9 | import type { Linter } from "eslint";
|
3 |
| -import { mocked } from "jest-mock"; |
4 |
| -import { diff, diffConfig, staged, stagedConfig } from "./processors"; |
| 10 | +import * as git from "./git"; |
5 | 11 | import {
|
6 | 12 | diff as fixtureDiff,
|
7 | 13 | staged as fixtureStaged,
|
8 | 14 | } from "./__fixtures__/diff";
|
9 | 15 | import { postprocessArguments } from "./__fixtures__/postprocessArguments";
|
10 | 16 |
|
11 |
| -jest.mock("child_process"); |
12 |
| -const mockedChildProcess = mocked(child_process, true); |
13 |
| -mockedChildProcess.execFileSync.mockReturnValue( |
14 |
| - Buffer.from('/mock filename ", ; .js') |
15 |
| -); |
16 |
| -jest.mock("./git", (): unknown => ({ |
17 |
| - ...jest.requireActual("./git"), |
18 |
| - getDiffFileList: jest |
19 |
| - .fn() |
20 |
| - .mockReturnValue(['/mock filename ", ; .js', "README.md"]), |
21 |
| -})); |
22 |
| - |
23 | 17 | const [messages, filename] = postprocessArguments;
|
24 | 18 |
|
25 |
| -describe("processors", () => { |
26 |
| - it("diff preprocess", () => { |
27 |
| - const validFilename = filename; |
28 |
| - const sourceCode = "/** Some source code */"; |
29 |
| - |
30 |
| - mockedChildProcess.execFileSync.mockReturnValue(Buffer.from(fixtureDiff)); |
| 19 | +const gitMocked: jest.MockedObjectDeep<typeof git> = jest.mocked(git); |
| 20 | +gitMocked.getDiffFileList.mockReturnValue([filename]); |
| 21 | +gitMocked.getUntrackedFileList.mockReturnValue([]); |
31 | 22 |
|
32 |
| - expect(diff.preprocess(sourceCode, validFilename)).toEqual([sourceCode]); |
33 |
| - }); |
34 |
| - |
35 |
| - it("staged preprocess", () => { |
| 23 | +describe("processors", () => { |
| 24 | + it("preprocess (diff and staged)", async () => { |
| 25 | + // The preprocessor does not depend on `staged` being true or false, so it's |
| 26 | + // sufficient to only test one of them. |
36 | 27 | const validFilename = filename;
|
37 | 28 | const sourceCode = "/** Some source code */";
|
38 | 29 |
|
39 |
| - mockedChildProcess.execFileSync.mockReturnValue(Buffer.from(fixtureDiff)); |
| 30 | + const { diff: diffProcessors } = await import("./processors"); |
40 | 31 |
|
41 |
| - expect(diff.preprocess(sourceCode, validFilename)).toEqual([sourceCode]); |
| 32 | + expect(diffProcessors.preprocess(sourceCode, validFilename)).toEqual([ |
| 33 | + sourceCode, |
| 34 | + ]); |
42 | 35 | });
|
43 | 36 |
|
44 |
| - it("diff postprocess", () => { |
45 |
| - mockedChildProcess.execFileSync.mockReturnValue(Buffer.from(fixtureDiff)); |
| 37 | + it("diff postprocess", async () => { |
| 38 | + gitMocked.getDiffForFile.mockReturnValue(fixtureDiff); |
46 | 39 |
|
47 |
| - expect(diff.postprocess(messages, filename)).toMatchSnapshot(); |
| 40 | + const { diff: diffProcessors } = await import("./processors"); |
48 | 41 |
|
49 |
| - expect(mockedChildProcess.execFileSync).toHaveBeenCalled(); |
| 42 | + expect(diffProcessors.postprocess(messages, filename)).toMatchSnapshot(); |
50 | 43 | });
|
51 | 44 |
|
52 |
| - it("staged postprocess", () => { |
53 |
| - mockedChildProcess.execFileSync.mockReturnValue(Buffer.from(fixtureStaged)); |
| 45 | + it("staged postprocess", async () => { |
| 46 | + gitMocked.hasCleanIndex.mockReturnValueOnce(true); |
| 47 | + gitMocked.getDiffForFile.mockReturnValueOnce(fixtureStaged); |
54 | 48 |
|
55 |
| - expect(staged.postprocess(messages, filename)).toMatchSnapshot(); |
| 49 | + const { staged: stagedProcessors } = await import("./processors"); |
56 | 50 |
|
57 |
| - expect(mockedChildProcess.execFileSync).toHaveBeenCalled(); |
| 51 | + expect(stagedProcessors.postprocess(messages, filename)).toMatchSnapshot(); |
58 | 52 | });
|
59 | 53 |
|
60 |
| - it("should report fatal errors", () => { |
61 |
| - mockedChildProcess.execFileSync.mockReturnValue(Buffer.from(fixtureDiff)); |
62 |
| - |
| 54 | + it("should report fatal errors", async () => { |
| 55 | + gitMocked.getDiffForFile.mockReturnValue(fixtureDiff); |
63 | 56 | const [[firstMessage, ...restMessage], ...restMessageArray] = messages;
|
64 | 57 | const messagesWithFatal: Linter.LintMessage[][] = [
|
65 | 58 | [{ ...firstMessage, fatal: true }, ...restMessage],
|
66 | 59 | ...restMessageArray,
|
67 | 60 | ];
|
68 | 61 |
|
69 |
| - expect(diff.postprocess(messages, filename)).toHaveLength(2); |
70 |
| - expect(diff.postprocess(messagesWithFatal, filename)).toHaveLength(3); |
| 62 | + const { diff: diffProcessors } = await import("./processors"); |
71 | 63 |
|
72 |
| - expect(mockedChildProcess.execFileSync).toHaveBeenCalled(); |
| 64 | + expect(diffProcessors.postprocess(messages, filename)).toHaveLength(2); |
| 65 | + expect( |
| 66 | + diffProcessors.postprocess(messagesWithFatal, filename) |
| 67 | + ).toHaveLength(3); |
73 | 68 | });
|
74 | 69 | });
|
75 | 70 |
|
76 | 71 | describe("configs", () => {
|
77 |
| - it("diff", () => { |
| 72 | + it("diff", async () => { |
| 73 | + const { diffConfig } = await import("./processors"); |
78 | 74 | expect(diffConfig).toMatchSnapshot();
|
79 | 75 | });
|
80 | 76 |
|
81 |
| - it("staged", () => { |
| 77 | + it("staged", async () => { |
| 78 | + const { stagedConfig } = await import("./processors"); |
82 | 79 | expect(stagedConfig).toMatchSnapshot();
|
83 | 80 | });
|
84 | 81 | });
|
| 82 | + |
| 83 | +describe("fatal error-message", () => { |
| 84 | + it("getUnstagedChangesError", async () => { |
| 85 | + const { getUnstagedChangesError } = await import("./processors"); |
| 86 | + |
| 87 | + const result = getUnstagedChangesError("mock filename.ts")[0]; |
| 88 | + expect(result?.fatal).toBe(true); |
| 89 | + expect(result?.message).toMatchInlineSnapshot( |
| 90 | + `"mock filename.ts has unstaged changes. Please stage or remove the changes."` |
| 91 | + ); |
| 92 | + }); |
| 93 | +}); |
0 commit comments