Skip to content

Commit 124d2a7

Browse files
committed
fix intermittent unit test failures
1 parent 6899d06 commit 124d2a7

1 file changed

Lines changed: 55 additions & 49 deletions

File tree

test/unit/npm/npm-dir-test.js

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -58,51 +58,57 @@ describe('npm-dir', () => {
5858

5959
it('skips install if package.json and node_modules exists', (done) => {
6060

61-
dir._exists
62-
.withArgs('package.json').returns(true)
63-
.withArgs('node_modules').returns(true);
64-
65-
dir.install({})
66-
.then((result) => {
67-
expect(result.skipped).to.eql(true);
68-
done();
69-
})
70-
.catch(done);
61+
withCloseHandler(() => {
62+
dir._exists
63+
.withArgs('package.json').returns(true)
64+
.withArgs('node_modules').returns(true);
7165

72-
setTimeout(function () {
73-
handlers.close(0);
74-
})
66+
dir.install({})
67+
.then((result) => {
68+
expect(result.skipped).to.eql(true);
69+
done();
70+
})
71+
.catch(done);
72+
73+
});
7574
});
7675

7776
it('writes package.json when installing', (done) => {
7877

79-
dir.install({})
80-
.then(() => {
81-
sinon.assert.calledWith(fs.writeJsonSync, path.join(__dirname, 'package.json'), sinon.match.object);
82-
done();
83-
})
84-
.catch(done);
85-
//trigger
86-
setTimeout(function () {
87-
handlers.close(0);
88-
})
78+
withCloseHandler(() => {
79+
dir.install({})
80+
.then(() => {
81+
sinon.assert.calledWith(fs.writeJsonSync, path.join(__dirname, 'package.json'), sinon.match.object);
82+
done();
83+
})
84+
.catch(done);
85+
});
8986
});
9087

9188
it('calls \'npm install\' in a child_process', (done) => {
92-
93-
dir.install({})
94-
.then(() => {
95-
sinon.assert.calledWith(childProcess.spawn, 'npm', ['install'], { cwd: __dirname });
96-
done();
97-
})
98-
.catch(done);
99-
100-
setTimeout(function () {
101-
handlers.close(0);
89+
withCloseHandler(() => {
90+
dir.install({})
91+
.then(() => {
92+
sinon.assert.calledWith(childProcess.spawn, 'npm', ['install'], { cwd: __dirname });
93+
done();
94+
})
95+
.catch(done);
10296
});
10397
});
10498
});
10599

100+
let withCloseHandler = (bodyFn) => {
101+
bodyFn();
102+
let close = () => {
103+
if (handlers.close) {
104+
handlers.close(0);
105+
} else {
106+
setTimeout(close);
107+
}
108+
}
109+
close();
110+
}
111+
106112
describe('installMoreDependencies', () => {
107113
let dir;
108114
beforeEach(() => {
@@ -111,21 +117,24 @@ describe('npm-dir', () => {
111117
});
112118

113119
it('skips the install if all the dependencies exist', (done) => {
114-
dir._exists
115-
.withArgs('node_modules/a').returns(true)
116-
.withArgs('node_modules/b').returns(true);
117120

118-
dir.installMoreDependencies({ a: '1.0.0', b: '1.0.0' })
119-
.then((result) => {
120-
expect(result.skipped).to.eql(true);
121-
done();
122-
}).catch(done);
121+
withCloseHandler(() => {
122+
dir._exists
123+
.withArgs('node_modules/a').returns(true)
124+
.withArgs('node_modules/b').returns(true);
125+
126+
dir.installMoreDependencies({ a: '1.0.0', b: '1.0.0' })
127+
.then((result) => {
128+
expect(result.skipped).to.eql(true);
129+
done();
130+
}).catch(done);
123131

124-
setTimeout(function () {
125-
handlers.close(0);
126132
});
133+
});
134+
135+
it('calls \'npm install a@1.0.0\'', (done) => {
127136

128-
it('calls \'npm install a@1.0.0\'', (done) => {
137+
withCloseHandler(() => {
129138
dir._exists
130139
.withArgs('node_modules/a').returns(false)
131140
.withArgs('node_modules/b').returns(true);
@@ -136,10 +145,7 @@ describe('npm-dir', () => {
136145
done();
137146
}).catch(done);
138147

139-
setTimeout(function () {
140-
handlers.close(0);
141-
});
142148
});
143-
})
144-
});
149+
});
150+
})
145151
});

0 commit comments

Comments
 (0)