Skip to content

Commit 0915501

Browse files
committed
refactor: fix unit-test for windows
1 parent aebd6a7 commit 0915501

1 file changed

Lines changed: 145 additions & 135 deletions

File tree

Lines changed: 145 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,172 +1,182 @@
1-
import type { ReporterOptions } from 'knip';
2-
import type { IssueRecords, IssueSet } from 'knip/dist/types/issues';
3-
import { fs as memfsFs, vol } from 'memfs';
4-
import { beforeEach, describe, expect, it, vi } from 'vitest';
5-
import type { AuditOutputs } from '@code-pushup/models';
6-
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
7-
import { logger } from '@code-pushup/utils';
8-
import { rawReport } from '../../../mocks/fixtures/raw-knip.report';
9-
import { KNIP_RAW_REPORT_NAME, KNIP_REPORT_NAME } from '../constants.js';
10-
import { CustomReporterOptions } from './model.js';
11-
import { knipReporter } from './reporter.js';
1+
import type {ReporterOptions} from 'knip';
2+
import type {IssueRecords, IssueSet} from 'knip/dist/types/issues';
3+
import {fs as memfsFs, vol} from 'memfs';
4+
import {beforeEach, describe, expect, it, vi} from 'vitest';
5+
import type {AuditOutputs} from '@code-pushup/models';
6+
import {MEMFS_VOLUME} from '@code-pushup/test-utils';
7+
import {logger} from '@code-pushup/utils';
8+
import {rawReport} from '../../../mocks/fixtures/raw-knip.report';
9+
import {KNIP_RAW_REPORT_NAME, KNIP_REPORT_NAME} from '../constants.js';
10+
import {CustomReporterOptions} from './model.js';
11+
import {knipReporter} from './reporter.js';
1212

1313
vi.mock('@code-pushup/utils', async () => {
14-
const actual = await vi.importActual('@code-pushup/utils');
15-
return {
16-
...actual,
17-
getGitRoot: vi
18-
.fn()
19-
.mockResolvedValue('/Users/username/Projects/code-pushup-cli/'),
20-
logger: {
21-
info: vi.fn(),
22-
error: vi.fn(),
23-
warn: vi.fn(),
24-
},
25-
};
14+
const actual = await vi.importActual('@code-pushup/utils');
15+
return {
16+
...actual,
17+
getGitRoot: vi
18+
.fn()
19+
.mockResolvedValue('/Users/username/Projects/code-pushup-cli/'),
20+
logger: {
21+
info: vi.fn(),
22+
error: vi.fn(),
23+
warn: vi.fn(),
24+
},
25+
};
2626
});
2727

2828
describe('knipReporter', () => {
29-
beforeEach(() => {
30-
vol.reset();
31-
vol.fromJSON({ [MEMFS_VOLUME]: null });
32-
});
29+
beforeEach(() => {
30+
vol.reset();
31+
vol.fromJSON({[MEMFS_VOLUME]: null});
32+
});
3333

34-
it('should saves report to file system by default', async () => {
35-
await expect(
36-
knipReporter({
37-
report: {
38-
files: true,
39-
},
40-
issues: {
41-
files: new Set(['main.js']),
42-
},
43-
} as ReporterOptions),
44-
).resolves.toBeUndefined();
34+
it('should saves report to file system by default', async () => {
35+
await expect(
36+
knipReporter({
37+
report: {
38+
files: true,
39+
},
40+
issues: {
41+
files: new Set(['main.js']),
42+
},
43+
} as ReporterOptions),
44+
).resolves.toBeUndefined();
4545

46-
expect(logger.info).not.toHaveBeenCalled();
47-
});
46+
expect(logger.info).not.toHaveBeenCalled();
47+
});
4848

49-
it('should accept reporter option outputFile', async () => {
50-
const outputFile = 'my-report.json';
51-
await expect(
52-
knipReporter({
53-
report: {
54-
files: true,
55-
},
56-
issues: {
57-
files: new Set(['main.js']),
58-
},
59-
options: JSON.stringify({ outputFile } satisfies CustomReporterOptions),
60-
} as ReporterOptions),
61-
).resolves.toBeUndefined();
49+
it('should accept reporter option outputFile', async () => {
50+
const outputFile = 'my-report.json';
51+
await expect(
52+
knipReporter({
53+
report: {
54+
files: true,
55+
},
56+
issues: {
57+
files: new Set(['main.js']),
58+
},
59+
options: JSON.stringify({outputFile} satisfies CustomReporterOptions),
60+
} as ReporterOptions),
61+
).resolves.toBeUndefined();
6262

63-
expect(logger.info).not.toHaveBeenCalled();
63+
expect(logger.info).not.toHaveBeenCalled();
6464

65-
const auditOutputs = JSON.parse(
66-
(
67-
await memfsFs.promises.readFile(outputFile, { encoding: 'utf8' })
68-
).toString(),
69-
);
70-
// Reporter returns all audits to match plugin configuration
71-
expect(auditOutputs).toHaveLength(14);
72-
expect(auditOutputs).toContainEqual(
73-
expect.objectContaining({ slug: 'files', score: 0, value: 1 }),
74-
);
75-
// Other audits should have score 1 (no issues)
76-
expect(
77-
auditOutputs.filter((audit: { slug: string }) => audit.slug !== 'files'),
78-
).toHaveLength(13);
79-
});
65+
const auditOutputs = JSON.parse(
66+
(
67+
await memfsFs.promises.readFile(outputFile, {encoding: 'utf8'})
68+
).toString(),
69+
);
70+
// Reporter returns all audits to match plugin configuration
71+
expect(auditOutputs).toHaveLength(14);
72+
expect(auditOutputs).toContainEqual(
73+
expect.objectContaining({slug: 'files', score: 0, value: 1}),
74+
);
75+
// Other audits should have score 1 (no issues)
76+
expect(
77+
auditOutputs.filter((audit: { slug: string }) => audit.slug !== 'files'),
78+
).toHaveLength(13);
79+
});
8080

81-
it('should accept reporter option rawOutputFile', async () => {
82-
const rawOutputFile = KNIP_RAW_REPORT_NAME;
83-
const fileIssueSet: IssueSet = new Set(['main.js']);
84-
const unlistedIssueRecords: IssueRecords = {
85-
'/User/username/code-pushup-cli/packages/utils/.eslintrc.json': {
86-
'jsonc-eslint-parser': {
87-
type: 'unlisted',
88-
symbol: 'jsonc-eslint-parser',
89-
filePath:
90-
'/User/username/code-pushup-cli/packages/utils/package.json',
91-
},
92-
},
93-
};
81+
it('should accept reporter option rawOutputFile', async () => {
82+
const rawOutputFile = KNIP_RAW_REPORT_NAME;
83+
const fileIssueSet: IssueSet = new Set(['main.js']);
84+
const unlistedIssueRecords: IssueRecords = {
85+
'/User/username/code-pushup-cli/packages/utils/.eslintrc.json': {
86+
'jsonc-eslint-parser': {
87+
type: 'unlisted',
88+
symbol: 'jsonc-eslint-parser',
89+
filePath:
90+
'/User/username/code-pushup-cli/packages/utils/package.json',
91+
},
92+
},
93+
};
9494

95-
await expect(
96-
knipReporter({
97-
report: {
98-
files: true,
99-
unlisted: true,
100-
},
101-
issues: {
102-
files: fileIssueSet,
103-
unlisted: unlistedIssueRecords,
104-
},
105-
options: JSON.stringify({ rawOutputFile }),
106-
// other reporter options for debugging purpose
107-
counters: { files: 1, unlisted: 1 },
108-
} as ReporterOptions),
109-
).resolves.toBeUndefined();
95+
await expect(
96+
knipReporter({
97+
report: {
98+
files: true,
99+
unlisted: true,
100+
},
101+
issues: {
102+
files: fileIssueSet,
103+
unlisted: unlistedIssueRecords,
104+
},
105+
options: JSON.stringify({rawOutputFile}),
106+
// other reporter options for debugging purpose
107+
counters: {files: 1, unlisted: 1},
108+
} as ReporterOptions),
109+
).resolves.toBeUndefined();
110110

111-
expect(logger.info).not.toHaveBeenCalled();
111+
expect(logger.info).not.toHaveBeenCalled();
112112

113-
const rawKnipReport = JSON.parse(
114-
(
115-
await memfsFs.promises.readFile(rawOutputFile, { encoding: 'utf8' })
116-
).toString(),
117-
);
118-
expect(rawKnipReport.report).toStrictEqual({ files: true, unlisted: true });
119-
expect(rawKnipReport.options).toStrictEqual({ rawOutputFile });
120-
expect(rawKnipReport.issues.files).toStrictEqual(['main.js']);
121-
expect(rawKnipReport.issues.unlisted).toStrictEqual(unlistedIssueRecords);
122-
expect(rawKnipReport.counters).toStrictEqual({ files: 1, unlisted: 1 });
123-
});
113+
const rawKnipReport = JSON.parse(
114+
(
115+
await memfsFs.promises.readFile(rawOutputFile, {encoding: 'utf8'})
116+
).toString(),
117+
);
118+
expect(rawKnipReport.report).toStrictEqual({files: true, unlisted: true});
119+
expect(rawKnipReport.options).toStrictEqual({rawOutputFile});
120+
expect(rawKnipReport.issues.files).toStrictEqual(['main.js']);
121+
expect(rawKnipReport.issues.unlisted).toStrictEqual({
122+
'/User/username/code-pushup-cli/packages/utils/.eslintrc.json': {
123+
'jsonc-eslint-parser': {
124+
type: 'unlisted',
125+
symbol: 'jsonc-eslint-parser',
126+
filePath: expect.stringContaining('package.json')
127+
},
128+
},
129+
});
130+
expect(rawKnipReport.counters).toStrictEqual({files: 1, unlisted: 1});
131+
});
132+
});
124133

125-
it('should log if custom reporter option verbose is true', async () => {
134+
it('should log if custom reporter option verbose is true', async () => {
126135
const reporterOptions: CustomReporterOptions = {
127-
verbose: true,
128-
outputFile: KNIP_REPORT_NAME,
129-
rawOutputFile: KNIP_RAW_REPORT_NAME,
136+
verbose: true,
137+
outputFile: KNIP_REPORT_NAME,
138+
rawOutputFile: KNIP_RAW_REPORT_NAME,
130139
};
131140
await expect(
132-
knipReporter({
133-
report: { files: true },
134-
issues: { files: new Set(['main.js']) },
135-
options: JSON.stringify(reporterOptions),
136-
} as ReporterOptions),
141+
knipReporter({
142+
report: {files: true},
143+
issues: {files: new Set(['main.js'])},
144+
options: JSON.stringify(reporterOptions),
145+
} as ReporterOptions),
137146
).resolves.toBeUndefined();
138147

139148
expect(logger.info).toHaveBeenCalledTimes(3);
140149
expect(logger.info).toHaveBeenNthCalledWith(
141-
1,
142-
`Reporter called with options: ${JSON.stringify(
143-
reporterOptions,
144-
null,
145-
2,
146-
)}`,
150+
1,
151+
`Reporter called with options: ${JSON.stringify(
152+
reporterOptions,
153+
null,
154+
2,
155+
)}`,
147156
);
148157
expect(logger.info).toHaveBeenNthCalledWith(
149-
2,
150-
`Saved raw report to ${reporterOptions.rawOutputFile}`,
158+
2,
159+
`Saved raw report to ${reporterOptions.rawOutputFile}`,
151160
);
152161
expect(logger.info).toHaveBeenNthCalledWith(
153-
3,
154-
`Saved report to ${reporterOptions.outputFile}`,
162+
3,
163+
`Saved report to ${reporterOptions.outputFile}`,
155164
);
156-
});
165+
});
157166

158-
it('should produce valid audit outputs', async () => {
167+
it('should produce valid audit outputs', async () => {
159168
await expect(
160-
knipReporter(rawReport as ReporterOptions),
169+
knipReporter(rawReport as ReporterOptions),
161170
).resolves.toBeUndefined();
162171

163172
const auditOutputsContent = await memfsFs.promises.readFile(
164-
'knip-report.json',
165-
{ encoding: 'utf8' },
173+
'knip-report.json',
174+
{encoding: 'utf8'},
166175
);
167176
const auditOutputsJson = JSON.parse(
168-
auditOutputsContent.toString(),
177+
auditOutputsContent.toString(),
169178
) as AuditOutputs;
170179
expect(auditOutputsJson).toMatchSnapshot();
171-
});
172180
});
181+
})
182+
;

0 commit comments

Comments
 (0)