Skip to content

Commit

Permalink
test(core): fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Mar 8, 2024
1 parent 72a04c2 commit 28717cf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
4 changes: 1 addition & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"@code-pushup/models": "*",
"@code-pushup/utils": "*",
"@code-pushup/portal-client": "^0.6.1",
"chalk": "^5.3.0",
"memfs": "^4.5.0",
"vitest": "1.3.1"
"chalk": "^5.3.0"
},
"type": "commonjs",
"main": "./index.cjs"
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/lib/implementation/execute-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ export async function executePlugins(

progressBar?.endProgress('Done running plugins');

const errorsTransform = ({ reason }: PromiseRejectedResult) =>
String(reason);
const errorsTransform = ({ reason }: PromiseRejectedResult) => String(reason);
const results = await Promise.allSettled(pluginsResult);

logMultipleResults(results, 'Plugins', undefined, errorsTransform);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,20 @@ import {
executePlugins,
} from './execute-plugin';

const nodePluginSlug = MINIMAL_PLUGIN_CONFIG_MOCK.slug;

// eslint-disable-next-line max-lines-per-function
describe('executePlugin', () => {
it('should execute a valid plugin config', async () => {
const pluginResult = await executePlugin(MINIMAL_PLUGIN_CONFIG_MOCK);
expect(pluginResult.audits[0]?.slug).toBe(nodePluginSlug);
expect(pluginResult.audits[0]?.slug).toBe('node-version');
});

it('should yield audit outputs for valid runner config', async () => {
vol.fromJSON(
{
'output.json': JSON.stringify([
{
slug: nodePluginSlug,
slug: 'node-version',
score: 1,
value: 2,
value: 1,
},
]),
},
Expand All @@ -43,26 +40,26 @@ describe('executePlugin', () => {
outputFile: 'output.json',
},
});
expect(pluginResult.audits[0]?.slug).toBe(nodePluginSlug);
expect(pluginResult.audits[0]?.slug).toBe('node-version');
});

it('should yield audit outputs for a valid runner function', async () => {
const pluginResult = await executePlugin({
...MINIMAL_PLUGIN_CONFIG_MOCK,
runner: () => [
{
slug: nodePluginSlug,
slug: 'node-version',
score: 0,
value: 1,
},
],
});
expect(pluginResult.audits).toEqual([
expect.objectContaining({
slug: nodePluginSlug,
slug: 'node-version',
title: 'Node version',
score: 0,
value: 2,
value: 1,
}),
]);
});
Expand All @@ -73,7 +70,7 @@ describe('executePlugin', () => {
...MINIMAL_PLUGIN_CONFIG_MOCK,
audits: [{ slug: '-invalid-slug', title: 'Invalid audit' }],
}),
).rejects.toThrow(new PluginOutputMissingAuditError(nodePluginSlug));
).rejects.toThrow(new PluginOutputMissingAuditError('node-version'));
});

it('should throw if invalid runnerOutput is produced', async () => {
Expand All @@ -92,7 +89,6 @@ describe('executePlugin', () => {
});
});

// eslint-disable-next-line max-lines-per-function
describe('executePlugins', () => {
it('should execute valid plugins', async () => {
const pluginResult = await executePlugins(
Expand All @@ -108,7 +104,7 @@ describe('executePlugins', () => {

expect(pluginResult[0]?.icon).toBe('javascript');
expect(pluginResult[1]?.icon).toBe('nodejs');
expect(pluginResult[0]?.audits[0]?.slug).toBe(nodePluginSlug);
expect(pluginResult[0]?.audits[0]?.slug).toBe('node-version');
});

it('should throw for invalid plugins', async () => {
Expand All @@ -131,7 +127,9 @@ describe('executePlugins', () => {
it('should print invalid plugin errors and throw', async () => {
const pluginConfig = {
...MINIMAL_PLUGIN_CONFIG_MOCK,
runner: vi.fn().mockRejectedValue('plugin 1 error'),
runner: vi
.fn()
.mockRejectedValue('Audit metadata not found for slug node-version'),
};
const pluginConfig2 = {
...MINIMAL_PLUGIN_CONFIG_MOCK,
Expand All @@ -147,15 +145,17 @@ describe('executePlugins', () => {
progress: false,
}),
).rejects.toThrow(
'Plugins failed: 2 errors: plugin 1 error, plugin 3 error',
'Plugins failed: 2 errors: Audit metadata not found for slug node-version, plugin 3 error',
);
const logs = ui()
.logger.getRenderer()
.getLogs()
.map(({ message }) => message);
expect(logs[0]).toBe('[ yellow(warn) ] Plugins failed: ');
expect(logs[1]).toBe('[ yellow(warn) ] plugin 1 error');
expect(logs[2]).toBe('[ yellow(warn) ] plugin 3 error');
expect(logs[1]).toBe(
'[ yellow(warn) ] Audit metadata not found for slug node-version',
);

expect(pluginConfig.runner).toHaveBeenCalled();
expect(pluginConfig2.runner).toHaveBeenCalled();
expect(pluginConfig3.runner).toHaveBeenCalled();
Expand All @@ -166,9 +166,9 @@ describe('executePlugins', () => {
{
'output.json': JSON.stringify([
{
slug: nodePluginSlug,
slug: 'node-version',
score: 1,
value: 2,
value: 1,
},
]),
},
Expand All @@ -188,7 +188,7 @@ describe('executePlugins', () => {
{
slug: (outputs as AuditOutputs)[0]!.slug,
score: 1,
value: 2,
value: 1,
displayValue: '2.0.0',
},
]),
Expand All @@ -197,7 +197,7 @@ describe('executePlugins', () => {
],
{ progress: false },
);
expect(pluginResult[0]?.audits[0]?.slug).toBe(nodePluginSlug);
expect(pluginResult[0]?.audits[0]?.slug).toBe('node-version');
expect(pluginResult[0]?.audits[0]?.displayValue).toBe('2.0.0');
});
});

0 comments on commit 28717cf

Please sign in to comment.