create-app: fix fetchYarnLockSeedTask timeout + add test
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -39,6 +39,7 @@
|
||||
"fs-extra": "10.1.0",
|
||||
"handlebars": "^4.7.3",
|
||||
"inquirer": "^8.2.0",
|
||||
"node-fetch": "^2.6.7",
|
||||
"ora": "^5.3.0",
|
||||
"recursive-readdir": "^2.2.2"
|
||||
},
|
||||
|
||||
@@ -471,5 +471,20 @@ describe('tasks', () => {
|
||||
|
||||
expect(mockDir.content()).toEqual({});
|
||||
});
|
||||
|
||||
it('should time out if it takes too long to fetch', async () => {
|
||||
worker.use(
|
||||
rest.get(
|
||||
'https://raw.githubusercontent.com/backstage/backstage/master/packages/create-app/seed-yarn.lock',
|
||||
(_, res, ctx) => res(ctx.delay(5000)),
|
||||
),
|
||||
);
|
||||
|
||||
mockDir.clear();
|
||||
|
||||
await expect(fetchYarnLockSeedTask(mockDir.path)).resolves.toBe(false);
|
||||
|
||||
expect(mockDir.content()).toEqual({});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
resolve as resolvePath,
|
||||
relative as relativePath,
|
||||
} from 'path';
|
||||
import fetch from 'node-fetch';
|
||||
import { exec as execCb } from 'child_process';
|
||||
import { packageVersions } from './versions';
|
||||
import { promisify } from 'util';
|
||||
@@ -317,13 +318,15 @@ export async function fetchYarnLockSeedTask(dir: string) {
|
||||
try {
|
||||
await Task.forItem('fetching', 'yarn.lock seed', async () => {
|
||||
const controller = new AbortController();
|
||||
setTimeout(() => controller.abort(), 5000);
|
||||
const timeout = setTimeout(() => controller.abort(), 3000);
|
||||
const res = await fetch(
|
||||
'https://raw.githubusercontent.com/backstage/backstage/master/packages/create-app/seed-yarn.lock',
|
||||
{
|
||||
signal: controller.signal,
|
||||
},
|
||||
);
|
||||
clearTimeout(timeout);
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(
|
||||
`Request failed with status ${res.status} ${res.statusText}`,
|
||||
|
||||
Reference in New Issue
Block a user