Promise (await) never resolved, tests timeout #2915
-
Hi I'm using AVA to test a custom server (nodejs). I'm starting the server in each test and I'd like to extract that into a function. However, when I do that, the Here's an example: const startServer = async (port, cliArgs = [], execaOptions = {}) => {
console.log('start server…');
const subprocess = execa(SERVER_BIN_PATH, ['--port', port, SERVER_FIXTURES_DIR, ...cliArgs], execaOptions);
// Wait for server to start before continuing
// @see https://github.com/avajs/ava/issues/2625#issuecomment-747273810
let connected = false;
while (!connected) {
connected = await isPortReachable(port, { host: 'localhost' });
}
// This runs…
console.log('connected…', connected);
return subprocess;
};
test.before(async t => {
const subprocess = await startServer(DEFAULT_SERVER_PORT);
// This never runs and the tests timeout…
console.log('awaited…');
t.context.server = subprocess;
}); if I write the test.before(async t => {
const subprocess = execaNode(SERVER_BIN_PATH, ['--port', DEFAULT_SERVER_PORT, SERVER_FIXTURES_DIR]);
// Wait for server to start before continuing
// @see https://github.com/avajs/ava/issues/2625#issuecomment-747273810
let connected = false;
while (!connected) {
connected = await isPortReachable(DEFAULT_SERVER_PORT, { host: 'localhost' });
}
t.context.server = subprocess;
}); Any ideas why this might be happening? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Beta Was this translation helpful? Give feedback.
-
This seems to work: const startServer = async (port = DEFAULT_SERVER_PORT, cliArgs = [], execaOptions = {}) => {
const subprocess = execaNode(SERVER_BIN_PATH, ['--port', port, SERVER_FIXTURES_DIR, ...cliArgs], execaOptions);
let connected = false;
while (!connected) {
connected = await isPortReachable(port, { host: 'localhost' });
}
return {
subprocess,
};
}; and then const { subprocess } = await startServer(); |
Beta Was this translation helpful? Give feedback.
execa
returns a promise for when the subprocess exits, which meansawait startServer()
never completes.