Skip to content

Commit a850fc2

Browse files
committed
fix helper tests
1 parent 5711f2e commit a850fc2

File tree

3 files changed

+40
-53
lines changed

3 files changed

+40
-53
lines changed

test/app/extend/context.jsonp.test.js test/app/extend/context.jsonp.test.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
'use strict';
1+
import { createApp, MockApplication } from '../../utils.js';
22

3-
const mm = require('egg-mock');
4-
const utils = require('../../utils');
5-
6-
describe('test/app/extend/context.jsonp.test.js', () => {
7-
let app;
3+
describe('test/app/extend/context.jsonp.test.ts', () => {
4+
let app: MockApplication;
85
before(() => {
9-
app = utils.app('apps/demo');
6+
app = createApp('apps/demo');
107
return app.ready();
118
});
129
after(() => app.close());
13-
afterEach(mm.restore);
1410

1511
it('should response jsonp', () => {
1612
return app.httpRequest()

test/app/extend/helper.test.js test/app/extend/helper.test.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
'use strict';
1+
import { createApp, MockApplication } from '../../utils.js';
22

3-
const utils = require('../../utils');
4-
5-
describe('test/app/extend/helper.test.js', () => {
6-
let app;
3+
describe('test/app/extend/helper.test.ts', () => {
4+
let app: MockApplication;
75
before(() => {
8-
app = utils.app('apps/helper');
6+
app = createApp('apps/helper');
97
return app.ready();
108
});
119
after(() => app.close());

test/app/extend/request.test.js test/app/extend/request.test.ts

+32-39
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
'use strict';
1+
import { strict as assert } from 'node:assert';
2+
import { once } from 'node:events';
3+
import urllib from 'urllib';
4+
import { createApp, MockApplication, restore, mm } from '../../utils.js';
25

3-
const assert = require('assert');
4-
const mm = require('egg-mock');
5-
const urllib = require('urllib');
6-
const utils = require('../../utils');
7-
8-
describe('test/app/extend/request.test.js', () => {
6+
describe('test/app/extend/request.test.ts', () => {
97
describe('normal', () => {
10-
let app;
8+
let app: MockApplication;
119
let ctx;
12-
let req;
10+
let req: any;
1311
before(() => {
14-
app = utils.app('apps/demo');
12+
app = createApp('apps/demo');
1513
return app.ready();
1614
});
1715
after(() => app.close());
1816
beforeEach(() => {
1917
ctx = app.mockContext();
2018
req = ctx.request;
2119
});
22-
afterEach(mm.restore);
20+
afterEach(restore);
2321

2422
describe('req.host', () => {
2523
it('should return host with port', () => {
@@ -180,8 +178,8 @@ describe('test/app/extend/request.test.js', () => {
180178

181179
it('should return value from socket.encrypted', () => {
182180
const ctx = app.mockContext();
183-
ctx.request.socket.encrypted = true;
184-
assert(ctx.request.protocol === 'https');
181+
(ctx.request as any).socket.encrypted = true;
182+
assert.equal(ctx.request.protocol, 'https');
185183
});
186184
});
187185

@@ -208,7 +206,7 @@ describe('test/app/extend/request.test.js', () => {
208206
});
209207

210208
describe('this.query[key] => String', () => {
211-
function expectQuery(querystring, query) {
209+
function expectQuery(querystring: any, query: any) {
212210
mm(req, 'querystring', querystring);
213211
assert.deepEqual(req.query, query);
214212
mm.restore();
@@ -240,7 +238,7 @@ describe('test/app/extend/request.test.js', () => {
240238
});
241239

242240
describe('this.queries[key] => Array', () => {
243-
function expectQueries(querystring, query) {
241+
function expectQueries(querystring: any, query: any) {
244242
mm(req, 'querystring', querystring);
245243
assert.deepEqual(req.queries, query);
246244
mm.restore();
@@ -370,41 +368,36 @@ describe('test/app/extend/request.test.js', () => {
370368
});
371369

372370
describe('work with egg app', () => {
373-
let app;
374-
let host;
371+
let app: MockApplication;
372+
let host: string;
375373
before(() => {
376-
app = utils.app('apps/querystring-extended');
374+
app = createApp('apps/querystring-extended');
377375
return app.ready();
378376
});
379-
before(done => {
380-
app.listen(0, function() {
381-
host = `http://127.0.0.1:${this.address().port}`;
382-
done();
383-
});
377+
before(async () => {
378+
const server = app.listen(0);
379+
await once(server, 'listening');
380+
host = `http://127.0.0.1:${server.address().port}`;
384381
});
385382
after(() => app.close());
386383

387-
it('should return query and queries', done => {
388-
urllib.request(`${host}/?p=a,b&p=b,c&a[foo]=bar`, {
384+
it('should return query and queries', async () => {
385+
const res = await urllib.request(`${host}/?p=a,b&p=b,c&a[foo]=bar`, {
389386
dataType: 'json',
390-
}, (err, body) => {
391-
assert.deepEqual(body, {
392-
query: { p: 'a,b', 'a[foo]': 'bar' },
393-
queries: { p: [ 'a,b', 'b,c' ], 'a[foo]': [ 'bar' ] },
394-
});
395-
done(err);
387+
});
388+
assert.deepEqual(res.data, {
389+
query: { p: 'a,b', 'a[foo]': 'bar' },
390+
queries: { p: [ 'a,b', 'b,c' ], 'a[foo]': [ 'bar' ] },
396391
});
397392
});
398393

399-
it('should work with encodeURIComponent', done => {
400-
urllib.request(`${host}/?p=a,b&p=b,c&${encodeURIComponent('a[foo]')}=bar`, {
394+
it('should work with encodeURIComponent', async () => {
395+
const res = await urllib.request(`${host}/?p=a,b&p=b,c&${encodeURIComponent('a[foo]')}=bar`, {
401396
dataType: 'json',
402-
}, (err, body) => {
403-
assert.deepEqual(body, {
404-
query: { p: 'a,b', 'a[foo]': 'bar' },
405-
queries: { p: [ 'a,b', 'b,c' ], 'a[foo]': [ 'bar' ] },
406-
});
407-
done(err);
397+
});
398+
assert.deepEqual(res.data, {
399+
query: { p: 'a,b', 'a[foo]': 'bar' },
400+
queries: { p: [ 'a,b', 'b,c' ], 'a[foo]': [ 'bar' ] },
408401
});
409402
});
410403
});

0 commit comments

Comments
 (0)