Fix package/create-app tests

Signed-off-by: Leonardo Maier <leonarmaier@gmail.com>
This commit is contained in:
Leonardo Maier
2022-09-20 18:00:51 -03:00
parent 099ce48eef
commit 7838cd71ff
3 changed files with 83 additions and 153 deletions
+1 -1
View File
@@ -49,7 +49,7 @@ describe('command entrypoint', () => {
});
});
afterAll(() => {
afterEach(() => {
mockFs.restore();
});
+80 -150
View File
@@ -22,12 +22,12 @@ import {
Task,
buildAppTask,
checkAppExistsTask,
checkForGitSetup,
checkPathExistsTask,
createTemporaryAppFolderTask,
initGitRepository,
moveAppTask,
templatingTask,
initGitRepository,
checkForGitSetup,
} from './tasks';
jest.spyOn(Task, 'log').mockReturnValue(undefined);
@@ -88,100 +88,36 @@ jest.mock('./versions', () => ({
},
}));
const mockExec = child_process.exec as unknown as jest.MockedFunction<
(
command: string,
callback: (error: null, stdout: any, stderr: any) => void,
) => void
>;
describe('tasks', () => {
const mockExec = child_process.exec as unknown as jest.MockedFunction<
(
command: string,
callback: (error: null, stdout: any, stderr: any) => void,
) => void
>;
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/')),
});
});
afterEach(() => {
mockExec.mockRestore();
mockFs.restore();
});
describe('checkAppExistsTask', () => {
it('should do nothing if the directory does not exist', async () => {
const dir = 'projects/';
const name = 'MyNewApp';
await expect(checkAppExistsTask(dir, name)).resolves.not.toThrow();
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/')),
});
});
afterEach(() => {
mockExec.mockRestore();
mockFs.restore();
});
it('should throw an error when a directory of the same name exists', async () => {
const dir = 'projects/';
const name = 'dir';
await expect(checkAppExistsTask(dir, name)).rejects.toThrow(
'already exists',
);
});
});
describe('checkPathExistsTask', () => {
it('should create a directory at the given path', async () => {
const appDir = 'projects/newProject';
await expect(checkPathExistsTask(appDir)).resolves.not.toThrow();
expect(fs.existsSync(appDir)).toBe(true);
});
it('should do nothing if a directory of the same name exists', async () => {
const appDir = 'projects/dir';
await expect(checkPathExistsTask(appDir)).resolves.not.toThrow();
expect(fs.existsSync(appDir)).toBe(true);
});
it('should fail if a file of the same name exists', async () => {
await expect(checkPathExistsTask('projects/my-module.ts')).rejects.toThrow(
'already exists',
);
});
});
describe('createTemporaryAppFolderTask', () => {
it('should create a directory at a given path', async () => {
const tempDir = 'projects/tmpFolder';
await expect(createTemporaryAppFolderTask(tempDir)).resolves.not.toThrow();
expect(fs.existsSync(tempDir)).toBe(true);
});
it('should fail if a directory of the same name exists', async () => {
const tempDir = 'projects/dir';
await expect(createTemporaryAppFolderTask(tempDir)).rejects.toThrow(
'file already exists',
);
});
it('should fail if a file of the same name exists', async () => {
const tempDir = 'projects/dir/my-file.txt';
await expect(createTemporaryAppFolderTask(tempDir)).rejects.toThrow(
'file already exists',
);
});
});
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) => {
callback(null, 'standard out', 'standard error');
describe('checkAppExistsTask', () => {
it('should do nothing if the directory does not exist', async () => {
const dir = 'projects/';
const name = 'MyNewApp';
await expect(checkAppExistsTask(dir, name)).resolves.not.toThrow();
});
it('should throw an error when a file of the same name exists', async () => {
@@ -248,12 +184,6 @@ describe('buildAppTask', () => {
describe('buildAppTask', () => {
it('should change to `appDir` and run `yarn install` and `yarn tsc`', async () => {
const mockChdir = jest.spyOn(process, 'chdir');
const mockExec = child_process.exec as unknown as jest.MockedFunction<
(
command: string,
callback: (error: null, stdout: string, stderr: string) => void,
) => void
>;
// requires callback implementation to support `promisify` wrapper
// https://stackoverflow.com/a/60579617/10044859
@@ -347,64 +277,64 @@ describe('buildAppTask', () => {
).toContain('sqlite3"');
});
});
});
describe('checkForGitSetup', () => {
it('should check if git package is installed and configured', async () => {
mockExec.mockImplementation((_command, callback) => {
callback(null, { stdout: 'main' }, 'standard error');
describe('checkForGitSetup', () => {
it('should check if git package is installed and configured', async () => {
mockExec.mockImplementation((_command, callback) => {
callback(null, { stdout: 'main' }, 'standard error');
});
await checkForGitSetup();
expect(mockExec).toHaveBeenCalledTimes(3);
expect(mockExec).toHaveBeenNthCalledWith(
1,
'which git',
expect.any(Function),
);
expect(mockExec).toHaveBeenNthCalledWith(
2,
'git config user.name',
expect.any(Function),
);
expect(mockExec).toHaveBeenNthCalledWith(
3,
'git config user.email',
expect.any(Function),
);
});
});
await checkForGitSetup();
describe('initGitRepository', () => {
it('should initialize a git repository at the given path', async () => {
const destinationDir = 'tmp/mockApp/';
const context = {
defaultBranch: '',
};
expect(mockExec).toHaveBeenCalledTimes(3);
expect(mockExec).toHaveBeenNthCalledWith(
1,
'which git',
expect.any(Function),
);
expect(mockExec).toHaveBeenNthCalledWith(
2,
'git config user.name',
expect.any(Function),
);
expect(mockExec).toHaveBeenNthCalledWith(
3,
'git config user.email',
expect.any(Function),
);
});
});
describe('initGitRepository', () => {
it('should initialize a git repository at the given path', async () => {
const destinationDir = 'tmp/mockApp/';
const context = {
defaultBranch: '',
};
mockExec.mockImplementation((_command, callback) => {
callback(null, { stdout: 'main' }, 'standard error');
});
await initGitRepository(destinationDir, context);
expect(context.defaultBranch).toBe('main');
expect(mockExec).toHaveBeenCalledTimes(3);
expect(mockExec).toHaveBeenNthCalledWith(
1,
'git init',
expect.any(Function),
);
expect(mockExec).toHaveBeenNthCalledWith(
2,
'git commit --allow-empty -m "Initial commit"',
expect.any(Function),
);
expect(mockExec).toHaveBeenNthCalledWith(
3,
'git branch --format="%(refname:short)"',
expect.any(Function),
);
mockExec.mockImplementation((_command, callback) => {
callback(null, { stdout: 'main' }, 'standard error');
});
await initGitRepository(destinationDir, context);
expect(context.defaultBranch).toBe('main');
expect(mockExec).toHaveBeenCalledTimes(3);
expect(mockExec).toHaveBeenNthCalledWith(
1,
'git init',
expect.any(Function),
);
expect(mockExec).toHaveBeenNthCalledWith(
2,
'git commit --allow-empty -m "Initial commit"',
expect.any(Function),
);
expect(mockExec).toHaveBeenNthCalledWith(
3,
'git branch --format="%(refname:short)"',
expect.any(Function),
);
});
});
});
@@ -14,8 +14,8 @@
"tsc": "tsc",
"tsc:full": "tsc --skipLibCheck false --incremental false",
"clean": "backstage-cli repo clean",
"test": "backstage-cli test",
"test:all": "lerna run test -- --coverage",
"test": "backstage-cli repo test",
"test:all": "backstage-cli repo test --coverage",
"lint": "backstage-cli repo lint --since origin/{{defaultBranch}}",
"lint:all": "backstage-cli repo lint",
"prettier:check": "prettier --check .",