Skip to content

Commit 7895211

Browse files
committed
test: add tests for formatters and e2e
1 parent bcd8be9 commit 7895211

7 files changed

+170
-11
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,5 @@ dist
118118
.DS_Store
119119

120120
.note/
121+
122+
vf-core-service-discovery-e2e-projects/

e2e/basic.test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import path from 'path';
2+
import test from 'ava';
3+
import execa from 'execa';
4+
import rimraf from 'rimraf';
5+
import { promisify } from 'util';
6+
7+
const rimrafP = promisify(rimraf);
8+
9+
test.serial.before(async () => {
10+
await rimrafP('vf-core-service-discovery-e2e-projects');
11+
await execa('git', ['clone', 'https://github.com/oss6/vf-core-service-discovery-e2e-projects']);
12+
});
13+
14+
test.serial('basic e2e', async (t) => {
15+
// arrange
16+
const testExampleDirectory = path.join('vf-core-service-discovery-e2e-projects', 'basic');
17+
const tsNodeFileName = path.resolve(path.join('node_modules', '.bin', 'ts-node'));
18+
const cliFileName = path.resolve(path.join('src', 'cli', 'index.ts'));
19+
const tsConfigFileName = path.resolve('tsconfig.json');
20+
21+
// act
22+
const { stdout } = await execa(tsNodeFileName, ['--project', tsConfigFileName, cliFileName, 'run'], {
23+
cwd: testExampleDirectory,
24+
});
25+
26+
// assert
27+
t.true(stdout.includes('vf-box'));
28+
t.true(stdout.includes('vf-footer'));
29+
t.true(stdout.includes('vf-grid'));
30+
});

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"eslint": "^7.15.0",
5151
"eslint-config-prettier": "^7.0.0",
5252
"eslint-plugin-prettier": "^3.3.0",
53+
"execa": "^5.0.0",
5354
"fetch-mock": "^9.11.0",
5455
"husky": "^4.3.8",
5556
"nyc": "^15.1.0",
@@ -79,8 +80,10 @@
7980
},
8081
"ava": {
8182
"files": [
83+
"e2e/**",
8284
"test/**",
83-
"!test/fixture/**"
85+
"!test/fixture/**",
86+
"!e2e/vf-core-service-discovery-e2e-projects/**"
8487
],
8588
"extensions": [
8689
"ts"

src/helpers/string.ts

-10
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,3 @@ export class StringBuilder {
2626
return this.value;
2727
}
2828
}
29-
30-
// const stringBuilder = {
31-
// value: '',
32-
// add(str: string): void {
33-
// this.value += str;
34-
// },
35-
// reset(): void {
36-
// this.value = '';
37-
// },
38-
// };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import test from 'ava';
2+
import { StringBuilder } from '../../src/helpers/string';
3+
import { PipelineItem } from '../../src/types';
4+
import customFormatter from '../../src/reporters/cli-formatters/custom-formatter';
5+
import { customFormatterFixture } from '../fixture/custom-formatter.fixture';
6+
7+
customFormatterFixture.forEach(({ format, discoveryItem, expected, message }) => {
8+
test(`customFormatter should ${message}`, (t) => {
9+
// arrange
10+
const pipelineItem: PipelineItem = {
11+
profilingInformation: {},
12+
discoveryItem,
13+
};
14+
15+
const stringBuilder = new StringBuilder();
16+
17+
// act
18+
customFormatter(pipelineItem, format, stringBuilder);
19+
20+
// assert
21+
const value = stringBuilder.getValue();
22+
t.true(value.includes(expected));
23+
});
24+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import test from 'ava';
2+
import { StringBuilder } from '../../src/helpers/string';
3+
import { PipelineItem } from '../../src/types';
4+
import onlyOutdatedFormatter from '../../src/reporters/cli-formatters/only-outdated-formatter';
5+
import chalk from 'chalk';
6+
7+
test('onlyOutdatedFormatter should not build a string if the versions match', (t) => {
8+
// arrange
9+
const pipelineItem: PipelineItem = {
10+
profilingInformation: {},
11+
discoveryItem: {
12+
version: '2.4.0',
13+
packageJson: {
14+
version: '2.4.0',
15+
},
16+
},
17+
};
18+
19+
const stringBuilder = new StringBuilder();
20+
21+
// act
22+
onlyOutdatedFormatter(pipelineItem, stringBuilder);
23+
24+
// assert
25+
t.is(stringBuilder.getValue(), '');
26+
});
27+
28+
test('onlyOutdatedFormatter should build the formatted string if the versions mismatch', (t) => {
29+
// arrange
30+
const pipelineItem: PipelineItem = {
31+
profilingInformation: {},
32+
discoveryItem: {
33+
nameWithoutPrefix: 'vf-box',
34+
version: '2.3.1',
35+
packageJson: {
36+
version: '2.4.0',
37+
},
38+
config: {
39+
title: 'Box',
40+
label: 'Box',
41+
status: 'live',
42+
},
43+
},
44+
};
45+
46+
const stringBuilder = new StringBuilder();
47+
48+
// act
49+
onlyOutdatedFormatter(pipelineItem, stringBuilder);
50+
51+
// assert
52+
const value = stringBuilder.getValue();
53+
t.true(value.includes(chalk.bold('vf-box (Box)')));
54+
t.true(value.includes(`(${chalk.red('2.3.1')} -> ${chalk.green('2.4.0')})`));
55+
});
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { PDiscoveryItem } from '../../src/types';
2+
3+
export interface CustomFormatterFixtureItem {
4+
message: string;
5+
discoveryItem: PDiscoveryItem;
6+
format: string;
7+
expected: string;
8+
}
9+
10+
export const customFormatterFixture: CustomFormatterFixtureItem[] = [
11+
{
12+
message: 'display name, used version, latest version, and status',
13+
discoveryItem: {
14+
nameWithoutPrefix: 'vf-box',
15+
version: '2.4.0',
16+
packageJson: {
17+
version: '2.5.1',
18+
},
19+
config: {
20+
label: 'Box',
21+
status: 'live',
22+
title: 'Box',
23+
},
24+
},
25+
format: '%name (%usedVersion, %latestVersion, %status)',
26+
expected: 'vf-box (2.4.0, 2.5.1, live)',
27+
},
28+
{
29+
message: 'display name and dependents',
30+
discoveryItem: {
31+
nameWithoutPrefix: 'vf-box',
32+
dependents: ['test.html', 'tmp.html'],
33+
},
34+
format: '%name\\n%dependents(- %dependent\\n)',
35+
expected: 'vf-box\n- test.html\n- tmp.html',
36+
},
37+
{
38+
message: 'display name and changelog',
39+
discoveryItem: {
40+
nameWithoutPrefix: 'vf-box',
41+
changelog: [
42+
{
43+
version: '2.0.1',
44+
changes: ['change1', 'change2'],
45+
},
46+
{
47+
version: '2.0.0',
48+
changes: ['change1', 'change2'],
49+
},
50+
],
51+
},
52+
format: '%name\\n%changelog(%version\\n%changes(- %change\\n)\\n)',
53+
expected: 'vf-box\n2.0.1\n- change1\n- change2\n\n2.0.0\n- change1\n- change2',
54+
},
55+
];

0 commit comments

Comments
 (0)