Skip to content

Commit 4fa7cc5

Browse files
Merge pull request #157 from contentstack/development
Mocking @contentstack/cli-utilities so ESM uuid does not break the test suite
2 parents 8565cf5 + b529a38 commit 4fa7cc5

11 files changed

Lines changed: 473 additions & 486 deletions

eslint.config.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ export default tseslint.config(
2525
]
2626
},
2727
},
28+
{
29+
files: ['src/**/*.test.ts'],
30+
rules: {
31+
'max-len': 'off',
32+
},
33+
},
2834
);

package-lock.json

Lines changed: 338 additions & 430 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/cli-launch",
3-
"version": "1.9.8",
3+
"version": "1.9.9",
44
"description": "Launch related operations",
55
"author": "Contentstack CLI",
66
"bin": {

src/adapters/base-class.test.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1+
import { baseClassAdapter as cliUtilitiesJestMock } from '../test/mocks/cli-utilities';
12
import BaseClass from './base-class';
23
import { cliux as ux, ContentstackClient } from '@contentstack/cli-utilities';
34
import config from '../config';
45
import { FILE_UPLOAD_SIZE_LIMIT_USER_MESSAGE } from '../util/deployment-errors';
56

6-
jest.mock('@contentstack/cli-utilities', () => ({
7-
cliux: {
8-
inquire: jest.fn(),
9-
table: jest.fn(),
10-
},
11-
}));
7+
jest.mock('@contentstack/cli-utilities', () => cliUtilitiesJestMock);
128

139
describe('BaseClass', () => {
1410
let baseClass: BaseClass;
@@ -121,7 +117,7 @@ describe('BaseClass', () => {
121117

122118
expect(exitMock).not.toHaveBeenCalled();
123119
expect(logMock).not.toHaveBeenCalledWith(
124-
"The 'Skip adding environment variables' option cannot be combined with other environment variable options. Please choose either 'Skip adding environment variables' or one or more of the other available options.",
120+
'The \'Skip adding environment variables\' option cannot be combined with other environment variable options. Please choose either \'Skip adding environment variables\' or one or more of the other available options.',
125121
'error',
126122
);
127123
});
@@ -233,7 +229,7 @@ describe('BaseClass', () => {
233229
await baseClass.handleEnvImportFlow();
234230

235231
expect(logMock).toHaveBeenCalledWith(
236-
"The 'Skip adding environment variables' option cannot be combined with other environment variable options. Please choose either 'Skip adding environment variables' or one or more of the other available options.",
232+
'The \'Skip adding environment variables\' option cannot be combined with other environment variable options. Please choose either \'Skip adding environment variables\' or one or more of the other available options.',
237233
'error',
238234
);
239235
expect(exitMock).toHaveBeenCalledWith(1);
@@ -271,7 +267,7 @@ describe('BaseClass', () => {
271267
await expect(baseClass.handleEnvImportFlow()).rejects.toThrow('Exit called');
272268

273269
expect(logMock).toHaveBeenCalledWith(
274-
"The 'Skip adding environment variables' option cannot be combined with other environment variable options. Please choose either 'Skip adding environment variables' or one or more of the other available options.",
270+
'The \'Skip adding environment variables\' option cannot be combined with other environment variable options. Please choose either \'Skip adding environment variables\' or one or more of the other available options.',
275271
'error',
276272
);
277273

src/adapters/file-upload.test.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
1+
import { fileUploadAdapter as cliUtilitiesJestMock } from '../test/mocks/cli-utilities';
12
import FileUpload from './file-upload';
23
import BaseClass from './base-class';
34
import { cliux } from '@contentstack/cli-utilities';
45
import { DeploymentStatus } from '../types/launch';
56

6-
jest.mock('@contentstack/cli-utilities', () => ({
7-
...jest.requireActual('@contentstack/cli-utilities'),
8-
cliux: {
9-
inquire: jest.fn(),
10-
loader: jest.fn(),
11-
print: jest.fn(),
12-
},
13-
configHandler: {
14-
get: jest.fn(),
15-
},
16-
HttpClient: jest.fn(),
17-
}));
7+
jest.mock('@contentstack/cli-utilities', () => cliUtilitiesJestMock);
188

199
describe('FileUpload Adapter', () => {
2010
let logMock: jest.Mock;

src/adapters/github.test.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { githubAdapter as cliUtilitiesJestMock } from '../test/mocks/cli-utilities';
12
import GitHub from './github';
23
import { getRemoteUrls } from '../util/create-git-meta';
34
import { repositoriesQuery, userConnectionsQuery } from '../graphql';
@@ -6,16 +7,7 @@ import { existsSync } from 'fs';
67
import { DeploymentStatus } from '../types';
78
import { cliux as ux } from '@contentstack/cli-utilities';
89

9-
jest.mock('@contentstack/cli-utilities', () => ({
10-
...jest.requireActual('@contentstack/cli-utilities'),
11-
cliux: {
12-
inquire: jest.fn(),
13-
print: jest.fn(),
14-
},
15-
configHandler: {
16-
get: jest.fn(),
17-
},
18-
}));
10+
jest.mock('@contentstack/cli-utilities', () => cliUtilitiesJestMock);
1911

2012
jest.mock('../util/create-git-meta');
2113
jest.mock('fs', () => ({

src/commands/launch/functions.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { functionsCommand as cliUtilitiesJestMock } from '../../test/mocks/cli-utilities';
12
import Functions from './functions';
23
import { Logger } from '../../util';
34
import { Contentfly } from '../../util/cloud-function/contentfly';
45

6+
jest.mock('@contentstack/cli-utilities', () => cliUtilitiesJestMock);
7+
58
jest.mock('../../util');
69
jest.mock('../../util/cloud-function/contentfly');
710

src/commands/launch/index.test.ts

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,26 @@
1+
import { launchCommand as cliUtilitiesJestMock } from '../../test/mocks/cli-utilities';
12
import Launch from './index';
23
import { BaseCommand } from '../../base-command';
34
import { FileUpload, GitHub, PreCheck } from '../../adapters';
4-
import { cliux } from '@contentstack/cli-utilities';
5+
import { cliux, configHandler } from '@contentstack/cli-utilities';
56

67
jest.mock('../../base-command');
8+
jest.mock('@contentstack/cli-utilities', () => cliUtilitiesJestMock);
79

8-
jest.mock('@contentstack/cli-utilities', () => {
9-
const actual = jest.requireActual('@contentstack/cli-utilities');
10-
return {
11-
...actual,
12-
configHandler: {
13-
get: jest.fn((key) => {
14-
if (key === 'authtoken') return 'dummy-token';
15-
if (key === 'authorisationType') return 'OAuth';
16-
if (key === 'oauthAccessToken') return 'dummy-oauth-token';
17-
return undefined;
18-
}),
19-
},
20-
cliux: {
21-
...actual.cliux,
22-
inquire: jest.fn(), // mock `inquire` explicitly
23-
},
24-
};
25-
});
10+
const LaunchWithArgv = Launch as unknown as new (argv: string[], config: unknown) => Launch;
2611

2712
describe('Run', () => {
2813
let launchCommandInstance: Launch;
2914
let prepareApiClientsMock: jest.SpyInstance;
3015
let preCheckRunMock: jest.SpyInstance;
3116

3217
beforeEach(() => {
18+
(configHandler.get as jest.Mock).mockImplementation((key: string) => {
19+
if (key === 'authtoken') return 'dummy-token';
20+
if (key === 'authorisationType') return 'OAuth';
21+
if (key === 'oauthAccessToken') return 'dummy-oauth-token';
22+
return undefined;
23+
});
3324
prepareApiClientsMock = jest.spyOn(BaseCommand.prototype, 'prepareApiClients').mockResolvedValueOnce(undefined);
3425
// @ts-expect-error - Override readonly property context on BaseCommand for testing
3526
BaseCommand.prototype['context'] = { analyticsInfo: {} } as any;
@@ -45,7 +36,7 @@ describe('Run', () => {
4536
const githubRunMock = jest.spyOn(GitHub.prototype, 'run').mockResolvedValueOnce(undefined);
4637
BaseCommand.prototype['sharedConfig'] = { provider: 'GitHub', isExistingProject: true } as any;
4738
BaseCommand.prototype['flags'] = { init: false };
48-
launchCommandInstance = new Launch([], {} as any);
39+
launchCommandInstance = new LaunchWithArgv([], {} as any);
4940

5041
await launchCommandInstance.run();
5142

@@ -58,7 +49,7 @@ describe('Run', () => {
5849
const fileUploadRunMock = jest.spyOn(FileUpload.prototype, 'run').mockResolvedValueOnce(undefined);
5950
BaseCommand.prototype['sharedConfig'] = { provider: 'FileUpload', isExistingProject: true } as any;
6051
BaseCommand.prototype['flags'] = { init: false };
61-
launchCommandInstance = new Launch([], {} as any);
52+
launchCommandInstance = new LaunchWithArgv([], {} as any);
6253

6354
await launchCommandInstance.run();
6455

@@ -73,7 +64,7 @@ describe('Run', () => {
7364
.mockResolvedValueOnce(undefined);
7465
BaseCommand.prototype['sharedConfig'] = { provider: 'OtherProvider', isExistingProject: true } as any;
7566
BaseCommand.prototype['flags'] = { init: false };
76-
launchCommandInstance = new Launch([], {} as any);
67+
launchCommandInstance = new LaunchWithArgv([], {} as any);
7768

7869
await launchCommandInstance.run();
7970

@@ -89,7 +80,7 @@ describe('Run', () => {
8980
BaseCommand.prototype['sharedConfig'] = { provider: 'GitHub', isExistingProject: false } as any;
9081
BaseCommand.prototype['flags'] = { init: false };
9182
(cliux.inquire as jest.Mock).mockResolvedValueOnce('GitHub');
92-
launchCommandInstance = new Launch([], {} as any);
83+
launchCommandInstance = new LaunchWithArgv([], {} as any);
9384

9485
await launchCommandInstance.run();
9586

@@ -111,7 +102,7 @@ describe('Run', () => {
111102
const githubRunMock = jest.spyOn(GitHub.prototype, 'run').mockResolvedValueOnce(undefined);
112103
BaseCommand.prototype['sharedConfig'] = {isExistingProject: false} as any;
113104
BaseCommand.prototype['flags'] = { init: false, type: 'GitHub' };
114-
launchCommandInstance = new Launch([], {} as any);
105+
launchCommandInstance = new LaunchWithArgv([], {} as any);
115106

116107
await launchCommandInstance.run();
117108

src/test/mocks/cli-utilities.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { Args, Command, Errors, Flags, Interfaces } from '@oclif/core';
2+
3+
class CLIError extends Error {}
4+
5+
export const launchCommand = {
6+
Args,
7+
Command,
8+
Errors,
9+
Flags,
10+
Interfaces,
11+
CLIError,
12+
cliux: {
13+
inquire: jest.fn(),
14+
print: jest.fn(),
15+
loader: jest.fn(),
16+
registerSearchPlugin: jest.fn(),
17+
},
18+
configHandler: { get: jest.fn() },
19+
authHandler: {
20+
compareOAuthExpiry: jest.fn().mockResolvedValue(undefined),
21+
host: '',
22+
},
23+
HttpClient: jest.fn(),
24+
ContentstackClient: jest.fn(),
25+
isAuthenticated: jest.fn().mockReturnValue(true),
26+
managementSDKClient: jest.fn(),
27+
managementSDKInitiator: jest.fn(),
28+
};
29+
30+
export const fileUploadAdapter = {
31+
cliux: {
32+
inquire: jest.fn(),
33+
loader: jest.fn(),
34+
print: jest.fn(),
35+
registerSearchPlugin: jest.fn(),
36+
confirm: jest.fn(),
37+
prompt: jest.fn(),
38+
styledJSON: jest.fn(),
39+
table: jest.fn(),
40+
},
41+
configHandler: { get: jest.fn() },
42+
HttpClient: jest.fn(),
43+
ContentstackClient: jest.fn(),
44+
authHandler: {
45+
compareOAuthExpiry: jest.fn().mockResolvedValue(undefined),
46+
host: '',
47+
},
48+
};
49+
50+
export const githubAdapter = {
51+
cliux: {
52+
inquire: jest.fn(),
53+
print: jest.fn(),
54+
loader: jest.fn(),
55+
registerSearchPlugin: jest.fn(),
56+
confirm: jest.fn(),
57+
prompt: jest.fn(),
58+
styledJSON: jest.fn(),
59+
table: jest.fn(),
60+
},
61+
configHandler: { get: jest.fn() },
62+
ContentstackClient: jest.fn(),
63+
authHandler: {
64+
compareOAuthExpiry: jest.fn().mockResolvedValue(undefined),
65+
host: '',
66+
},
67+
};
68+
69+
export const baseClassAdapter = {
70+
cliux: {
71+
inquire: jest.fn(),
72+
table: jest.fn(),
73+
print: jest.fn(),
74+
loader: jest.fn(),
75+
registerSearchPlugin: jest.fn(),
76+
confirm: jest.fn(),
77+
prompt: jest.fn(),
78+
styledJSON: jest.fn(),
79+
},
80+
ContentstackClient: jest.fn(),
81+
configHandler: { get: jest.fn() },
82+
authHandler: {
83+
compareOAuthExpiry: jest.fn().mockResolvedValue(undefined),
84+
host: '',
85+
},
86+
};
87+
88+
export const functionsCommand = {};
89+
90+
export const logPolling = {
91+
cliux: {
92+
loaderV2: jest.fn(() => ({})),
93+
},
94+
};

src/util/logs-polling-utilities.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { logPolling as cliUtilitiesJestMock } from '../test/mocks/cli-utilities';
2+
13
type LogPollingCtor = typeof import('./logs-polling-utilities').default;
24

5+
jest.mock('@contentstack/cli-utilities', () => cliUtilitiesJestMock);
6+
37
const CONFIG = {
48
deployment: 'd1',
59
environment: 'e1',

0 commit comments

Comments
 (0)