Skip to content

Commit

Permalink
Merge pull request #974 from forcedotcom/sm/testsetup-without-ts-sino…
Browse files Browse the repository at this point in the history
…n-dep

fix(deps): testSetup has non ts-sinon dependency
  • Loading branch information
mshanemc authored Nov 2, 2023
2 parents 731e56f + 599f23c commit a34c4b7
Showing 1 changed file with 41 additions and 32 deletions.
73 changes: 41 additions & 32 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,9 +340,11 @@ export class TestContext {
})
);

stubMethod(this.SANDBOX, User.prototype, 'retrieve').callsFake(
(username): Promise<UserFields | undefined> => Promise.resolve(mockUsers.find((org) => org.username === username))
);
this.SANDBOX.stub(User.prototype, 'retrieve').callsFake((username): Promise<UserFields> => {
const user = mockUsers.find((org) => org.username === username);
if (!user) throw new SfError('User not found', 'UserNotFoundError');
return Promise.resolve(user);
});

const retrieveContents = async function (this: { path: string }): Promise<{ usernames?: string[] }> {
const orgId = basename(this.path.replace('.json', ''));
Expand All @@ -359,7 +363,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 +536,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 +546,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 +614,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 +642,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

0 comments on commit a34c4b7

Please sign in to comment.