Skip to content

Commit

Permalink
test(ci-e2e): set up nx monorepo fixture and test push event
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Dec 16, 2024
1 parent 6a6c2f3 commit 701598b
Show file tree
Hide file tree
Showing 22 changed files with 246 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { resolveConfig } from '../../code-pushup.preset.js';

export default resolveConfig(import.meta.url);
9 changes: 9 additions & 0 deletions e2e/ci-e2e/mocks/fixtures/nx-monorepo/apps/api/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "api",
"projectType": "application",
"targets": {
"code-pushup": {
"command": "npx @code-pushup/cli --no-progress --config=apps/api/code-pushup.config.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello, world!');
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { resolveConfig } from '../../code-pushup.preset.js';

export default resolveConfig(import.meta.url);
9 changes: 9 additions & 0 deletions e2e/ci-e2e/mocks/fixtures/nx-monorepo/apps/cms/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "cms",
"projectType": "application",
"targets": {
"code-pushup": {
"command": "npx @code-pushup/cli --no-progress --config=apps/cms/code-pushup.config.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello, world!');
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { resolveConfig } from '../../code-pushup.preset.js';

export default resolveConfig(import.meta.url);
9 changes: 9 additions & 0 deletions e2e/ci-e2e/mocks/fixtures/nx-monorepo/apps/web/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "web",
"projectType": "application",
"targets": {
"code-pushup": {
"command": "npx @code-pushup/cli --no-progress --config=apps/web/code-pushup.config.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello, world!');
66 changes: 66 additions & 0 deletions e2e/ci-e2e/mocks/fixtures/nx-monorepo/code-pushup.preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { crawlFileSystem } from '@code-pushup/utils';

export function resolveConfig(url) {
const directory = fileURLToPath(dirname(url));
return {
persist: {
outputDir: join(directory, '.code-pushup'),
},
plugins: [
{
slug: 'ts-migration',
title: 'TypeScript migration',
icon: 'typescript',
audits: [
{
slug: 'ts-files',
title: 'Source files converted from JavaScript to TypeScript',
},
],
runner: async () => {
const paths = await crawlFileSystem({
directory,
pattern: /\.[jt]s$/,
});
const jsPaths = paths.filter(path => path.endsWith('.js'));
const tsPaths = paths.filter(path => path.endsWith('.ts'));
const jsFileCount = jsPaths.length;
const tsFileCount = tsPaths.length;
const ratio = tsFileCount / (jsFileCount + tsFileCount);
const percentage = Math.round(ratio * 100);
return [
{
slug: 'ts-files',
value: percentage,
score: ratio,
displayValue: `${percentage}% converted`,
details: {
issues: jsPaths.map(file => ({
message: 'Use .ts file extension instead of .js',
severity: 'warning',
source: { file },
})),
},
},
];
},
},
],
categories: [
{
slug: 'ts-migration',
title: 'TypeScript migration',
refs: [
{
type: 'audit',
plugin: 'ts-migration',
slug: 'ts-files',
weight: 1,
},
],
},
],
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { resolveConfig } from '../../code-pushup.preset.js';

export default resolveConfig(import.meta.url);
9 changes: 9 additions & 0 deletions e2e/ci-e2e/mocks/fixtures/nx-monorepo/libs/ui/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "ui",
"projectType": "library",
"targets": {
"code-pushup": {
"command": "npx @code-pushup/cli --no-progress --config=libs/ui/code-pushup.config.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import assert from 'node:assert';
import test from 'node:test';

test('1984', () => {
assert.equal(2 + 2, 5);
});
1 change: 1 addition & 0 deletions e2e/ci-e2e/mocks/fixtures/nx-monorepo/libs/ui/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello, world!');
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { resolveConfig } from '../../code-pushup.preset.js';

export default resolveConfig(import.meta.url);
9 changes: 9 additions & 0 deletions e2e/ci-e2e/mocks/fixtures/nx-monorepo/libs/utils/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "utils",
"projectType": "library",
"targets": {
"code-pushup": {
"command": "npx @code-pushup/cli --no-progress --config=libs/utils/code-pushup.config.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello, world!');
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import assert from 'node:assert';
import test from 'node:test';

test('1984', () => {
assert.equal(2 + 2, 5);
});
1 change: 1 addition & 0 deletions e2e/ci-e2e/mocks/fixtures/nx-monorepo/nx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 0 additions & 2 deletions e2e/ci-e2e/mocks/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export async function setupTestRepo(folder: string) {
TEST_OUTPUT_DIR,
folder,
);
const outputDir = join(baseDir, '.code-pushup');

await cp(fixturesDir, baseDir, { recursive: true });

Expand All @@ -43,7 +42,6 @@ export async function setupTestRepo(folder: string) {
return {
git,
baseDir,
outputDir,
cleanup: () => teardownTestFolder(baseDir),
};
}
Expand Down
19 changes: 11 additions & 8 deletions e2e/ci-e2e/tests/basic.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ describe('CI - standalone mode', () => {
mode: 'standalone',
files: {
report: {
json: join(repo.outputDir, 'report.json'),
md: join(repo.outputDir, 'report.md'),
json: join(repo.baseDir, '.code-pushup/report.json'),
md: join(repo.baseDir, '.code-pushup/report.md'),
},
},
} satisfies RunResult);

const jsonPromise = readFile(join(repo.outputDir, 'report.json'), 'utf8');
const jsonPromise = readFile(
join(repo.baseDir, '.code-pushup/report.json'),
'utf8',
);
await expect(jsonPromise).resolves.toBeTruthy();
const report = JSON.parse(await jsonPromise) as Report;
expect(report).toEqual(
Expand Down Expand Up @@ -98,18 +101,18 @@ describe('CI - standalone mode', () => {
newIssues: [],
files: {
report: {
json: join(repo.outputDir, 'report.json'),
md: join(repo.outputDir, 'report.md'),
json: join(repo.baseDir, '.code-pushup/report.json'),
md: join(repo.baseDir, '.code-pushup/report.md'),
},
diff: {
json: join(repo.outputDir, 'report-diff.json'),
md: join(repo.outputDir, 'report-diff.md'),
json: join(repo.baseDir, '.code-pushup/report-diff.json'),
md: join(repo.baseDir, '.code-pushup/report-diff.md'),
},
},
} satisfies RunResult);

const mdPromise = readFile(
join(repo.outputDir, 'report-diff.md'),
join(repo.baseDir, '.code-pushup/report-diff.md'),
'utf8',
);
await expect(mdPromise).resolves.toBeTruthy();
Expand Down
91 changes: 91 additions & 0 deletions e2e/ci-e2e/tests/nx-monorepo.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { join } from 'node:path';
import type { SimpleGit } from 'simple-git';
import { afterEach } from 'vitest';
import { type Options, type RunResult, runInCI } from '@code-pushup/ci';
import { readJsonFile } from '@code-pushup/utils';
import { MOCK_API } from '../mocks/api';
import { type TestRepo, setupTestRepo } from '../mocks/setup';

describe('CI - monorepo mode (Nx)', () => {
let repo: TestRepo;
let git: SimpleGit;
let options: Options;

beforeEach(async () => {
repo = await setupTestRepo('nx-monorepo');
git = repo.git;
options = {
monorepo: true,
directory: repo.baseDir,
};
});

afterEach(async () => {
await repo.cleanup();
});

describe('push event', () => {
beforeEach(async () => {
await git.checkout('main');
});

it('should collect reports for all projects', async () => {
await expect(
runInCI(
{ head: { ref: 'main', sha: await git.revparse('main') } },
MOCK_API,
options,
git,
),
).resolves.toEqual({
mode: 'monorepo',
projects: expect.arrayContaining([
{
name: 'api',
files: {
report: {
json: join(repo.baseDir, 'apps/api/.code-pushup/report.json'),
md: join(repo.baseDir, 'apps/api/.code-pushup/report.md'),
},
},
},
]),
} satisfies RunResult);

await expect(
readJsonFile(join(repo.baseDir, 'apps/api/.code-pushup/report.json')),
).resolves.toEqual(
expect.objectContaining({
plugins: [
expect.objectContaining({
audits: [
expect.objectContaining({
score: 0,
displayValue: '0% converted',
}),
],
}),
],
}),
);
await expect(
readJsonFile(join(repo.baseDir, 'libs/ui/.code-pushup/report.json')),
).resolves.toEqual(
expect.objectContaining({
plugins: [
expect.objectContaining({
audits: [
expect.objectContaining({
score: expect.closeTo(0.666),
displayValue: '67% converted',
}),
],
}),
],
}),
);
});
});

// TODO: pull request event
});

0 comments on commit 701598b

Please sign in to comment.