Skip to content

Commit 0b6cebb

Browse files
authored
Merge pull request #159 from rtfpessoa/test-main
add main tests
2 parents e9b5c17 + 7af0cfb commit 0b6cebb

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

jest.config.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ const jestConfig: JestConfigWithTsJest = {
2727
coveragePathIgnorePatterns: ['/node_modules/', 'src/__tests__/'],
2828
coverageThreshold: {
2929
global: {
30-
statements: 19,
31-
branches: 10,
32-
functions: 33,
33-
lines: 19,
30+
statements: 75,
31+
branches: 53,
32+
functions: 63,
33+
lines: 75,
3434
},
3535
},
3636
};

src/__tests__/cli-tests.ts

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ afterEach(() => {
2626
jest.clearAllMocks();
2727
});
2828

29+
process.argv = ['node', 'diff2html.js', '-i', 'stdin', '--', 'test'];
30+
2931
describe('cli', () => {
3032
describe('getInput', () => {
3133
test('should readFile when inputType is `file`', async () => {

src/__tests__/main-tests.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { jest, expect } from '@jest/globals';
2+
3+
import { Configuration, InputType } from '../types';
4+
import { Diff2HtmlConfig } from 'diff2html';
5+
6+
let command: typeof import('../main');
7+
8+
const getInputSpy: jest.Mock<(inputType: InputType, inputArgs: string[], ignore: string[]) => Promise<string>> =
9+
jest.fn();
10+
const getOutputSpy: jest.Mock<(options: Diff2HtmlConfig, config: Configuration, input: string) => string> = jest.fn();
11+
const previewSpy: jest.Mock<(content: string, format: string) => void> = jest.fn();
12+
jest.unstable_mockModule('../cli', async () => ({
13+
getInput: getInputSpy,
14+
getOutput: getOutputSpy,
15+
preview: previewSpy,
16+
}));
17+
18+
beforeEach(async () => {
19+
command = await import('../main');
20+
});
21+
22+
afterEach(() => {
23+
jest.clearAllMocks();
24+
});
25+
26+
process.argv = ['node', 'diff2html.js', '-i', 'file', '--', 'test'];
27+
28+
describe('cli', () => {
29+
test('should parse input and run', async () => {
30+
getInputSpy.mockReturnValue(Promise.resolve('input'));
31+
getOutputSpy.mockReturnValue('output');
32+
previewSpy.mockReturnValue();
33+
34+
await command.main();
35+
36+
expect(getInputSpy).toHaveBeenCalledTimes(1);
37+
expect(getInputSpy).toHaveBeenCalledWith('file', ['test'], []);
38+
39+
expect(getOutputSpy).toHaveBeenCalledTimes(1);
40+
expect(getOutputSpy).toHaveBeenCalledWith(
41+
{
42+
diffMaxChanges: undefined,
43+
diffMaxLineLength: undefined,
44+
diffStyle: 'word',
45+
drawFileList: true,
46+
matchWordsThreshold: 0.25,
47+
matching: 'none',
48+
matchingMaxComparisons: 1000,
49+
maxLineLengthHighlight: 10000,
50+
maxLineSizeInBlockForComparison: 200,
51+
outputFormat: 'line-by-line',
52+
renderNothingWhenEmpty: false,
53+
},
54+
{
55+
diffyType: undefined,
56+
fileContentToggle: true,
57+
formatType: 'html',
58+
highlightCode: true,
59+
htmlWrapperTemplate: expect.stringContaining('diff2html-cli/template.html'),
60+
ignore: [],
61+
inputSource: 'file',
62+
outputDestinationFile: undefined,
63+
outputDestinationType: 'preview',
64+
pageHeader: 'Diff to HTML by <a href="https://github.com/rtfpessoa">rtfpessoa</a>',
65+
pageTitle: 'Diff to HTML by rtfpessoa',
66+
showFilesOpen: false,
67+
synchronisedScroll: true,
68+
},
69+
'input',
70+
);
71+
});
72+
});

0 commit comments

Comments
 (0)