From 7838cd71ffee00dbcd6c2302e9fe34dec6221c30 Mon Sep 17 00:00:00 2001 From: Leonardo Maier Date: Tue, 20 Sep 2022 18:00:51 -0300 Subject: [PATCH] Fix package/create-app tests Signed-off-by: Leonardo Maier --- packages/create-app/src/createApp.test.ts | 2 +- packages/create-app/src/lib/tasks.test.ts | 230 ++++++------------ .../templates/default-app/package.json.hbs | 4 +- 3 files changed, 83 insertions(+), 153 deletions(-) diff --git a/packages/create-app/src/createApp.test.ts b/packages/create-app/src/createApp.test.ts index 47ac8b6be6..36552ae306 100644 --- a/packages/create-app/src/createApp.test.ts +++ b/packages/create-app/src/createApp.test.ts @@ -49,7 +49,7 @@ describe('command entrypoint', () => { }); }); - afterAll(() => { + afterEach(() => { mockFs.restore(); }); diff --git a/packages/create-app/src/lib/tasks.test.ts b/packages/create-app/src/lib/tasks.test.ts index 45c63d3e02..ac5f9e063b 100644 --- a/packages/create-app/src/lib/tasks.test.ts +++ b/packages/create-app/src/lib/tasks.test.ts @@ -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), + ); + }); }); }); diff --git a/packages/create-app/templates/default-app/package.json.hbs b/packages/create-app/templates/default-app/package.json.hbs index 19cb98ec09..c5810d8880 100644 --- a/packages/create-app/templates/default-app/package.json.hbs +++ b/packages/create-app/templates/default-app/package.json.hbs @@ -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 .",