Skip to content

Commit 1234fdd

Browse files
committed
test(cli): add test for watcher
1 parent bfcb2e9 commit 1234fdd

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

packages/cli/src/commands/start.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as chokidar from 'chokidar';
1212
import * as jsYAML from 'js-yaml';
1313
import { promises as fs } from 'fs';
1414
import * as path from 'path';
15-
import { logger } from '../utils';
15+
import { addShutdownJob, logger } from '../utils';
1616

1717
const callAfterFulfilled = (func: () => Promise<void>) => {
1818
let busy = false;
@@ -70,8 +70,13 @@ export const handleStart = async (
7070

7171
const restartServer = async () => {
7272
if (stopServer) await stopServer();
73-
await buildVulcan(buildOptions);
74-
stopServer = (await serveVulcan(serveOptions)).stopServer;
73+
try {
74+
await buildVulcan(buildOptions);
75+
stopServer = (await serveVulcan(serveOptions)).stopServer;
76+
} catch (e) {
77+
// Ignore the error to keep watch process works
78+
if (!startOptions.watch) throw e;
79+
}
7580
};
7681

7782
await restartServer();
@@ -110,10 +115,13 @@ export const handleStart = async (
110115
}
111116

112117
const restartWhenFulfilled = callAfterFulfilled(restartServer);
113-
chokidar
118+
const watcher = chokidar
114119
.watch(pathsToWatch, { ignoreInitial: true })
115120
.on('all', () => restartWhenFulfilled());
116-
121+
addShutdownJob(async () => {
122+
logger.info(`Stop watching changes...`);
123+
await watcher.close();
124+
});
117125
logger.info(`Start watching changes...`);
118126
}
119127
};

packages/cli/test/cli.spec.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ beforeAll(async () => {
3737
}, 30000);
3838

3939
afterAll(async () => {
40-
await fs.rm(projectRoot, { recursive: true, force: true });
40+
await fs.rm(projectRoot, { recursive: true, force: true, maxRetries: 5 });
4141
});
4242

4343
afterEach(async () => {
@@ -83,6 +83,28 @@ it('Start command should build the project and start Vulcan server', async () =>
8383
await runShutdownJobs();
8484
});
8585

86+
it('Start command with watch option should watch the file changes', (done) => {
87+
// Arrange
88+
const agent = supertest(`http://localhost:${testingServerPort}`);
89+
const testYamlPath = path.resolve(projectRoot, 'sqls', 'test.yaml');
90+
91+
// Act
92+
program
93+
.parseAsync(['node', 'vulcan', 'start', '-w'])
94+
// Wait for server start
95+
.then(() => new Promise((resolve) => setTimeout(resolve, 1000)))
96+
// Write some invalid configs, let server fail to start
97+
.then(() => fs.writeFile(testYamlPath, 'url: /user/:id\ns', 'utf-8'))
98+
// Wait for serer restart
99+
.then(() => new Promise((resolve) => setTimeout(resolve, 1000)))
100+
.then(() => expect(agent.get('/doc')).rejects.toThrow())
101+
.finally(() => {
102+
runShutdownJobs()
103+
.then(() => fs.rm(testYamlPath))
104+
.then(() => done());
105+
});
106+
}, 5000);
107+
86108
it('Version command should execute without error', async () => {
87109
// Action, Assert
88110
await expect(

0 commit comments

Comments
 (0)