create-app: refactor tests to avoid mock-fs
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -42,6 +42,7 @@
|
||||
"recursive-readdir": "^2.2.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@backstage/backend-test-utils": "workspace:^",
|
||||
"@backstage/cli": "workspace:^",
|
||||
"@types/command-exists": "^1.2.0",
|
||||
"@types/fs-extra": "^9.0.1",
|
||||
|
||||
@@ -15,13 +15,13 @@
|
||||
*/
|
||||
|
||||
import inquirer from 'inquirer';
|
||||
import mockFs from 'mock-fs';
|
||||
import path from 'path';
|
||||
import { Command } from 'commander';
|
||||
import * as tasks from './lib/tasks';
|
||||
import createApp from './createApp';
|
||||
import { findPaths } from '@backstage/cli-common';
|
||||
import { tmpdir } from 'os';
|
||||
import { createMockDirectory } from '@backstage/backend-test-utils';
|
||||
|
||||
jest.mock('./lib/tasks');
|
||||
|
||||
@@ -40,16 +40,7 @@ const moveAppMock = jest.spyOn(tasks, 'moveAppTask');
|
||||
const buildAppMock = jest.spyOn(tasks, 'buildAppTask');
|
||||
|
||||
describe('command entrypoint', () => {
|
||||
beforeEach(() => {
|
||||
mockFs({
|
||||
[`${__dirname}/package.json`]: '', // required by `findPaths(__dirname)`
|
||||
'templates/': mockFs.load(path.resolve(__dirname, '../templates/')),
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mockFs.restore();
|
||||
});
|
||||
const mockDir = createMockDirectory({ mockOsTmpDir: true });
|
||||
|
||||
beforeEach(() => {
|
||||
promptMock.mockResolvedValueOnce({
|
||||
@@ -62,6 +53,7 @@ describe('command entrypoint', () => {
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mockDir.clear();
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
@@ -75,7 +67,6 @@ describe('command entrypoint', () => {
|
||||
findPaths(__dirname).resolveTarget(
|
||||
'packages',
|
||||
'create-app',
|
||||
'src',
|
||||
'templates',
|
||||
'default-app',
|
||||
),
|
||||
@@ -97,7 +88,6 @@ describe('command entrypoint', () => {
|
||||
findPaths(__dirname).resolveTarget(
|
||||
'packages',
|
||||
'create-app',
|
||||
'src',
|
||||
'templates',
|
||||
'default-app',
|
||||
),
|
||||
|
||||
@@ -15,9 +15,8 @@
|
||||
*/
|
||||
|
||||
import fs from 'fs-extra';
|
||||
import mockFs from 'mock-fs';
|
||||
import child_process from 'child_process';
|
||||
import path, { resolve as resolvePath } from 'path';
|
||||
import { resolve as resolvePath } from 'path';
|
||||
import os from 'os';
|
||||
import {
|
||||
Task,
|
||||
@@ -29,6 +28,7 @@ import {
|
||||
tryInitGitRepository,
|
||||
readGitConfig,
|
||||
} from './tasks';
|
||||
import { createMockDirectory } from '@backstage/backend-test-utils';
|
||||
|
||||
jest.spyOn(Task, 'log').mockReturnValue(undefined);
|
||||
jest.spyOn(Task, 'error').mockReturnValue(undefined);
|
||||
@@ -101,21 +101,33 @@ describe('tasks', () => {
|
||||
) => void
|
||||
>;
|
||||
|
||||
const mockDir = createMockDirectory();
|
||||
|
||||
const realChdir = process.chdir;
|
||||
// If anyone calls chdir then make it resolve within the tmpdir
|
||||
const mockChdir = jest.spyOn(process, 'chdir');
|
||||
|
||||
beforeEach(() => {
|
||||
mockFs({
|
||||
'projects/my-module.ts': '',
|
||||
'projects/dir/my-file.txt': '',
|
||||
'tmp/mockApp/.gitignore': '',
|
||||
'tmp/mockApp/package.json': '',
|
||||
'tmp/mockApp/packages/app/package.json': '',
|
||||
// load templates into mock filesystem
|
||||
'templates/': mockFs.load(path.resolve(__dirname, '../../templates/')),
|
||||
mockDir.setContent({
|
||||
projects: {
|
||||
'my-module.ts': '',
|
||||
'dir/my-file.txt': '',
|
||||
},
|
||||
'tmp/mockApp': {
|
||||
'.gitignore': '',
|
||||
'package.json': '',
|
||||
'packages/app/package.json': '',
|
||||
},
|
||||
});
|
||||
realChdir(mockDir.path);
|
||||
mockChdir.mockImplementation((dir: string) =>
|
||||
realChdir(mockDir.resolve(dir)),
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mockExec.mockRestore();
|
||||
mockFs.restore();
|
||||
mockChdir.mockReset();
|
||||
});
|
||||
|
||||
describe('checkAppExistsTask', () => {
|
||||
@@ -164,8 +176,6 @@ describe('tasks', () => {
|
||||
|
||||
describe('buildAppTask', () => {
|
||||
it('should change to `appDir` and run `yarn install` and `yarn tsc`', async () => {
|
||||
const mockChdir = jest.spyOn(process, 'chdir');
|
||||
|
||||
// requires callback implementation to support `promisify` wrapper
|
||||
// https://stackoverflow.com/a/60579617/10044859
|
||||
mockExec.mockImplementation((_command, callback) => {
|
||||
@@ -199,8 +209,6 @@ describe('tasks', () => {
|
||||
});
|
||||
|
||||
it('should error out on incorrect yarn version', async () => {
|
||||
const mockChdir = jest.spyOn(process, 'chdir');
|
||||
|
||||
// requires callback implementation to support `promisify` wrapper
|
||||
// https://stackoverflow.com/a/60579617/10044859
|
||||
mockExec.mockImplementation((_command, callback) => {
|
||||
@@ -265,7 +273,7 @@ describe('tasks', () => {
|
||||
|
||||
describe('templatingTask', () => {
|
||||
it('should generate a project populating context parameters', async () => {
|
||||
const templateDir = 'templates/default-app';
|
||||
const templateDir = resolvePath(__dirname, '../../templates/default-app');
|
||||
const destinationDir = 'templatedApp';
|
||||
const context = {
|
||||
name: 'SuperCoolBackstageInstance',
|
||||
|
||||
@@ -4206,6 +4206,7 @@ __metadata:
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@backstage/create-app@workspace:packages/create-app"
|
||||
dependencies:
|
||||
"@backstage/backend-test-utils": "workspace:^"
|
||||
"@backstage/cli": "workspace:^"
|
||||
"@backstage/cli-common": "workspace:^"
|
||||
"@types/command-exists": ^1.2.0
|
||||
|
||||
Reference in New Issue
Block a user