create-app: add tests for fetchYarnLockSeedTask

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-02-02 11:57:16 +01:00
parent d99e713970
commit 58c0f2bcf0
2 changed files with 64 additions and 1 deletions
+63 -1
View File
@@ -27,8 +27,14 @@ import {
templatingTask,
tryInitGitRepository,
readGitConfig,
fetchYarnLockSeedTask,
} from './tasks';
import { createMockDirectory } from '@backstage/backend-test-utils';
import {
createMockDirectory,
setupRequestMockHandlers,
} from '@backstage/backend-test-utils';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
jest.spyOn(Task, 'log').mockReturnValue(undefined);
jest.spyOn(Task, 'error').mockReturnValue(undefined);
@@ -410,4 +416,60 @@ describe('tasks', () => {
);
});
});
describe('fetchYarnLockSeedTask', () => {
const worker = setupServer();
setupRequestMockHandlers(worker);
it('should fetch the yarn.lock seed file', async () => {
worker.use(
rest.get(
'https://raw.githubusercontent.com/backstage/backstage/master/packages/create-app/seed-yarn.lock',
(_, res, ctx) =>
res(
ctx.status(200),
ctx.text(`# the-lockfile-header
// some comments
// in the file
// that should
// be removed
// a comment about the entry
"@backstage/cli@1.0.0":
some info
`),
),
),
);
mockDir.clear();
await expect(fetchYarnLockSeedTask(mockDir.path)).resolves.toBe(true);
expect(mockDir.content({ shouldReadAsText: true })).toEqual({
'yarn.lock': `# the-lockfile-header
"@backstage/cli@1.0.0":
some info
`,
});
});
it('should fail gracefully', async () => {
worker.use(
rest.get(
'https://raw.githubusercontent.com/backstage/backstage/master/packages/create-app/seed-yarn.lock',
(_, res, ctx) => res(ctx.status(404)),
),
);
mockDir.clear();
await expect(fetchYarnLockSeedTask(mockDir.path)).resolves.toBe(false);
expect(mockDir.content()).toEqual({});
});
});
});