Skip to content

Commit

Permalink
fix(deps): testSetup has non ts-sinon dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Nov 2, 2023
1 parent 731e56f commit 9fcb206
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions src/testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { basename, join as pathJoin, dirname } from 'path';
import * as util from 'util';
import { SinonSandbox, SinonStatic, SinonStub } from 'sinon';

import { stubMethod } from '@salesforce/ts-sinon';
import {
AnyFunction,
AnyJson,
Expand Down Expand Up @@ -291,9 +290,12 @@ export class TestContext {
);
const orgMap = new Map(entries);

stubMethod(this.SANDBOX, OrgAccessor.prototype, 'getAllFiles').resolves([...orgMap.keys()].map((o) => `${o}.json`));
// @ts-expect-error because private method
this.SANDBOX.stub(OrgAccessor.prototype, 'getAllFiles').resolves([...orgMap.keys()].map((o) => `${o}.json`));

stubMethod(this.SANDBOX, OrgAccessor.prototype, 'hasFile').callsFake((username: string) => orgMap.has(username));
this.SANDBOX.stub(OrgAccessor.prototype, 'hasFile').callsFake((username: string) =>
Promise.resolve(orgMap.has(username))
);

const retrieveContents = async function (this: { path: string }): Promise<AuthFields> {
const username = basename(this.path.replace('.json', ''));
Expand Down Expand Up @@ -338,7 +340,8 @@ export class TestContext {
})
);

stubMethod(this.SANDBOX, User.prototype, 'retrieve').callsFake(
this.SANDBOX.stub(User.prototype, 'retrieve').callsFake(
// @ts-expect-error the real method guarantees a user, but find might not return one
(username): Promise<UserFields | undefined> => Promise.resolve(mockUsers.find((org) => org.username === username))
);

Expand All @@ -359,7 +362,8 @@ export class TestContext {
)) as Array<[string, SandboxFields]>;
const sandboxMap = new Map(entries);

stubMethod(this.SANDBOX, SandboxAccessor.prototype, 'getAllFiles').resolves(
// @ts-expect-error because private method
this.SANDBOX.stub(SandboxAccessor.prototype, 'getAllFiles').resolves(
[...sandboxMap.keys()].map((o) => `${o}.sandbox.json`)
);

Expand Down Expand Up @@ -531,8 +535,8 @@ export const stubContext = (testContext: TestContext): Record<string, SinonStub>
const stubs: Record<string, SinonStub> = {};

// Most core files create a child logger so stub this to return our test logger.
stubMethod(testContext.SANDBOX, Logger, 'child').resolves(testContext.TEST_LOGGER);
stubMethod(testContext.SANDBOX, Logger, 'childFromRoot').returns(testContext.TEST_LOGGER);
testContext.SANDBOX.stub(Logger, 'child').resolves(testContext.TEST_LOGGER);
testContext.SANDBOX.stub(Logger, 'childFromRoot').returns(testContext.TEST_LOGGER);
testContext.inProject(true);
testContext.SANDBOXES.CONFIG.stub(ConfigFile, 'resolveRootFolder').callsFake((isGlobal: boolean) =>
testContext.rootPathRetriever(isGlobal, testContext.id)
Expand All @@ -541,7 +545,8 @@ export const stubContext = (testContext: TestContext): Record<string, SinonStub>
testContext.rootPathRetrieverSync(isGlobal, testContext.id)
);

stubMethod(testContext.SANDBOXES.PROJECT, SfProjectJson.prototype, 'doesPackageExist').callsFake(() => true);
// @ts-expect-error using private method
testContext.SANDBOXES.PROJECT.stub(SfProjectJson.prototype, 'doesPackageExist').callsFake(() => true);

const initStubForRead = (configFile: ConfigFile<ConfigFile.Options>): ConfigStub => {
const stub: ConfigStub = testContext.configStubs[configFile.constructor.name] ?? {};
Expand Down Expand Up @@ -608,23 +613,24 @@ export const stubContext = (testContext: TestContext): Record<string, SinonStub>
}
};

stubs.configWriteSync = stubMethod(testContext.SANDBOXES.CONFIG, ConfigFile.prototype, 'writeSync').callsFake(
writeSync
);
stubs.configWriteSync = testContext.SANDBOXES.CONFIG.stub(ConfigFile.prototype, 'writeSync').callsFake(writeSync);

stubs.configWrite = stubMethod(testContext.SANDBOXES.CONFIG, ConfigFile.prototype, 'write').callsFake(write);
stubs.configWrite = testContext.SANDBOXES.CONFIG.stub(ConfigFile.prototype, 'write').callsFake(write);

stubMethod(testContext.SANDBOXES.CRYPTO, Crypto.prototype, 'getKeyChain').callsFake(() =>
// @ts-expect-error: getKeyChain is private
testContext.SANDBOXES.CRYPTO.stub(Crypto.prototype, 'getKeyChain').callsFake(() =>
// @ts-expect-error: not the full type
Promise.resolve({
setPassword: () => Promise.resolve(),
getPassword: (data: Record<string, unknown>, cb: AnyFunction) =>
cb(undefined, '12345678901234567890123456789012'),
})
);

stubMethod(testContext.SANDBOXES.CONNECTION, Connection.prototype, 'isResolvable').resolves(true);
testContext.SANDBOXES.CONNECTION.stub(Connection.prototype, 'isResolvable').resolves(true);

stubMethod(testContext.SANDBOXES.CONNECTION, Connection.prototype, 'request').callsFake(function (
// @ts-expect-error: just enough of an httpResponse for testing
testContext.SANDBOXES.CONNECTION.stub(Connection.prototype, 'request').callsFake(function (
this: Connection,
request: string,
options?: Dictionary
Expand All @@ -635,23 +641,25 @@ export const stubContext = (testContext: TestContext): Record<string, SinonStub>
return testContext.fakeConnectionRequest.call(this, request, options as AnyJson);
});

stubMethod(testContext.SANDBOX, aliasAccessorEntireFile, 'getFileLocation').returns(getAliasFileLocation());
testContext.SANDBOX.stub(aliasAccessorEntireFile, 'getFileLocation').returns(getAliasFileLocation());

stubs.configExists = stubMethod(testContext.SANDBOXES.ORGS, OrgAccessor.prototype, 'exists').callsFake(
async function (this: OrgAccessor, username: string): Promise<boolean | undefined> {
// @ts-expect-error because private member
if ([...this.contents.keys()].includes(username)) return Promise.resolve(true);
else return Promise.resolve(false);
}
);
stubs.configExists = testContext.SANDBOXES.ORGS.stub(OrgAccessor.prototype, 'exists').callsFake(async function (
this: OrgAccessor,
username: string
): Promise<boolean> {
// @ts-expect-error because private member
if ([...this.contents.keys()].includes(username)) return Promise.resolve(true);
else return Promise.resolve(false);
});

stubs.configRemove = stubMethod(testContext.SANDBOXES.ORGS, OrgAccessor.prototype, 'remove').callsFake(
async function (this: OrgAccessor, username: string): Promise<boolean | undefined> {
// @ts-expect-error because private member
if ([...this.contents.keys()].includes(username)) return Promise.resolve(true);
else return Promise.resolve(false);
}
);
stubs.configRemove = testContext.SANDBOXES.ORGS.stub(OrgAccessor.prototype, 'remove').callsFake(async function (
this: OrgAccessor,
username: string
): Promise<void> {
// @ts-expect-error because private member
if ([...this.contents.keys()].includes(username)) return Promise.resolve();
else return Promise.resolve();
});

// Always start with the default and tests beforeEach or it methods can override it.
testContext.fakeConnectionRequest = defaultFakeConnectionRequest;
Expand Down

3 comments on commit 9fcb206

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - ubuntu-latest

Benchmark suite Current: 9fcb206 Previous: 9d99e61 Ratio
Child logger creation 412810 ops/sec (±13.76%) 474829 ops/sec (±1.31%) 1.15
Logging a string on root logger 537411 ops/sec (±9.94%) 486835 ops/sec (±10.67%) 0.91
Logging an object on root logger 285532 ops/sec (±15.92%) 382325 ops/sec (±11.56%) 1.34
Logging an object with a message on root logger 191690 ops/sec (±23.68%) 276471 ops/sec (±13.26%) 1.44
Logging an object with a redacted prop on root logger 180328 ops/sec (±19.31%) 271589 ops/sec (±15.43%) 1.51
Logging a nested 3-level object on root logger 141407 ops/sec (±20.92%) 3360 ops/sec (±209.82%) 0.023761199940597

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - windows-latest

Benchmark suite Current: 9fcb206 Previous: 9d99e61 Ratio
Child logger creation 560868 ops/sec (±0.91%) 373101 ops/sec (±6.19%) 0.67
Logging a string on root logger 775020 ops/sec (±5.51%) 410241 ops/sec (±13.08%) 0.53
Logging an object on root logger 527407 ops/sec (±8.26%) 263545 ops/sec (±14.01%) 0.50
Logging an object with a message on root logger 16870 ops/sec (±184.85%) 161199 ops/sec (±19.68%) 9.56
Logging an object with a redacted prop on root logger 399750 ops/sec (±20.25%) 160561 ops/sec (±23.64%) 0.40
Logging a nested 3-level object on root logger 285018 ops/sec (±7.36%) 109823 ops/sec (±24.51%) 0.39

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Logger Benchmarks - windows-latest'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 9fcb206 Previous: 9d99e61 Ratio
Logging an object with a message on root logger 16870 ops/sec (±184.85%) 161199 ops/sec (±19.68%) 9.56

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.