Skip to content

Commit 396b761

Browse files
committed
refactor tests
1 parent 4ceac7f commit 396b761

File tree

5 files changed

+131
-143
lines changed

5 files changed

+131
-143
lines changed

packages/plugin-typescript/src/lib/schema.unit.test.ts

Lines changed: 47 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,84 +4,65 @@ import {
44
typescriptPluginConfigSchema,
55
} from './schema.js';
66

7-
describe('TypescriptPlugin Configuration', () => {
7+
describe('typescriptPluginConfigSchema', () => {
88
const tsConfigPath = 'tsconfig.json';
99

10-
describe('typescriptPluginConfigSchema', () => {
11-
it('accepts a valid configuration', () => {
12-
expect(() =>
13-
typescriptPluginConfigSchema.parse({
14-
tsConfigPath,
15-
onlyAudits: ['no-implicit-any', 'module-resolution-node'],
16-
} satisfies TypescriptPluginOptions),
17-
).not.toThrow();
18-
});
10+
it('accepts a empty configuration', () => {
11+
expect(() => typescriptPluginConfigSchema.parse({})).not.toThrow();
1912
});
2013

21-
describe('tsConfigPath', () => {
22-
it('accepts a valid configuration', () => {
23-
expect(() =>
24-
typescriptPluginConfigSchema.parse({
25-
tsConfigPath,
26-
} satisfies TypescriptPluginOptions),
27-
).not.toThrow();
28-
});
14+
it('accepts a configuration with tsConfigPath set', () => {
15+
expect(() =>
16+
typescriptPluginConfigSchema.parse({
17+
tsConfigPath,
18+
} satisfies TypescriptPluginOptions),
19+
).not.toThrow();
20+
});
2921

30-
it('throws for invalid tsConfigPath type', () => {
31-
expect(() =>
32-
typescriptPluginConfigSchema.parse({
33-
onlyAudits: 123,
34-
}),
35-
).toThrow('invalid_type');
36-
});
22+
it('accepts a configuration with tsConfigPath and empty onlyAudits', () => {
23+
expect(() =>
24+
typescriptPluginConfigSchema.parse({
25+
tsConfigPath,
26+
onlyAudits: [],
27+
} satisfies TypescriptPluginOptions),
28+
).not.toThrow();
3729
});
3830

39-
describe('onlyAudits', () => {
40-
it('accepts a valid `onlyAudits` array', () => {
41-
expect(() =>
42-
typescriptPluginConfigSchema.parse({
43-
tsConfigPath,
44-
onlyAudits: ['no-implicit-any', 'module-resolution-node'],
45-
} satisfies TypescriptPluginOptions),
46-
).not.toThrow();
47-
});
31+
it('accepts a configuration with tsConfigPath and full onlyAudits', () => {
32+
expect(() =>
33+
typescriptPluginConfigSchema.parse({
34+
tsConfigPath,
35+
onlyAudits: ['no-implicit-any', 'jsx', 'strict-function-types'],
36+
} satisfies TypescriptPluginOptions),
37+
).not.toThrow();
38+
});
4839

49-
it('accepts empty array for tsCodes', () => {
50-
expect(() =>
51-
typescriptPluginConfigSchema.parse({
52-
tsConfigPath,
53-
onlyAudits: [],
54-
} satisfies TypescriptPluginOptions),
55-
).not.toThrow();
56-
});
40+
it('throws for invalid onlyAudits', () => {
41+
expect(() =>
42+
typescriptPluginConfigSchema.parse({
43+
onlyAudits: 123,
44+
}),
45+
).toThrow('invalid_type');
46+
});
5747

58-
it('allows tsCodes to be undefined', () => {
59-
expect(() =>
60-
typescriptPluginConfigSchema.parse({
61-
tsConfigPath,
62-
} satisfies TypescriptPluginOptions),
63-
).not.toThrow();
64-
});
48+
it('throws for invalid onlyAudits items', () => {
49+
expect(() =>
50+
typescriptPluginConfigSchema.parse({
51+
tsConfigPath,
52+
onlyAudits: [123, true],
53+
}),
54+
).toThrow('invalid_type');
55+
});
6556

66-
it('throws for array with non-string elements', () => {
67-
expect(() =>
57+
it('throws for unknown audit slug', () => {
58+
expect(
59+
() =>
6860
typescriptPluginConfigSchema.parse({
6961
tsConfigPath,
70-
onlyAudits: [123, true],
62+
onlyAudits: ['unknown-audit'],
7163
}),
72-
).toThrow('invalid_type');
73-
});
74-
75-
it('throws for unknown audit slug', () => {
76-
expect(
77-
() =>
78-
typescriptPluginConfigSchema.parse({
79-
tsConfigPath,
80-
onlyAudits: ['unknown-audit'],
81-
}),
82-
// Message too large because enums validation
83-
// eslint-disable-next-line vitest/require-to-throw-message
84-
).toThrow();
85-
});
64+
// Message too large because enums validation
65+
// eslint-disable-next-line vitest/require-to-throw-message
66+
).toThrow();
8667
});
8768
});

packages/plugin-typescript/src/lib/typescript-plugin.unit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { AUDITS, GROUPS } from './constants.js';
44
import { typescriptPlugin } from './typescript-plugin.js';
55

66
describe('typescriptPlugin-config-object', () => {
7-
it('should create valid plugin config', async () => {
7+
it.skip('should create valid plugin config', async () => {
88
const pluginConfig = await typescriptPlugin({
99
tsConfigPath:
1010
'packages/plugin-typescript/mocks/fixtures/basic-setup/tsconfig.json',

packages/plugin-typescript/src/lib/utils.integration.test.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

packages/plugin-typescript/src/lib/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export function filterAuditsByCompilerOptions(
5454
return ({ slug }: { slug: string }) => {
5555
const option = compilerOptions[auditSlugToCompilerOption(slug)];
5656
return (
57-
option !== false && option !== undefined && filterAuditsBySlug(onlyAudits)
57+
option !== false &&
58+
option !== undefined &&
59+
filterAuditsBySlug(onlyAudits)({ slug })
5860
);
5961
};
6062
}

packages/plugin-typescript/src/lib/utils.unit.test.ts

Lines changed: 80 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,90 @@ import { describe, expect, it, vi } from 'vitest';
33
import type { Audit } from '@code-pushup/models';
44
import { AUDITS } from './constants.js';
55
import {
6+
filterAuditsByCompilerOptions,
67
filterAuditsBySlug,
78
handleCompilerOptionStrict,
89
validateAudits,
910
} from './utils.js';
1011

1112
describe('filterAuditsBySlug', () => {
12-
const mockAudits: Audit[] = [
13-
{ slug: 'test-1', title: 'Test 1' },
14-
{ slug: 'test-2', title: 'Test 2' },
15-
{ slug: 'test-3', title: 'Test 3' },
16-
];
17-
18-
it.each([
19-
[undefined, mockAudits, [true, true, true]],
20-
[[], mockAudits, [true, true, true]],
21-
[['test-1', 'test-2'], mockAudits, [true, true, false]],
22-
])(
23-
'should filter audits correctly when slugs is %p',
24-
(slugs, audits, expected) => {
25-
const filter = filterAuditsBySlug(slugs);
26-
audits.forEach((audit, index) => {
27-
expect(filter(audit)).toBe(expected[index]);
28-
});
29-
},
30-
);
13+
const mockAudit = { slug: 'strict-function-types' } as Audit;
14+
15+
it('should return true if slugs are undefined', () => {
16+
expect(filterAuditsBySlug(undefined)(mockAudit)).toBe(true);
17+
});
18+
19+
it('should return true if slugs are empty', () => {
20+
expect(filterAuditsBySlug([])(mockAudit)).toBe(true);
21+
});
22+
23+
it('should return true if slugs are including the current audit slug', () => {
24+
expect(filterAuditsBySlug(['strict-function-types'])(mockAudit)).toBe(true);
25+
});
26+
27+
it('should return false if slugs are not including the current audit slug', () => {
28+
expect(filterAuditsBySlug(['verbatim-module-syntax'])(mockAudit)).toBe(
29+
false,
30+
);
31+
});
32+
});
33+
34+
describe('filterAuditsByCompilerOptions', () => {
35+
it('should return false if the audit is false in compiler options', () => {
36+
expect(
37+
filterAuditsByCompilerOptions(
38+
{
39+
strictFunctionTypes: false,
40+
},
41+
['strict-function-types'],
42+
)({ slug: 'strict-function-types' }),
43+
).toBe(false);
44+
});
45+
46+
it('should return false if the audit is undefined in compiler options', () => {
47+
expect(
48+
filterAuditsByCompilerOptions(
49+
{
50+
strictFunctionTypes: undefined,
51+
},
52+
['strict-function-types'],
53+
)({ slug: 'strict-function-types' }),
54+
).toBe(false);
55+
});
56+
57+
it('should return false if the audit is enabled in compiler options but not in onlyAudits', () => {
58+
const onlyAudits = ['strict-null-checks'];
59+
expect(
60+
filterAuditsByCompilerOptions(
61+
{
62+
strictFunctionTypes: true,
63+
},
64+
onlyAudits,
65+
)({ slug: 'strict-function-types' }),
66+
).toBe(false);
67+
});
68+
69+
it('should return true if the audit is enabled in compiler options and onlyAudits is empty', () => {
70+
expect(
71+
filterAuditsByCompilerOptions(
72+
{
73+
strictFunctionTypes: true,
74+
},
75+
[],
76+
)({ slug: 'strict-function-types' }),
77+
).toBe(true);
78+
});
79+
80+
it('should return true if the audit is enabled in compiler options and in onlyAudits', () => {
81+
expect(
82+
filterAuditsByCompilerOptions(
83+
{
84+
strictFunctionTypes: true,
85+
},
86+
['strict-function-types'],
87+
)({ slug: 'strict-function-types' }),
88+
).toBe(true);
89+
});
3190
});
3291

3392
describe('handleCompilerOptionStrict', () => {
@@ -38,7 +97,7 @@ describe('handleCompilerOptionStrict', () => {
3897
};
3998

4099
const result = handleCompilerOptionStrict(options);
41-
expect(result).toEqual(options);
100+
expect(result).toB(options);
42101
});
43102

44103
it('should add all strict options when strict is true', () => {
@@ -49,7 +108,7 @@ describe('handleCompilerOptionStrict', () => {
49108

50109
const result = handleCompilerOptionStrict(options);
51110

52-
expect(result).toEqual({
111+
expect(result).toStrictEqual({
53112
...options,
54113
noImplicitAny: true,
55114
noImplicitThis: true,

0 commit comments

Comments
 (0)