Skip to content

Commit

Permalink
fix: fixed mocking error
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Sep 23, 2020
1 parent a221b2d commit f5cfe52
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export interface TestContext {
* @param value The actual stub contents. The Mock data.
*/
setConfigStubContents(name: string, value: ConfigContents): void;
inProject(inProject: boolean): void;
}

const uniqid = (): string => {
Expand Down Expand Up @@ -286,6 +287,24 @@ export const instantiateContext = (sinon?: any) => {
this.configStubs[name] = value;
}
},
inProject(inProject = true) {
testContext.SANDBOXES.PROJECT.restore();
if (inProject) {
testContext.SANDBOXES.PROJECT.stub(SfdxProject, 'resolveProjectPath').returns(
testContext.localPathRetriever(testContext.id)
);
testContext.SANDBOXES.PROJECT.stub(SfdxProject, 'resolveProjectPathSync').returns(
testContext.localPathRetrieverSync(testContext.id)
);
} else {
testContext.SANDBOXES.PROJECT.stub(SfdxProject, 'resolveProjectPath').rejects(
new SfdxError('InvalidProjectWorkspace')
);
testContext.SANDBOXES.PROJECT.stub(SfdxProject, 'resolveProjectPathSync').throws(
new SfdxError('InvalidProjectWorkspace')
);
}
},
};
return testContext;
};
Expand Down Expand Up @@ -318,14 +337,7 @@ export const stubContext = (testContext: TestContext) => {
// Most core files create a child logger so stub this to return our test logger.
stubMethod(testContext.SANDBOX, Logger, 'child').returns(Promise.resolve(testContext.TEST_LOGGER));
stubMethod(testContext.SANDBOX, Logger, 'childFromRoot').returns(testContext.TEST_LOGGER);

testContext.SANDBOXES.PROJECT.stub(SfdxProject, 'resolveProjectPath').callsFake(() =>
testContext.localPathRetriever(testContext.id)
);
testContext.SANDBOXES.PROJECT.stub(SfdxProject, 'resolveProjectPathSync').callsFake(() =>
testContext.localPathRetrieverSync(testContext.id)
);

testContext.inProject(true);
testContext.SANDBOXES.CONFIG.stub(ConfigFile, 'resolveRootFolder').callsFake((isGlobal: boolean) =>
testContext.rootPathRetriever(isGlobal, testContext.id)
);
Expand Down

0 comments on commit f5cfe52

Please sign in to comment.