Skip to content

Commit b8c2e29

Browse files
authoredOct 3, 2022
Merge pull request #792 from lamweili/refactor/test
test: 100% coverage
2 parents 5543d67 + da1e842 commit b8c2e29

File tree

5 files changed

+110
-22
lines changed

5 files changed

+110
-22
lines changed
 

‎.github/workflows/node.js.yml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: Node.js CI
5+
6+
on: [push, pull_request]
7+
8+
jobs:
9+
build:
10+
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
include:
16+
- node-version: 10.x
17+
test-on-old-node: 1
18+
- node-version: 12.x
19+
test-on-old-node: 1
20+
- node-version: 14.x
21+
- node-version: 16.x
22+
- node-version: 18.x
23+
24+
steps:
25+
- uses: actions/checkout@v3
26+
- name: Use Node.js ${{ matrix.node-version }}
27+
uses: actions/setup-node@v3
28+
with:
29+
node-version: ${{ matrix.node-version }}
30+
cache: 'npm'
31+
- name: Install Dependencies On Old Node ${{ matrix.node-version }}
32+
if: ${{ matrix.test-on-old-node == '1' }}
33+
run: node ci/remove-deps-4-old-node.js && yarn install --ignore-scripts
34+
- name: Install Dependencies On Node ${{ matrix.node-version }}
35+
if: ${{ matrix.test-on-old-node != '1' }}
36+
run: yarn install
37+
- run: npm test
38+
- name: Coverage On Node ${{ matrix.node-version }}
39+
run:
40+
npm run coverage

‎ci/remove-deps-4-old-node.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const package = require('../package.json');
4+
5+
const UNSUPPORT_DEPS_4_OLD = {
6+
'eslint': undefined,
7+
'mocha': '6.x'
8+
};
9+
10+
const deps = Object.keys(UNSUPPORT_DEPS_4_OLD);
11+
for (const item in package.devDependencies) {
12+
if (deps.includes(item)) {
13+
package.devDependencies[item] = UNSUPPORT_DEPS_4_OLD[item];
14+
}
15+
}
16+
17+
delete package.scripts.lint;
18+
19+
fs.writeFileSync(
20+
path.join(__dirname, '../package.json'),
21+
JSON.stringify(package, null, 2)
22+
);

‎lib/agent.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ const Test = require('./test.js');
2020
function TestAgent(app, options) {
2121
if (!(this instanceof TestAgent)) return new TestAgent(app, options);
2222
if (typeof app === 'function') app = http.createServer(app); // eslint-disable-line no-param-reassign
23-
if (options) {
24-
this._ca = options.ca;
25-
this._key = options.key;
26-
this._cert = options.cert;
27-
}
28-
Agent.call(this);
23+
Agent.call(this, options);
Has conversations. Original line has conversations.
2924
this.app = app;
3025
}
3126

@@ -44,19 +39,17 @@ TestAgent.prototype.host = function(host) {
4439
// override HTTP verb methods
4540
methods.forEach(function(method) {
4641
TestAgent.prototype[method] = function(url, fn) { // eslint-disable-line no-unused-vars
47-
const req = new Test(this.app, method.toUpperCase(), url, this._host);
48-
req.ca(this._ca);
49-
req.cert(this._cert);
50-
req.key(this._key);
42+
const req = new Test(this.app, method.toUpperCase(), url);
43+
5144
if (this._host) {
5245
req.set('host', this._host);
5346
}
5447

5548
req.on('response', this._saveCookies.bind(this));
5649
req.on('redirect', this._saveCookies.bind(this));
5750
req.on('redirect', this._attachCookies.bind(this, req));
58-
this._attachCookies(req);
5951
this._setDefaults(req);
52+
this._attachCookies(req);
6053

6154
return req;
6255
};

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"scripts": {
2828
"lint": "eslint lib/**/*.js test/**/*.js index.js",
2929
"lint:fix": "eslint --fix lib/**/*.js test/**/*.js index.js",
30-
"pretest": "npm run lint",
30+
"pretest": "npm run lint --if-present",
3131
"test": "nyc --reporter=html --reporter=text mocha --exit --require should --reporter spec --check-leaks",
3232
"coverage": "nyc report --reporter=text-lcov | coveralls"
3333
},

‎test/supertest.js

+43-10
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ function shouldIncludeStackWithThisFile(err) {
2020
describe('request(url)', function () {
2121
it('should be supported', function (done) {
2222
const app = express();
23-
let s;
23+
let server;
2424

2525
app.get('/', function (req, res) {
2626
res.send('hello');
2727
});
2828

29-
s = app.listen(function () {
30-
const url = 'http://localhost:' + s.address().port;
29+
server = app.listen(function () {
30+
const url = 'http://localhost:' + server.address().port;
3131
request(url)
3232
.get('/')
3333
.expect('hello', done);
@@ -37,14 +37,14 @@ describe('request(url)', function () {
3737
describe('.end(cb)', function () {
3838
it('should set `this` to the test object when calling cb', function (done) {
3939
const app = express();
40-
let s;
40+
let server;
4141

4242
app.get('/', function (req, res) {
4343
res.send('hello');
4444
});
4545

46-
s = app.listen(function () {
47-
const url = 'http://localhost:' + s.address().port;
46+
server = app.listen(function () {
47+
const url = 'http://localhost:' + server.address().port;
4848
const test = request(url).get('/');
4949
test.end(function (err, res) {
5050
this.should.eql(test);
@@ -80,7 +80,7 @@ describe('request(app)', function () {
8080
res.send('hey');
8181
});
8282

83-
server = app.listen(4000, function () {
83+
server = app.listen(function () {
8484
request(server)
8585
.get('/')
8686
.end(function (err, res) {
@@ -93,13 +93,15 @@ describe('request(app)', function () {
9393

9494
it('should work with remote server', function (done) {
9595
const app = express();
96+
let server;
9697

9798
app.get('/', function (req, res) {
9899
res.send('hey');
99100
});
100101

101-
app.listen(4001, function () {
102-
request('http://localhost:4001')
102+
server = app.listen(function () {
103+
const url = 'http://localhost:' + server.address().port;
104+
request(url)
103105
.get('/')
104106
.end(function (err, res) {
105107
res.status.should.equal(200);
@@ -1269,7 +1271,7 @@ describe('request.get(url).query(vals) works as expected', function () {
12691271
});
12701272
});
12711273

1272-
it('handles unknown errors', function (done) {
1274+
it('handles unknown errors (err without res)', function (done) {
12731275
const app = express();
12741276

12751277
nock.disableNetConnect();
@@ -1285,6 +1287,8 @@ describe('request.get(url).query(vals) works as expected', function () {
12851287
// https://github.com/visionmedia/supertest/issues/352
12861288
.expect(200)
12871289
.end(function (err, res) {
1290+
should.exist(err);
1291+
should.not.exist(res);
12881292
err.should.be.an.instanceof(Error);
12891293
err.message.should.match(/Nock: Disallowed net connect/);
12901294
shouldIncludeStackWithThisFile(err);
@@ -1294,6 +1298,35 @@ describe('request.get(url).query(vals) works as expected', function () {
12941298
nock.restore();
12951299
});
12961300

1301+
// this scenario should never happen
1302+
// there shouldn't be any res if there is an err
1303+
// meant for test coverage for lib/test.js#169
1304+
// https://github.com/visionmedia/supertest/blob/5543d674cf9aa4547927ba6010d31d9474950dec/lib/test.js#L169
1305+
it('handles unknown errors (err with res)', function (done) {
1306+
const app = express();
1307+
1308+
app.get('/', function (req, res) {
1309+
res.status(200).send('OK');
1310+
});
1311+
1312+
const resError = new Error();
1313+
resError.status = 400;
1314+
1315+
const serverRes = { status: 200 };
1316+
1317+
request(app)
1318+
.get('/')
1319+
// private api
1320+
.assert(resError, serverRes, function (err, res) {
1321+
should.exist(err);
1322+
should.exist(res);
1323+
err.should.equal(resError);
1324+
res.should.equal(serverRes);
1325+
// close the server explicitly (as we are not using expect/end/then)
1326+
this.end(done);
1327+
});
1328+
});
1329+
12971330
it('should assert using promises', function (done) {
12981331
const app = express();
12991332

0 commit comments

Comments
 (0)
Please sign in to comment.