Skip to content

Commit c51148c

Browse files
committed
Refactor CLI tests to use vitest (#1075)
1 parent 1dac1e9 commit c51148c

12 files changed

Lines changed: 305 additions & 336 deletions

File tree

cli/jest.config.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"watch": "node watch.mjs",
1515
"copy-calm-schema": "copyfiles \"../calm/draft/2024-10/meta/*\" dist/calm/",
1616
"copy-docify-templates": "copyfiles \"../shared/src/docify/template-bundles/**/*\" dist --up 4",
17-
"test": "jest",
17+
"test": "vitest run",
1818
"lint": "eslint src",
1919
"lint-fix": "eslint src --fix",
2020
"dependency-check": "dependency-check --project 'calm-cli' --scan . --out ./dependency-check-report --format ALL --suppression ../.github/node-cve-ignore-list.xml"

cli/src/cli.e2e.spec.ts

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import axios from 'axios';
99

1010

1111
// Mock axios
12-
jest.mock('axios');
12+
vi.mock('axios');
1313

1414
const execPromise = util.promisify(exec);
1515

@@ -19,7 +19,7 @@ describe('CLI Integration Tests', () => {
1919
const millisPerSecond = 1000;
2020
const integrationTestPrefix = 'calm-test';
2121
const projectRoot = __dirname;
22-
jest.setTimeout(30 * millisPerSecond);
22+
vi.setConfig({ testTimeout: 30 * millisPerSecond });
2323

2424
beforeAll(async () => {
2525
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), integrationTestPrefix));
@@ -32,46 +32,42 @@ describe('CLI Integration Tests', () => {
3232
}
3333
}, millisPerSecond * 20);
3434

35-
test('shows help if no arguments provided', (done) => {
35+
test('shows help if no arguments provided', async () => {
3636
const noArgCommand = 'calm';
3737
exec(noArgCommand, (error, _stdout, stderr) => {
3838
expect(error).not.toBeNull();
3939
expect(stderr).toContain('A set of tools for interacting with the Common Architecture Language Model');
4040
expect(stderr).toContain('Usage:');
41-
done();
4241
});
4342
});
4443

45-
test('shows help if -h provided', (done) => {
44+
test('shows help if -h provided', async () => {
4645
const helpShortFlagCommand = 'calm -h';
4746
exec(helpShortFlagCommand, (_error, stdout, _stderr) => {
4847
expect(stdout).toContain('A set of tools for interacting with the Common Architecture Language Model');
4948
expect(stdout).toContain('Usage:');
50-
done();
5149
});
5250
});
5351

54-
test('shows help if --help provided', (done) => {
52+
test('shows help if --help provided', async () => {
5553
const helpLongFlagCommand = 'calm --help';
5654
exec(helpLongFlagCommand, (_error, stdout, _stderr) => {
5755
expect(stdout).toContain('A set of tools for interacting with the Common Architecture Language Model');
5856
expect(stdout).toContain('Usage:');
59-
done();
6057
});
6158
});
6259

63-
test('example validate command - outputting JSON to stdout', (done) => {
60+
test('example validate command - outputting JSON to stdout', async () => {
6461
const exampleValidateCommand = 'calm validate -p ../calm/pattern/api-gateway.json -a ../calm/samples/api-gateway-architecture.json';
6562
exec(exampleValidateCommand, (_error, stdout, _stderr) => {
6663
const parsedOutput = JSON.parse(stdout);
6764
const expectedFilePath = path.join(__dirname, '../test_fixtures/validate_output.json');
6865
const expectedJson = JSON.parse(fs.readFileSync(expectedFilePath, 'utf-8'));
6966
expect(parsedOutput).toEqual(expectedJson);
70-
done();
7167
});
7268
});
7369

74-
test('example validate command - outputting JSON to file', (done) => {
70+
test('example validate command - outputting JSON to file', async () => {
7571
const targetOutputFile = path.join(tempDir, 'validate-output.json');
7672
const exampleValidateCommand = `calm validate -p ../calm/pattern/api-gateway.json -a ../calm/samples/api-gateway-architecture.json -o ${targetOutputFile}`;
7773
exec(exampleValidateCommand, (_error, _stdout, _stderr) => {
@@ -84,12 +80,10 @@ describe('CLI Integration Tests', () => {
8480
const expectedJson = JSON.parse(fs.readFileSync(expectedFilePath, 'utf-8'));
8581

8682
expect(parsedOutput).toEqual(expectedJson);
87-
88-
done();
8983
});
9084
});
9185

92-
test('example validate command - outputting JUNIT to stdout', (done) => {
86+
test('example validate command - outputting JUNIT to stdout', async () => {
9387
const exampleValidateCommand = 'calm validate -p ../calm/pattern/api-gateway.json -a ../calm/samples/api-gateway-architecture.json -f junit';
9488
exec(exampleValidateCommand, async (_error, stdout, _stderr) => {
9589
const parsedOutput = await parseStringPromise(stdout);
@@ -99,11 +93,10 @@ describe('CLI Integration Tests', () => {
9993
const expectedXml = await parseStringPromise(expectedXmlString);
10094

10195
expect(parsedOutput).toEqual(expectedXml);
102-
done();
10396
});
10497
});
10598

106-
test('example validate command - outputting JUNIT to file', (done) => {
99+
test('example validate command - outputting JUNIT to file', async () => {
107100
const targetOutputFile = path.join(tempDir, 'validate-output.xml');
108101
const exampleValidateCommand = `calm validate -p ../calm/pattern/api-gateway.json -a ../calm/samples/api-gateway-architecture.json -f junit -o ${targetOutputFile}`;
109102
exec(exampleValidateCommand, async (_error, _stdout, _stderr) => {
@@ -117,13 +110,11 @@ describe('CLI Integration Tests', () => {
117110
const expectedXml = await parseStringPromise(expectedXmlString);
118111

119112
expect(parsedOutput).toEqual(expectedXml);
120-
121-
done();
122113
});
123114
});
124115

125116

126-
test('example generate command - does it give the output we expect', (done) => {
117+
test('example generate command - does it give the output we expect', async () => {
127118
const targetOutputFile = path.join(tempDir, 'generate-output.json');
128119
const exampleGenerateCommand = `calm generate -p ../calm/pattern/api-gateway.json -o ${targetOutputFile}`;
129120
exec(exampleGenerateCommand, async (_error, _stdout, _stderr) => {
@@ -136,23 +127,20 @@ describe('CLI Integration Tests', () => {
136127
const expectedJson = JSON.parse(fs.readFileSync(expectedFilePath, 'utf-8'));
137128

138129
expect(parsedOutput).toEqual(expectedJson);
139-
140-
done();
141130
});
142131
});
143132

144-
test('example validate command - outputting PRETTY to stdout', (done) => {
133+
test('example validate command - outputting PRETTY to stdout', async () => {
145134
const exampleValidateCommand = 'calm validate -p ../calm/pattern/api-gateway.json -a ../calm/samples/api-gateway-architecture.json -f pretty';
146135
exec(exampleValidateCommand, (_error, stdout, _stderr) => {
147136
const expectedFilePath = path.join(__dirname, '../test_fixtures/validate_output_pretty.txt');
148137
const expectedOutput = fs.readFileSync(expectedFilePath, 'utf-8');
149138
//Some minor replacement logic to avoid issues with line endings
150139
expect(stdout.replace(/\r\n/g, '\n')).toEqual(expectedOutput.replace(/\r\n/g, '\n'));
151-
done();
152140
});
153141
});
154142

155-
test('example validate command - outputting PRETTY to file', (done) => {
143+
test('example validate command - outputting PRETTY to file', async () => {
156144
const targetOutputFile = path.join(tempDir, 'validate-output-pretty.txt');
157145
const exampleValidateCommand = `calm validate -p ../calm/pattern/api-gateway.json -a ../calm/samples/api-gateway-architecture.json -f pretty -o ${targetOutputFile}`;
158146
exec(exampleValidateCommand, (_error, _stdout, _stderr) => {
@@ -164,34 +152,31 @@ describe('CLI Integration Tests', () => {
164152

165153
//Some minor replacement logic to avoid issues with line endings
166154
expect(outputString.replace(/\r\n/g, '\n')).toEqual(expectedOutput.replace(/\r\n/g, '\n'));
167-
done();
168155
});
169156
});
170157

171-
test('example validate command - fails when neither an architecture or a pattern is provided', (done) => {
158+
test('example validate command - fails when neither an architecture or a pattern is provided', async () => {
172159
const calmValidateCommand = 'calm validate';
173160
exec(calmValidateCommand, (error, _stdout, stderr) => {
174161
expect(error).not.toBeNull();
175162
expect(stderr).toContain('error: one of the required options \'-p, --pattern <file>\' or \'-a, --architecture <file>\' was not specified');
176-
done();
177163
});
178164
});
179165

180-
test('example validate command - validates an architecture only', (done) => {
166+
test('example validate command - validates an architecture only', async () => {
181167
const calmValidateArchitectureOnlyCommand = 'calm validate -a ../calm/samples/api-gateway-architecture.json';
182168
exec(calmValidateArchitectureOnlyCommand, (error, stdout, _stderr) => {
183169
const expectedFilePath = path.join(__dirname, '../test_fixtures/validate_architecture_only_output.json');
184170
const expectedOutput = fs.readFileSync(expectedFilePath, 'utf-8');
185171
expect(error).toBeNull();
186172
expect(stdout).toContain(expectedOutput);
187-
done();
188173
});
189174
});
190175

191176
test('example server command - starts server and responds to requests', async () => {
192177
// Mock the axios response
193178
const mockResponse = { status: 200, data: { status: 'ok' } };
194-
(axios.get as jest.Mock).mockResolvedValue(mockResponse);
179+
(axios.get as vi.Mock).mockResolvedValue(mockResponse);
195180

196181
const serverCommand = 'calm server -p 3001 --schemaDirectory ../../dist/calm/';
197182
const serverProcess = exec(serverCommand);

0 commit comments

Comments
 (0)