Skip to content

Commit fb6f4f7

Browse files
authored
Merge pull request #413 from salesforcecli/sm/test-refactor
Sm/test-refactor
2 parents 994109b + 05e5ec8 commit fb6f4f7

4 files changed

Lines changed: 193 additions & 744 deletions

File tree

package.json

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
"main": "lib/index.js",
77
"bugs": "https://github.com/forcedotcom/cli/issues",
88
"dependencies": {
9-
"@oclif/core": "^2.1.1",
10-
"@salesforce/core": "^3.33.1",
11-
"@salesforce/sf-plugins-core": "^2.1.1",
9+
"@oclif/core": "^2.4.0",
10+
"@salesforce/core": "^3.33.5",
11+
"@salesforce/sf-plugins-core": "^2.2.4",
1212
"tslib": "^2"
1313
},
1414
"devDependencies": {
1515
"@oclif/plugin-command-snapshot": "^3.3.6",
1616
"@oclif/plugin-help": "^5.2.5",
1717
"@salesforce/cli-plugins-testkit": "^3.2.18",
1818
"@salesforce/dev-config": "^3.0.1",
19-
"@salesforce/dev-scripts": "^3.1.1",
19+
"@salesforce/dev-scripts": "^4.1.2",
2020
"@salesforce/plugin-command-reference": "^2.4.1",
2121
"@salesforce/prettier-config": "^0.0.2",
2222
"@salesforce/ts-sinon": "1.4.6",
@@ -32,7 +32,7 @@
3232
"eslint-plugin-header": "^3.0.0",
3333
"eslint-plugin-import": "2.27.5",
3434
"eslint-plugin-jsdoc": "^39.9.1",
35-
"eslint-plugin-sf-plugin": "^1.8.1",
35+
"eslint-plugin-sf-plugin": "^1.9.2",
3636
"husky": "^7.0.4",
3737
"mocha": "^9.1.3",
3838
"nyc": "^15.1.0",
@@ -43,7 +43,7 @@
4343
"sinon": "10.0.0",
4444
"ts-node": "^10.0.0",
4545
"typescript": "^4.9.5",
46-
"wireit": "^0.9.3"
46+
"wireit": "^0.9.5"
4747
},
4848
"config": {},
4949
"engines": {
@@ -88,23 +88,18 @@
8888
},
8989
"repository": "salesforcecli/plugin-schema",
9090
"scripts": {
91-
"build": "sf-build",
91+
"build": "wireit",
9292
"clean": "sf-clean",
9393
"clean-all": "sf-clean all",
9494
"clean:lib": "shx rm -rf lib && shx rm -rf coverage && shx rm -rf .nyc_output && shx rm -f oclif.manifest.json",
95-
"compile": "sf-compile",
95+
"compile": "wireit",
9696
"docs": "sf-docs",
97-
"format": "sf-format",
98-
"lint": "sf-lint",
97+
"format": "wireit",
98+
"lint": "wireit",
9999
"postpack": "shx rm -f oclif.manifest.json",
100100
"prepack": "sf-prepack",
101101
"prepare": "sf-install",
102-
"pretest": "sf-compile-test",
103-
"test": "sf-test",
104-
"test:command-reference": "wireit",
105-
"test:compile": "wireit",
106-
"test:deprecation-policy": "wireit",
107-
"test:json-schema": "wireit",
102+
"test": "wireit",
108103
"test:nuts": "nyc mocha \"**/*.nut.ts\" --slow 4500 --timeout 600000 --parallel",
109104
"test:only": "wireit",
110105
"version": "oclif readme"
@@ -133,16 +128,23 @@
133128
"clean": "if-file-deleted"
134129
},
135130
"format": {
136-
"command": "prettier --write \"+(src|test|schemas)/**/*.+(ts|js|json)|command-snapshot.json\""
131+
"command": "prettier --write \"+(src|test|schemas)/**/*.+(ts|js|json)|command-snapshot.json\"",
132+
"files": [
133+
"src/**/*.ts",
134+
"test/**/*.ts",
135+
"schemas/**/*.json",
136+
"command-snapshot.json",
137+
".prettier*"
138+
],
139+
"output": []
137140
},
138141
"lint": {
139142
"command": "eslint src test --color --cache --cache-location .eslintcache",
140143
"files": [
141144
"src/**/*.ts",
142145
"test/**/*.ts",
143146
"messages/**",
144-
".eslintignore",
145-
".eslintrc.js"
147+
".eslint*"
146148
],
147149
"output": []
148150
},
@@ -167,19 +169,26 @@
167169
},
168170
"test:only": {
169171
"command": "nyc mocha \"test/**/*.test.ts\"",
172+
"env": {
173+
"FORCE_COLOR": "2"
174+
},
170175
"files": [
171176
"test/**/*.ts",
172177
"src/**/*.ts",
173178
"tsconfig.json",
174-
"test/tsconfig.json"
179+
".mocha*",
180+
"test/tsconfig.json",
181+
"!*.nut.ts",
182+
".nycrc"
175183
],
176184
"output": []
177185
},
178186
"test:command-reference": {
179187
"command": "\"./bin/dev\" commandreference:generate --erroronwarnings",
180188
"files": [
181189
"src/**/*.ts",
182-
"messages/**"
190+
"messages/**",
191+
"package.json"
183192
],
184193
"output": [
185194
"tmp/root"

test/commands/sobject/describe.test.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,19 @@
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7-
import * as path from 'path';
87
import { isString, AnyJson } from '@salesforce/ts-types';
98
import { TestContext, MockTestOrgData } from '@salesforce/core/lib/testSetup';
10-
import { Config } from '@oclif/core';
119
import { expect } from 'chai';
12-
import stripAnsi = require('strip-ansi');
10+
import { stubSfCommandUx } from '@salesforce/sf-plugins-core';
1311
import { SObjectDescribe } from '../../../src/commands/sobject/describe';
1412

1513
describe('force:schema:sobject:describe', () => {
1614
const $$ = new TestContext();
1715
const testOrg = new MockTestOrgData();
18-
let config: Config;
19-
20-
let stdoutSpy: sinon.SinonSpy;
21-
22-
before(async () => {
23-
config = new Config({ root: path.resolve(__dirname, '../../..') });
24-
await config.load();
25-
});
2616

2717
beforeEach(async () => {
2818
await $$.stubAuths(testOrg);
29-
stdoutSpy = $$.SANDBOX.stub(process.stdout, 'write');
19+
stubSfCommandUx($$.SANDBOX);
3020
});
3121

3222
afterEach(async () => {
@@ -43,15 +33,8 @@ describe('force:schema:sobject:describe', () => {
4333
return Promise.resolve({});
4434
};
4535

46-
const cmd = new SObjectDescribe(['--sobjecttype', 'Account', '-u', 'testUser@test.com', '--json'], config);
47-
48-
// eslint-disable-next-line no-underscore-dangle
49-
await cmd._run();
50-
51-
const jsonOutput = JSON.parse(stripAnsi(stdoutSpy.args.flat().join('')));
52-
53-
expect(jsonOutput.status).to.equal(0);
54-
expect(jsonOutput.result).to.deep.equal(expected);
36+
const result = await SObjectDescribe.run(['--sobjecttype', 'Account', '-u', 'testUser@test.com', '--json']);
37+
expect(result).to.deep.equal(expected);
5538
});
5639

5740
it('logs types correctly with no errors and tooling api', async () => {
@@ -62,14 +45,7 @@ describe('force:schema:sobject:describe', () => {
6245
return Promise.resolve({});
6346
};
6447

65-
const cmd = new SObjectDescribe(['--sobjecttype', 'ApexClass', '-u', 'testUser@test.com', '-t', '--json'], config);
66-
67-
// eslint-disable-next-line no-underscore-dangle
68-
await cmd._run();
69-
70-
const jsonOutput = JSON.parse(stripAnsi(stdoutSpy.args.flat().join('')));
71-
72-
expect(jsonOutput.status).to.equal(0);
73-
expect(jsonOutput.result).to.deep.equal(expected);
48+
const result = await SObjectDescribe.run(['--sobjecttype', 'ApexClass', '-u', 'testUser@test.com', '-t', '--json']);
49+
expect(result).to.deep.equal(expected);
7450
});
7551
});

test/commands/sobject/list.test.ts

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,22 @@
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
import * as os from 'node:os';
8-
import * as path from 'path';
98
import { isString, AnyJson } from '@salesforce/ts-types';
10-
import { TestContext, MockTestOrgData } from '@salesforce/core/lib/testSetup';
11-
import { Config } from '@oclif/core';
12-
import { expect } from 'chai';
13-
import stripAnsi = require('strip-ansi');
9+
import { TestContext, MockTestOrgData, shouldThrow } from '@salesforce/core/lib/testSetup';
10+
import { expect, assert } from 'chai';
11+
import { stubSfCommandUx } from '@salesforce/sf-plugins-core';
12+
import { SfError } from '@salesforce/core';
1413
import { SObjectList } from '../../../src/commands/sobject/list';
1514

1615
describe('force:schema:sobject:list', () => {
1716
const $$ = new TestContext();
1817
const testOrg = new MockTestOrgData();
19-
let config: Config;
20-
21-
let stdoutSpy: sinon.SinonSpy;
22-
23-
before(async () => {
24-
config = new Config({ root: path.resolve(__dirname, '../../..') });
25-
await config.load();
26-
});
18+
let sfCommandUxStubs: ReturnType<typeof stubSfCommandUx>;
2719

2820
beforeEach(async () => {
2921
await $$.stubAuths(testOrg);
30-
stdoutSpy = $$.SANDBOX.stub(process.stdout, 'write');
3122
$$.SANDBOX.stub(process.stderr, 'write');
32-
});
33-
34-
afterEach(async () => {
35-
$$.SANDBOX.restore();
23+
sfCommandUxStubs = stubSfCommandUx($$.SANDBOX);
3624
});
3725

3826
it('should log metadata types correctly', async () => {
@@ -47,14 +35,11 @@ describe('force:schema:sobject:list', () => {
4735
}
4836
return Promise.resolve({});
4937
};
50-
const cmd = new SObjectList(['--sobjecttypecategory', 'all', '-u', 'testUser@test.com'], config);
5138

52-
// eslint-disable-next-line no-underscore-dangle
53-
await cmd._run();
54-
55-
const stdout = stdoutSpy.args.flat().join('');
56-
57-
expect(stdout).includes(`customMDT${os.EOL}defaultMDT`);
39+
await SObjectList.run(['--sobjecttypecategory', 'all', '-u', 'testUser@test.com']);
40+
expect(sfCommandUxStubs.log.getCalls().flatMap((call) => call.args)).to.deep.include(
41+
`customMDT${os.EOL}defaultMDT`
42+
);
5843
});
5944

6045
it('should print the error when describeGlobal method fails', async () => {
@@ -64,15 +49,13 @@ describe('force:schema:sobject:list', () => {
6449
}
6550
return Promise.resolve({});
6651
};
67-
const cmd = new SObjectList(['--sobjecttypecategory', 'all', '-u', 'testUser@test.com', '--json'], config);
68-
69-
// eslint-disable-next-line no-underscore-dangle
70-
await cmd._run();
71-
72-
const jsonOutput = JSON.parse(stripAnsi(stdoutSpy.args.flat().join('')));
7352

74-
expect(jsonOutput.status).to.equal(1);
75-
expect(jsonOutput.message).to.equal('describeGlobal query failed');
53+
try {
54+
await shouldThrow(SObjectList.run(['--sobjecttypecategory', 'all', '-u', 'testUser@test.com', '--json']));
55+
} catch (e) {
56+
assert(e instanceof SfError);
57+
expect(e.message).to.equal('describeGlobal query failed');
58+
}
7659
});
7760

7861
it('should print the "noTypeFound" msg when no sobjects are found', async () => {
@@ -84,11 +67,8 @@ describe('force:schema:sobject:list', () => {
8467
}
8568
throw new Error('Unexpected request');
8669
};
87-
const cmd = new SObjectList(['-u', 'testUser@test.com'], config);
70+
await SObjectList.run(['-u', 'testUser@test.com']);
8871

89-
// eslint-disable-next-line no-underscore-dangle
90-
await cmd._run();
91-
const stdout = stdoutSpy.args.flat().join('');
92-
expect(stdout).to.include('No ALL objects found.\n');
72+
expect(sfCommandUxStubs.log.getCalls().flatMap((call) => call.args)).to.deep.include('No ALL objects found.');
9373
});
9474
});

0 commit comments

Comments
 (0)