Skip to content

Commit

Permalink
test: refactor old tests and cleanup test helpers (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton authored Nov 25, 2024
1 parent 3db30da commit cf29bc3
Show file tree
Hide file tree
Showing 89 changed files with 661 additions and 4,385 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ jobs:
- name: Install dependencies
run: npm ci
- name: E2E test affected projects
run: npx nx affected -t nxv-e2e --exclude cli-e2e --parallel=1
- name: E2E test cli-e2e project (due to bugs in the setup it has to run last :( )
run: npx nx run cli-e2e:e2e-old
run: npx nx affected -t nxv-e2e --parallel=1

build:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion code-pushup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const config: CoreConfig = {

plugins: [
fileSizePlugin({
directory: './dist/examples/react-todos-app',
directory: './dist/packages',
pattern: /\.js$/,
budget: 174_080, // 170 kB
}),
Expand Down
1 change: 1 addition & 0 deletions e2e/ci-e2e/mocks/fixtures/ci-test-repo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hello, world!")
56 changes: 29 additions & 27 deletions e2e/ci-e2e/tests/ci.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
copyFile,
mkdir,
readFile,
rename,
rm,
writeFile,
} from 'node:fs/promises';
import { cp, readFile, rename } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import {
Expand All @@ -14,6 +7,7 @@ import {
type SimpleGit,
simpleGit,
} from 'simple-git';
import { afterEach } from 'vitest';
import {
type Comment,
type GitRefs,
Expand All @@ -22,41 +16,42 @@ import {
type RunResult,
runInCI,
} from '@code-pushup/ci';
import { initGitRepo } from '@code-pushup/test-utils';
import { nxTargetProject } from '@code-pushup/test-nx-utils';
import { teardownTestFolder } from '@code-pushup/test-setup';
import {
E2E_ENVIRONMENTS_DIR,
TEST_OUTPUT_DIR,
TEST_SNAPSHOTS_DIR,
initGitRepo,
} from '@code-pushup/test-utils';

describe('CI package', () => {
const fixturesDir = join(
fileURLToPath(dirname(import.meta.url)),
'..',
'mocks',
'fixtures',
'ci-test-repo',
);
const workDir = join(
const ciSetupRepoDir = join(
process.cwd(),
'tmp',
'e2e',
'ci-e2e',
'__test__',
E2E_ENVIRONMENTS_DIR,
nxTargetProject(),
TEST_OUTPUT_DIR,
'ci-test-repo',
);
const outputDir = join(workDir, '.code-pushup');
const outputDir = join(ciSetupRepoDir, '.code-pushup');

const options = {
directory: workDir,
directory: ciSetupRepoDir,
} satisfies Options;

let git: SimpleGit;

beforeEach(async () => {
await rm(workDir, { recursive: true, force: true });
await mkdir(workDir, { recursive: true });
await copyFile(
join(fixturesDir, 'code-pushup.config.ts'),
join(workDir, 'code-pushup.config.ts'),
);
await writeFile(join(workDir, 'index.js'), 'console.log("Hello, world!")');
await cp(fixturesDir, ciSetupRepoDir, { recursive: true });

git = await initGitRepo(simpleGit, { baseDir: workDir });
git = await initGitRepo(simpleGit, { baseDir: ciSetupRepoDir });

vi.spyOn(git, 'fetch').mockResolvedValue({} as FetchResult);
vi.spyOn(git, 'diffSummary').mockResolvedValue({
Expand All @@ -69,8 +64,12 @@ describe('CI package', () => {
await git.commit('Initial commit');
});

afterEach(async () => {
await teardownTestFolder(ciSetupRepoDir);
});

afterAll(async () => {
await rm(workDir, { recursive: true, force: true });
await teardownTestFolder(ciSetupRepoDir);
});

describe('push event', () => {
Expand Down Expand Up @@ -138,7 +137,10 @@ describe('CI package', () => {
beforeEach(async () => {
await git.checkoutLocalBranch('feature-1');

await rename(join(workDir, 'index.js'), join(workDir, 'index.ts'));
await rename(
join(ciSetupRepoDir, 'index.js'),
join(ciSetupRepoDir, 'index.ts'),
);

await git.add('index.ts');
await git.commit('Convert JS file to TS');
Expand Down Expand Up @@ -177,7 +179,7 @@ describe('CI package', () => {
const md = await mdPromise;
await expect(
md.replace(/[\da-f]{40}/g, '`<commit-sha>`'),
).toMatchFileSnapshot('__snapshots__/report-diff.md');
).toMatchFileSnapshot(join(TEST_SNAPSHOTS_DIR, 'report-diff.md'));
});
});
});
44 changes: 0 additions & 44 deletions e2e/cli-e2e/mocks/fixtures/code-pushup.config.js

This file was deleted.

44 changes: 0 additions & 44 deletions e2e/cli-e2e/mocks/fixtures/code-pushup.config.mjs

This file was deleted.

45 changes: 0 additions & 45 deletions e2e/cli-e2e/mocks/fixtures/code-pushup.config.ts

This file was deleted.

6 changes: 6 additions & 0 deletions e2e/cli-e2e/mocks/fixtures/dummy-setup/code-pushup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import dummyPlugin, { dummyCategory } from './dummy.plugin';

export default {
plugins: [dummyPlugin()],
categories: [dummyCategory],
};
6 changes: 6 additions & 0 deletions e2e/cli-e2e/mocks/fixtures/dummy-setup/code-pushup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import dummyPlugin, { dummyCategory } from './dummy.plugin';

export default {
plugins: [dummyPlugin()],
categories: [dummyCategory],
};
6 changes: 6 additions & 0 deletions e2e/cli-e2e/mocks/fixtures/dummy-setup/code-pushup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import dummyPlugin, { dummyCategory } from './dummy.plugin';

export default {
plugins: [dummyPlugin()],
categories: [dummyCategory],
};
50 changes: 50 additions & 0 deletions e2e/cli-e2e/mocks/fixtures/dummy-setup/dummy.plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import type { PluginConfig } from '@code-pushup/models';

export const dummyPluginSlug = 'dummy-plugin';

const dummyAuditSlug = 'dummy-audit';
export const dummyAudit = {
slug: dummyAuditSlug,
title: 'Dummy Audit',
description: 'A dummy audit to test the cli.',
};

export const dummyCategory = {
slug: 'dummy-category',
title: 'Dummy Category',
refs: [
{
type: 'audit',
plugin: dummyPluginSlug,
slug: dummyAuditSlug,
weight: 1,
},
],
};

export function create(): PluginConfig {
return {
slug: dummyPluginSlug,
title: 'Dummy Plugin',
icon: 'folder-javascript',
description: 'A dummy plugin to test the cli.',
runner: async () => {
const itemCount = JSON.parse(
await readFile(join('src', 'items.json'), 'utf-8'),
).length;
return [
{
...dummyAudit,
slug: dummyAuditSlug,
score: itemCount < 10 ? itemCount / 10 : 1,
value: itemCount,
},
];
},
audits: [dummyAudit],
};
}

export default create;
1 change: 1 addition & 0 deletions e2e/cli-e2e/mocks/fixtures/dummy-setup/src/items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1, 2, 3]
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"commit": {
"hash": "11f3153b8df2f5e651a1c72d3ddde3a0f400b8a2",
"message": "Merge branch 'main' into split-compare-e2e",
"date": "2024-11-19T14:28:43.000Z",
"author": "Michael"
},
"packageName": "@code-pushup/core",
"version": "0.54.0",
"date": "2024-11-19T14:50:29.040Z",
"duration": 64,
"categories": [
{
"slug": "dummy-category",
"refs": [
{
"slug": "dummy-audit",
"weight": 1,
"type": "audit",
"plugin": "dummy-plugin"
}
],
"title": "Dummy Category"
}
],
"plugins": [
{
"title": "Dummy Plugin",
"slug": "dummy-plugin",
"icon": "folder-javascript",
"date": "2024-11-19T14:50:29.080Z",
"duration": 1,
"audits": [
{
"slug": "dummy-audit",
"value": 3,
"score": 0.3,
"title": "Dummy Audit",
"description": "A dummy audit to test the cli."
}
],
"description": "A dummy plugin to test the cli."
}
]
}
Loading

0 comments on commit cf29bc3

Please sign in to comment.