Skip to content

Commit 957b359

Browse files
committed
chore: fix tests and the ci workflows
Signed-off-by: tunnckoCore <5038030+tunnckoCore@users.noreply.github.com>
1 parent 798559d commit 957b359

File tree

4 files changed

+96
-74
lines changed

4 files changed

+96
-74
lines changed

.github/workflows/nodejs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
steps:
3434
- uses: actions/checkout@v2
3535
if: steps.nodejs-cache.outputs.cache-hit != 'true'
36-
- uses: actions/cache@v1
36+
- uses: actions/cache@v2
3737
id: nodejs-cache
3838
name: Cache node modules
3939
with:
@@ -61,7 +61,7 @@ jobs:
6161
steps:
6262
- uses: actions/checkout@v2
6363
if: steps.nodejs-cache.outputs.cache-hit != 'true'
64-
- uses: actions/cache@v1
64+
- uses: actions/cache@v2
6565
id: nodejs-cache
6666
name: Cache node modules
6767
with:

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@
6060
"postreinstall": "yarn setup",
6161
"setup": "yarn",
6262
"pretest": "del-cli ./test/tmp && make-dir ./test/tmp",
63-
"test-specific": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --testPathPattern=test/standalone/keep-alive-error.test.js",
63+
"test-specific": "node --disable-warning=ExperimentalWarning --experimental-vm-modules ./node_modules/jest/bin/jest.js --testPathPattern=test/standalone/keep-alive-error.test.js",
6464
"test": "npm run test-jest && npm run test-node",
65-
"test-jest": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --testPathPattern=test/ --coverage",
66-
"test-node": "node --test test-node/**/*.test.js",
65+
"test-jest": "node --disable-warning=ExperimentalWarning --experimental-vm-modules ./node_modules/jest/bin/jest.js --testPathPattern=test/ --coverage",
66+
"test-node": "node --disable-warning=ExperimentalWarning --test test-node/**/*.test.js",
6767
"pretest:ci": "yarn run pretest",
68-
"test:ci": "node --experimental-vm-modules node_modules/.bin/nyc jest --testPathPattern=test/ --coverage && node --experimental-vm-modules node_modules/.bin/nyc node --test test-node/"
68+
"test:ci": "node --disable-warning=ExperimentalWarning --experimental-vm-modules node_modules/.bin/nyc jest --testPathPattern=test/ --coverage && node --disable-warning=ExperimentalWarning --experimental-vm-modules node_modules/.bin/nyc node --test test-node/"
6969
},
7070
"dependencies": {
7171
"dezalgo": "^1.0.4",
Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1-
import {strictEqual, ok} from 'node:assert';
2-
import { createServer, request } from 'node:http';
3-
import formidable, {errors} from '../../src/index.js';
1+
import { ok, strictEqual } from 'node:assert';
2+
import { createServer } from 'node:http';
43
import test from 'node:test';
5-
6-
const PORT = 13539;
4+
import formidable, { errors } from '../../src/index.js';
75

86
const isPromise = (x) => {
9-
return x && typeof x === `object` && typeof x.then === `function`;
7+
return x && typeof x === `object` && typeof x.then === `function`;
108
};
119

12-
test('parse returns promise if no callback is provided', (t,done) => {
13-
const server = createServer((req, res) => {
10+
let server;
11+
let port = 13540;
12+
13+
test.beforeEach(() => {
14+
// Increment port to avoid conflicts between tests
15+
port += 1;
16+
server = createServer();
17+
});
18+
19+
test.afterEach(() => {
20+
return new Promise((resolve) => {
21+
if (server.listening) {
22+
server.close(() => resolve());
23+
} else {
24+
resolve();
25+
}
26+
});
27+
});
28+
29+
test('parse returns promise if no callback is provided', async (t) => {
30+
server.on('request', (req, res) => {
1431
const form = formidable();
1532

1633
const promise = form.parse(req);
@@ -19,15 +36,16 @@ test('parse returns promise if no callback is provided', (t,done) => {
1936
ok(typeof fields === 'object');
2037
ok(typeof files === 'object');
2138
res.writeHead(200);
22-
res.end("ok")
39+
res.end("ok");
2340
}).catch(e => {
24-
done(e)
25-
})
41+
res.writeHead(500);
42+
res.end(String(e));
43+
});
2644
});
2745

28-
server.listen(PORT, () => {
29-
const chosenPort = server.address().port;
30-
const body = `----13068458571765726332503797717\r
46+
await new Promise(resolve => server.listen(port, resolve));
47+
48+
const body = `----13068458571765726332503797717\r
3149
Content-Disposition: form-data; name="title"\r
3250
\r
3351
a\r
@@ -44,42 +62,39 @@ d\r
4462
\r
4563
----13068458571765726332503797717--\r
4664
`;
47-
fetch(String(new URL(`http:localhost:${chosenPort}/`)), {
48-
method: 'POST',
49-
50-
headers: {
51-
'Content-Length': body.length,
52-
Host: `localhost:${chosenPort}`,
53-
'Content-Type': 'multipart/form-data; boundary=--13068458571765726332503797717',
54-
},
55-
body
56-
}).then(res => {
57-
strictEqual(res.status, 200);
58-
server.close();
59-
done();
60-
});
61-
65+
66+
const res = await fetch(String(new URL(`http:localhost:${port}/`)), {
67+
method: 'POST',
68+
headers: {
69+
'Content-Length': body.length,
70+
Host: `localhost:${port}`,
71+
'Content-Type': 'multipart/form-data; boundary=--13068458571765726332503797717',
72+
},
73+
body
6274
});
75+
76+
strictEqual(res.status, 200);
6377
});
6478

65-
test('parse rejects with promise if it fails', (t,done) => {
66-
const server = createServer((req, res) => {
79+
test('parse rejects with promise if it fails', async (t) => {
80+
server.on('request', (req, res) => {
6781
const form = formidable({minFileSize: 10 ** 6}); // create condition to fail
6882

6983
const promise = form.parse(req);
7084
strictEqual(isPromise(promise), true);
7185
promise.then(() => {
72-
done('should have failed')
86+
res.writeHead(500);
87+
res.end('should have failed');
7388
}).catch(e => {
7489
res.writeHead(e.httpCode);
7590
strictEqual(e.code, errors.smallerThanMinFileSize);
76-
res.end(String(e))
77-
})
91+
res.end(String(e));
92+
});
7893
});
7994

80-
server.listen(PORT, () => {
81-
const chosenPort = server.address().port;
82-
const body = `----13068458571765726332503797717\r
95+
await new Promise(resolve => server.listen(port, resolve));
96+
97+
const body = `----13068458571765726332503797717\r
8398
Content-Disposition: form-data; name="title"\r
8499
\r
85100
a\r
@@ -96,20 +111,16 @@ d\r
96111
\r
97112
----13068458571765726332503797717--\r
98113
`;
99-
fetch(String(new URL(`http:localhost:${chosenPort}/`)), {
100-
method: 'POST',
101-
102-
headers: {
103-
'Content-Length': body.length,
104-
Host: `localhost:${chosenPort}`,
105-
'Content-Type': 'multipart/form-data; boundary=--13068458571765726332503797717',
106-
},
107-
body
108-
}).then(res => {
109-
strictEqual(res.status, 400);
110-
server.close();
111-
done();
112-
});
113-
114+
115+
const res = await fetch(String(new URL(`http:localhost:${port}/`)), {
116+
method: 'POST',
117+
headers: {
118+
'Content-Length': body.length,
119+
Host: `localhost:${port}`,
120+
'Content-Type': 'multipart/form-data; boundary=--13068458571765726332503797717',
121+
},
122+
body
114123
});
124+
125+
strictEqual(res.status, 400);
115126
});

test/standalone/keep-alive-error.test.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
/* eslint-disable max-nested-callbacks */
22

3-
import { createConnection } from 'node:net';
3+
import assert from 'node:assert/strict';
44
import { createServer } from 'node:http';
5-
import { strictEqual } from 'node:assert';
5+
import { createConnection } from 'node:net';
66
import formidable from '../../src/index.js';
77

8+
let server;
9+
let port = 13539;
810
let ok = 0;
911
let errors = 0;
1012

11-
const PORT = 89;
13+
beforeEach(() => {
14+
server = createServer();
15+
ok = 0;
16+
errors = 0;
17+
port += 1;
18+
});
19+
20+
afterEach(() => {
21+
return new Promise((resolve) => {
22+
if (server.listening) {
23+
server.close(() => resolve());
24+
} else {
25+
resolve();
26+
}
27+
});
28+
});
1229

1330
test('keep alive error', (done) => {
14-
const server = createServer(async (req, res) => {
31+
server.on('request', async (req, res) => {
1532
const form = formidable();
1633
form.on('error', () => {
1734
errors += 1;
@@ -26,15 +43,12 @@ test('keep alive error', (done) => {
2643
try {
2744
await form.parse(req);
2845
// for client two
29-
strictEqual(ok, 1, `should "ok" count === 1, has: ${ok}`);
30-
31-
server.close(() => {
32-
done();
33-
});
46+
assert.strictEqual(ok, 1, `should "ok" count === 1, has: ${ok}`);
47+
done();
3448
} catch (formidableError) {
35-
strictEqual(errors, 1, `should "errors" === 1, has: ${errors}`);
49+
assert.strictEqual(errors, 1, `should "errors" === 1, has: ${errors}`);
3650

37-
const clientTwo = createConnection(PORT);
51+
const clientTwo = createConnection(port);
3852

3953
// correct post upload (with hyphens)
4054
clientTwo.write(
@@ -46,13 +60,11 @@ test('keep alive error', (done) => {
4660
'------aaa--\r\n',
4761
);
4862
clientTwo.end();
49-
5063
}
5164
});
5265

53-
server.listen(PORT, () => {
54-
55-
const client = createConnection(PORT);
66+
server.listen(port, () => {
67+
const client = createConnection(port);
5668

5769
// first send malformed (boundary / hyphens) post upload
5870
client.write(
@@ -69,7 +81,6 @@ test('keep alive error', (done) => {
6981
buf.fill('a');
7082
client.write(buf);
7183
client.end();
72-
7384
}, 150);
7485
});
7586
});

0 commit comments

Comments
 (0)