Skip to content

Commit

Permalink
test(examples-plugins): convert integration tests to e2e (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton authored Feb 29, 2024
1 parent ec7ee5e commit 90c839c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 73 deletions.
1 change: 0 additions & 1 deletion examples/plugins/src/lighthouse/mock/constants.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { vol } from 'memfs';
import { beforeEach, describe, expect, it } from 'vitest';
import { executePlugin } from '@code-pushup/core';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
import {
auditSchema,
categoryRefSchema,
pluginConfigSchema,
} from '@code-pushup/models';
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
import { LIGHTHOUSE_URL } from '../mock/constants';
import { lhr } from '../mock/fixtures/lhr';
import { LIGHTHOUSE_OUTPUT_FILE_DEFAULT, corePerfGroupRefs } from './constants';
import { corePerfGroupRefs } from './constants';
import { audits, PLUGIN_SLUG as slug } from './index';
import { create } from './lighthouse.plugin';

describe('lighthouse-create-export-config', () => {
it('should return valid PluginConfig if create is called', async () => {
const pluginConfig = await create({ url: LIGHTHOUSE_URL });
const pluginConfig = await create({ url: 'http://localhost:8080' });
expect(() => pluginConfigSchema.parse(pluginConfig)).not.toThrow();
expect(pluginConfig).toEqual({
slug,
Expand Down Expand Up @@ -51,7 +47,7 @@ describe('lighthouse-create-export-config', () => {

it('should parse options for headless by default to "new" in runner args', async () => {
const pluginConfig = await create({
url: LIGHTHOUSE_URL,
url: 'http://localhost:8080',
});
expect(pluginConfig.runner.args).toEqual(
expect.arrayContaining(['--chrome-flags="--headless=new"']),
Expand All @@ -60,7 +56,7 @@ describe('lighthouse-create-export-config', () => {

it('should parse options for headless to new if true is given in runner args', async () => {
const pluginConfig = await create({
url: LIGHTHOUSE_URL,
url: 'http://localhost:8080',
headless: true,
});
expect(pluginConfig.runner.args).toEqual(
Expand All @@ -70,7 +66,7 @@ describe('lighthouse-create-export-config', () => {

it('should parse options for headless to new if false is given in runner args', async () => {
const pluginConfig = await create({
url: LIGHTHOUSE_URL,
url: 'http://localhost:8080',
headless: false,
});
expect(pluginConfig.runner.args).toEqual(
Expand All @@ -80,7 +76,7 @@ describe('lighthouse-create-export-config', () => {

it('should override userDataDir option when given in runner args', async () => {
const pluginConfig = await create({
url: LIGHTHOUSE_URL,
url: 'http://localhost:8080',
userDataDir: 'test',
});
expect(pluginConfig.runner.args).toEqual(
Expand All @@ -89,49 +85,24 @@ describe('lighthouse-create-export-config', () => {
]),
);
});
});

describe('lighthouse-create-export-execution', () => {
beforeEach(() => {
vol.fromJSON(
{
[LIGHTHOUSE_OUTPUT_FILE_DEFAULT]: JSON.stringify(lhr),
},
MEMFS_VOLUME,
);
});

// TODO: Convert to E2E test or reduce scope, it takes too long to run these tests
/* eslint-disable vitest/no-disabled-tests */
it.skip('should return PluginConfig that executes correctly', async () => {
const pluginConfig = await create({ url: LIGHTHOUSE_URL });
await expect(executePlugin(pluginConfig)).resolves.toMatchObject(
expect.objectContaining({
slug,
title: 'Lighthouse',
description: 'Chrome lighthouse CLI as code-pushup plugin',
duration: expect.any(Number),
date: expect.any(String),
audits: expect.any(Array),
groups: expect.any(Array),
}),
);
});

it.skip('should use onlyAudits', async () => {
it('should use onlyAudits', async () => {
const pluginConfig = await create({
url: LIGHTHOUSE_URL,
url: 'http://localhost:8080',
outputPath: `${join('tmp', 'lighthouse-report.json')}`,
onlyAudits: 'largest-contentful-paint',
});
expect(pluginConfig.runner.args).toEqual(
expect.arrayContaining(['--onlyAudits="largest-contentful-paint"']),
);
const { audits: auditOutputs } = await executePlugin(pluginConfig);

expect(auditOutputs).toHaveLength(1);
expect(auditOutputs[0]?.slug).toBe('largest-contentful-paint');
expect(pluginConfig).toStrictEqual(
expect.objectContaining({
audits: expect.arrayContaining([
expect.objectContaining({ slug: 'largest-contentful-paint' }),
]),
}),
);
});
/* eslint-enable vitest/no-disabled-tests */
});

describe('lighthouse-audits-export', () => {
Expand Down
38 changes: 17 additions & 21 deletions examples/plugins/src/lighthouse/src/lighthouse.plugin.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
import { describe, expect, it } from 'vitest';
import { LIGHTHOUSE_URL } from '../mock/constants';
import { LIGHTHOUSE_OUTPUT_FILE_DEFAULT } from './constants';
import { runnerConfig } from './lighthouse.plugin';
import type { LighthouseCliOptions } from './types';

describe('lighthouse-runnerConfig', () => {
const baseOptions: LighthouseCliOptions = {
url: LIGHTHOUSE_URL,
};
const lcpAuditOutputBase = {
displayValue: expect.stringContaining('sec'),
score: 1,
slug: 'largest-contentful-paint',
value: 0,
};

it('should execute if url is given', () => {
expect(runnerConfig(baseOptions)).toEqual(
expect(
runnerConfig({
url: 'http://localhost:8080',
}),
).toEqual(
expect.objectContaining({
args: expect.arrayContaining(['lighthouse', LIGHTHOUSE_URL]),
args: expect.arrayContaining(['lighthouse', 'http://localhost:8080']),
command: 'npx',
outputFile: LIGHTHOUSE_OUTPUT_FILE_DEFAULT,
}),
);
});

it('should execute with output "json" and output-path "lighthouse-report.json" by default', () => {
expect(runnerConfig(baseOptions)).toEqual(
expect(
runnerConfig({
url: 'http://localhost:8080',
}),
).toEqual(
expect.objectContaining({
args: expect.arrayContaining([
'--output="json"',
Expand All @@ -42,21 +38,21 @@ describe('lighthouse-runnerConfig', () => {
it('should run only audits included in given onlyAudits', () => {
expect(
runnerConfig({
...baseOptions,
onlyAudits: [lcpAuditOutputBase.slug],
url: 'http://localhost:8080',
onlyAudits: ['largest-contentful-paint'],
}),
).toEqual(
expect.objectContaining({
args: expect.arrayContaining([
`--onlyAudits="${lcpAuditOutputBase.slug}"`,
'--onlyAudits="largest-contentful-paint"',
]),
}),
);
});

it('should parse options for headless by default to false', () => {
const args = runnerConfig({
url: LIGHTHOUSE_URL,
url: 'http://localhost:8080',
});
expect(args).toEqual(
expect.not.arrayContaining(['--chrome-flags="--headless=new"']),
Expand All @@ -66,12 +62,12 @@ describe('lighthouse-runnerConfig', () => {
it('should run headless "new" if set to true', () => {
expect(
runnerConfig({
...baseOptions,
url: 'http://localhost:8080',
headless: 'new',
}),
).toEqual(
expect.objectContaining({
args: expect.arrayContaining([`--chrome-flags="--headless=new"`]),
args: expect.arrayContaining(['--chrome-flags="--headless=new"']),
}),
);
});
Expand Down
7 changes: 3 additions & 4 deletions examples/plugins/src/lighthouse/src/utils.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { describe, expect, it } from 'vitest';
import { LIGHTHOUSE_URL } from '../mock/constants';
import { getLighthouseCliArguments } from './utils';

describe('getLighthouseCliArguments', () => {
Expand All @@ -13,7 +12,7 @@ describe('getLighthouseCliArguments', () => {

it('should parse options for headless to new if true is given', () => {
const args = getLighthouseCliArguments({
url: LIGHTHOUSE_URL,
url: 'http://localhost:8080',
headless: 'new',
});
expect(args).toEqual(
Expand All @@ -23,7 +22,7 @@ describe('getLighthouseCliArguments', () => {

it('should not include options for headless if false is given', () => {
const args = getLighthouseCliArguments({
url: LIGHTHOUSE_URL,
url: 'http://localhost:8080',
headless: false,
});
expect(args).toEqual(
Expand All @@ -33,7 +32,7 @@ describe('getLighthouseCliArguments', () => {

it('should use userDataDir option in chrome flags when given', () => {
const args = getLighthouseCliArguments({
url: LIGHTHOUSE_URL,
url: 'http://localhost:8080',
userDataDir: 'test',
});
expect(args).toEqual(
Expand Down

0 comments on commit 90c839c

Please sign in to comment.