From facd0a7c6d7cf8e811438bc724042501ed03fed5 Mon Sep 17 00:00:00 2001 From: studpeps Date: Sat, 17 Jul 2021 13:26:35 +0530 Subject: [PATCH] fix(amplify-cli):Moved function to seperate file --- .../__tests__/commands/uninstall.pkg.test.ts | 16 +++------- .../__tests__/commands/upgrade.pkg.test.ts | 29 +++++++------------ .../amplify-cli/src/snapshot-serializer.ts | 12 ++++++++ 3 files changed, 26 insertions(+), 31 deletions(-) create mode 100644 packages/amplify-cli/src/snapshot-serializer.ts diff --git a/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts b/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts index 804f17b258f..395cc251171 100644 --- a/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts +++ b/packages/amplify-cli/src/__tests__/commands/uninstall.pkg.test.ts @@ -4,6 +4,8 @@ import execa from 'execa'; import * as fs from 'fs-extra'; import { hideSync } from 'hidefile'; import { setRegPendingDelete } from '../../utils/win-utils'; +import { snapshotSerializer } from '../../snapshot-serializer'; +import path from 'path'; jest.mock('execa'); const execa_mock = execa as jest.Mocked; @@ -43,6 +45,7 @@ const setRegPendingDelete_mock = setRegPendingDelete as jest.MockedFunction { }); }); - expect.addSnapshotSerializer({ - test(val) { - return typeof val === 'string'; - }, - print(val) { - if (typeof val === 'string') - return `"${val.replace(/\\/ig, '/')}"`; - return ''; - }, - }); - it('removes the .amplify dir', async () => { await run(context_stub_typed); @@ -118,7 +110,7 @@ describe('uninstall packaged CLI on windows', () => { expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "homedir/.amplify/bin/amplify.exe", + "${path.join('homedir','.amplify','bin','amplify.exe')}", "a/test/path/.amplify-pending-delete.exe", Object { "overwrite": true, diff --git a/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts b/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts index e61562be6e7..9bfadbaebf1 100644 --- a/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts +++ b/packages/amplify-cli/src/__tests__/commands/upgrade.pkg.test.ts @@ -4,6 +4,7 @@ import fetch, { Response } from 'node-fetch'; import { $TSContext } from 'amplify-cli-core'; import * as core from 'amplify-cli-core'; import * as path from 'path'; +import {snapshotSerializer} from '../../snapshot-serializer'; jest.mock('fs-extra'); const fs_mock = (fs as unknown) as jest.Mocked; @@ -45,6 +46,7 @@ const context_stub_typed = (context_stub as unknown) as $TSContext; // save original process.platform const originalPlatform = process.platform; +snapshotSerializer(); describe('run upgrade using packaged CLI', () => { beforeEach(() => jest.clearAllMocks()); @@ -53,17 +55,6 @@ describe('run upgrade using packaged CLI', () => { value: originalPlatform, }); }); - - expect.addSnapshotSerializer({ - test(val) { - return typeof val === 'string'; - }, - print(val) { - if (typeof val === 'string') - return `"${val.replace(/\\/ig, '/')}"`; - return ''; - }, - }); it('exits early if no new packaged version available', async () => { // setup @@ -109,8 +100,8 @@ describe('run upgrade using packaged CLI', () => { // validate expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "${path.posix.join('homedir', 'bin', 'amplify-pkg-linux')}", - "${path.posix.join('homedir', 'bin', 'amplify')}", + "${path.join('homedir', 'bin', 'amplify-pkg-linux')}", + "${path.join('homedir', 'bin', 'amplify')}", Object { "overwrite": true, }, @@ -119,7 +110,7 @@ describe('run upgrade using packaged CLI', () => { expect(fs_mock.chmod.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "homedir/bin/amplify", + "${path.join('homedir', 'bin', 'amplify')}", "700", ] `); @@ -161,14 +152,14 @@ describe('run upgrade using packaged CLI', () => { // validate expect(fs_mock.move.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "${path.posix.join('homedir', 'bin', 'amplify.exe')}", - "${path.posix.join('homedir', 'bin', 'amplify-old.exe')}", + "${path.join('homedir', 'bin', 'amplify.exe')}", + "${path.join('homedir', 'bin', 'amplify-old.exe')}", ] `); expect(fs_mock.move.mock.calls[1]).toMatchInlineSnapshot(` Array [ - "${path.posix.join('homedir', 'bin', 'amplify-pkg-win.exe')}", - "${path.posix.join('homedir', 'bin', 'amplify.exe')}", + "${path.join('homedir', 'bin', 'amplify-pkg-win.exe')}", + "${path.join('homedir', 'bin', 'amplify.exe')}", Object { "overwrite": true, }, @@ -176,7 +167,7 @@ describe('run upgrade using packaged CLI', () => { `); expect(fs_mock.chmod.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "${path.posix.join('homedir', 'bin', 'amplify.exe')}", + "${path.join('homedir', 'bin', 'amplify.exe')}", "700", ] `); diff --git a/packages/amplify-cli/src/snapshot-serializer.ts b/packages/amplify-cli/src/snapshot-serializer.ts new file mode 100644 index 00000000000..ffdbcbf7484 --- /dev/null +++ b/packages/amplify-cli/src/snapshot-serializer.ts @@ -0,0 +1,12 @@ +const originalPlatform = process.platform; +// converts inputs to toMatchInlineSnapshot() function into supported format +export function snapshotSerializer(){ + expect.addSnapshotSerializer({ + test(val) { + return typeof val === 'string' && originalPlatform === 'win32'; + }, + print(val) { + return `"${(val as string).replace(/\\/g, '\\')}"`; + }, + }); +} \ No newline at end of file