Skip to content

Commit 72fb681

Browse files
committed
fix: chain test behavior
Fixes mutation of the mockPushProcessors
1 parent bb1df64 commit 72fb681

2 files changed

Lines changed: 61 additions & 40 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"@material-ui/icons": "4.11.3",
3838
"@primer/octicons-react": "^19.8.0",
3939
"@seald-io/nedb": "^4.0.2",
40-
"axios": "^1.6.7",
40+
"axios": "^1.6.0",
4141
"bcryptjs": "^2.4.3",
4242
"bit-mask": "^1.0.2",
4343
"body-parser": "^1.20.1",

test/chain.test.js

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,44 @@ const mockLoader = {
1414
],
1515
};
1616

17-
const mockPushProcessors = {
18-
parsePush: sinon.stub(),
19-
audit: sinon.stub(),
20-
checkRepoInAuthorisedList: sinon.stub(),
21-
checkCommitMessages: sinon.stub(),
22-
checkAuthorEmails: sinon.stub(),
23-
checkUserPushPermission: sinon.stub(),
24-
checkIfWaitingAuth: sinon.stub(),
25-
pullRemote: sinon.stub(),
26-
writePack: sinon.stub(),
27-
getDiff: sinon.stub(),
28-
clearBareClone: sinon.stub(),
29-
scanDiff: sinon.stub(),
30-
blockForAuth: sinon.stub(),
17+
const clearCache = (sinon) => {
18+
delete require.cache[require.resolve('../src/proxy/processors')];
19+
delete require.cache[require.resolve('../src/proxy/chain')];
20+
sinon.restore();
21+
};
22+
23+
const initMockPushProcessors = (sinon) => {
24+
const mockPushProcessors = {
25+
parsePush: sinon.stub(),
26+
audit: sinon.stub(),
27+
checkRepoInAuthorisedList: sinon.stub(),
28+
checkCommitMessages: sinon.stub(),
29+
checkAuthorEmails: sinon.stub(),
30+
checkUserPushPermission: sinon.stub(),
31+
checkIfWaitingAuth: sinon.stub(),
32+
pullRemote: sinon.stub(),
33+
writePack: sinon.stub(),
34+
getDiff: sinon.stub(),
35+
clearBareClone: sinon.stub(),
36+
scanDiff: sinon.stub(),
37+
blockForAuth: sinon.stub(),
38+
};
39+
mockPushProcessors.parsePush.displayName = 'parsePush';
40+
mockPushProcessors.audit.displayName = 'audit';
41+
mockPushProcessors.checkRepoInAuthorisedList.displayName = 'checkRepoInAuthorisedList';
42+
mockPushProcessors.checkCommitMessages.displayName = 'checkCommitMessages';
43+
mockPushProcessors.checkAuthorEmails.displayName = 'checkAuthorEmails';
44+
mockPushProcessors.checkUserPushPermission.displayName = 'checkUserPushPermission';
45+
mockPushProcessors.checkIfWaitingAuth.displayName = 'checkIfWaitingAuth';
46+
mockPushProcessors.pullRemote.displayName = 'pullRemote';
47+
mockPushProcessors.writePack.displayName = 'writePack';
48+
mockPushProcessors.getDiff.displayName = 'getDiff';
49+
mockPushProcessors.clearBareClone.displayName = 'clearBareClone';
50+
mockPushProcessors.scanDiff.displayName = 'scanDiff';
51+
mockPushProcessors.blockForAuth.displayName = 'blockForAuth';
52+
53+
return mockPushProcessors;
3154
};
32-
mockPushProcessors.parsePush.displayName = 'parsePush';
33-
mockPushProcessors.audit.displayName = 'audit';
34-
mockPushProcessors.checkRepoInAuthorisedList.displayName = 'checkRepoInAuthorisedList';
35-
mockPushProcessors.checkCommitMessages.displayName = 'checkCommitMessages';
36-
mockPushProcessors.checkAuthorEmails.displayName = 'checkAuthorEmails';
37-
mockPushProcessors.checkUserPushPermission.displayName = 'checkUserPushPermission';
38-
mockPushProcessors.checkIfWaitingAuth.displayName = 'checkIfWaitingAuth';
39-
mockPushProcessors.pullRemote.displayName = 'pullRemote';
40-
mockPushProcessors.writePack.displayName = 'writePack';
41-
mockPushProcessors.getDiff.displayName = 'getDiff';
42-
mockPushProcessors.clearBareClone.displayName = 'clearBareClone';
43-
mockPushProcessors.scanDiff.displayName = 'scanDiff';
44-
mockPushProcessors.blockForAuth.displayName = 'blockForAuth';
4555

4656
const mockPreProcessors = {
4757
parseAction: sinon.stub(),
@@ -50,27 +60,30 @@ const mockPreProcessors = {
5060
describe('proxy chain', function () {
5161
let processors;
5262
let chain;
63+
let sandboxSinon;
64+
let mockPushProcessors;
65+
66+
beforeEach(async () => {
67+
sandboxSinon = sinon.createSandbox();
68+
69+
// Init mock processors
70+
mockPushProcessors = initMockPushProcessors(sandboxSinon);
5371

54-
beforeEach(() => {
5572
// Re-require the processors module after clearing the cache
5673
processors = require('../src/proxy/processors');
5774

5875
// Mock the processors module
59-
sinon.stub(processors, 'pre').value(mockPreProcessors);
60-
61-
sinon.stub(processors, 'push').value(mockPushProcessors);
76+
sandboxSinon.stub(processors, 'pre').value(mockPreProcessors);
77+
sandboxSinon.stub(processors, 'push').value(mockPushProcessors);
6278

6379
// Re-require the chain module after stubbing processors
6480
chain = require('../src/proxy/chain');
6581

66-
chain.chainPluginLoader = new PluginLoader([])
82+
chain.chainPluginLoader = new PluginLoader([]);
6783
});
6884

6985
afterEach(() => {
70-
// Clear the module from the cache after each test
71-
delete require.cache[require.resolve('../src/proxy/processors')];
72-
delete require.cache[require.resolve('../src/proxy/chain')];
73-
sinon.reset();
86+
clearCache(sandboxSinon);
7487
});
7588

7689
it('getChain should set pluginLoaded if loader is undefined', async function () {
@@ -108,7 +121,11 @@ describe('proxy chain', function () {
108121
mockPushProcessors.checkUserPushPermission.resolves(continuingAction);
109122

110123
// this stops the chain from further execution
111-
mockPushProcessors.checkIfWaitingAuth.resolves({ type: 'push', continue: () => false, allowPush: false });
124+
mockPushProcessors.checkIfWaitingAuth.resolves({
125+
type: 'push',
126+
continue: () => false,
127+
allowPush: false,
128+
});
112129
const result = await chain.executeChain(req);
113130

114131
expect(mockPreProcessors.parseAction.called).to.be.true;
@@ -136,7 +153,11 @@ describe('proxy chain', function () {
136153
mockPushProcessors.checkAuthorEmails.resolves(continuingAction);
137154
mockPushProcessors.checkUserPushPermission.resolves(continuingAction);
138155
// this stops the chain from further execution
139-
mockPushProcessors.checkIfWaitingAuth.resolves({ type: 'push', continue: () => true, allowPush: true });
156+
mockPushProcessors.checkIfWaitingAuth.resolves({
157+
type: 'push',
158+
continue: () => true,
159+
allowPush: true,
160+
});
140161
const result = await chain.executeChain(req);
141162

142163
expect(mockPreProcessors.parseAction.called).to.be.true;
@@ -232,5 +253,5 @@ describe('proxy chain', function () {
232253
expect(mockPushProcessors.checkRepoInAuthorisedList.called).to.be.false;
233254
expect(mockPushProcessors.parsePush.called).to.be.false;
234255
expect(result).to.deep.equal(action);
235-
})
256+
});
236257
});

0 commit comments

Comments
 (0)