Skip to content

Commit 4f8ae35

Browse files
committed
working on 8.0, vitest, maybe no browserify
1 parent 3568477 commit 4f8ae35

18 files changed

+84
-74
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "roads",
3-
"version": "7.4.1",
3+
"version": "8.0.0-alpha.0",
44
"author": {
55
"name": "Aaron Hedges",
66
"email": "[email protected]",

src/tests/__tests__/client/testRequest.ts test/__tests__/client/request.test.ts

+22-29
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
/* eslint-disable @typescript-eslint/ban-ts-comment */
2-
import Client from '../../../client/request';
2+
import Client from '../../../src/client/request';
33
import createServer, { port } from '../../resources/mockServer';
44
import { Server as HttpServer } from 'http';
55
import { Server as HttpsServer } from 'https';
6-
import Response from '../../../core/response';
7-
import fetch, { Headers, Request, Response as FetchResponse } from 'node-fetch';
6+
import Response from '../../../src/core/response';
87

9-
10-
if (!globalThis.fetch) {
11-
// @ts-ignore
12-
globalThis.fetch = fetch;
13-
// @ts-ignore
14-
globalThis.Headers = Headers;
15-
// @ts-ignore
16-
globalThis.Request = Request;
17-
// @ts-ignore
18-
globalThis.Response = FetchResponse;
19-
}
8+
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
209

2110
describe('request', () => {
2211
let server: HttpServer | HttpsServer;
@@ -50,20 +39,22 @@ describe('request', () => {
5039
one : 'two'
5140
}).then(function (response: Response) {
5241
expect(response.status).toEqual(200);
53-
expect(response.body).toEqual(JSON.stringify({
42+
expect(JSON.parse(response.body as string)).toEqual({
5443
url: '/',
5544
method: 'GET',
5645
body: '',
5746
headers: {
5847
one: 'two',
5948
accept: '*/*',
60-
'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
61-
'accept-encoding': 'gzip,deflate',
62-
connection: 'close',
49+
'user-agent': 'node',
50+
'accept-encoding': 'gzip, deflate',
51+
'accept-language': '*',
52+
'sec-fetch-mode': 'cors',
53+
connection: 'keep-alive',
6354
host: `127.0.0.1:${port}`,
6455
},
6556
message: 'hello!'
66-
}));
57+
});
6758
expect(response.headers['this-is']).toEqual('for real');
6859
}));
6960
});
@@ -78,21 +69,23 @@ describe('request', () => {
7869
one : ['two', 'three']
7970
}).then(function (response: Response) {
8071
expect(response.status).toEqual(200);
81-
expect(response.body).toEqual(JSON.stringify({
72+
expect(JSON.parse(response.body as string)).toEqual({
8273
url: '/',
8374
method: 'GET',
8475
body: '',
8576
headers: {
8677
// node fetch doesn't seem to retain dupe arrays, this is what we get.
8778
one: 'two, three',
8879
accept: '*/*',
89-
'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
90-
'accept-encoding': 'gzip,deflate',
91-
connection: 'close',
80+
'user-agent': 'node',
81+
'accept-encoding': 'gzip, deflate',
82+
'accept-language': '*',
83+
connection: 'keep-alive',
9284
host: `127.0.0.1:${port}`,
85+
'sec-fetch-mode': 'cors',
9386
},
9487
message: 'hello!'
95-
}));
88+
});
9689
// I don't think this is correct for dupliate headers, it seems to be something node-fetch is doing,
9790
// not sure if it's spec accurate: https://github.com/node-fetch/node-fetch/issues/771
9891
expect(response.headers['cache-control']).toEqual('no-cache, no-store');
@@ -114,7 +107,7 @@ describe('request', () => {
114107
}).then(function (response: Response) {
115108
expect(response.status).toEqual(200);
116109

117-
expect(response.body).toEqual(JSON.stringify({
110+
expect(JSON.parse(response.body as string)).toMatchObject({
118111
url: '/',
119112
method: 'POST',
120113
body: '{"yeah": "what"}',
@@ -123,13 +116,13 @@ describe('request', () => {
123116
'content-type': 'text/plain;charset=UTF-8',
124117
accept: '*/*',
125118
'content-length': '16',
126-
'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
127-
'accept-encoding': 'gzip,deflate',
128-
connection: 'close',
119+
'user-agent': 'node',
120+
'accept-encoding': 'gzip, deflate',
121+
connection: 'keep-alive',
129122
host: `127.0.0.1:${port}`,
130123
},
131124
message: 'hello!'
132-
}));
125+
});
133126

134127
expect(response.headers['content-type']).toEqual('application/json');
135128
}));

src/tests/__tests__/middleware/testApplyToContext.ts test/__tests__/middleware/applyToContext.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { build } from '../../../middleware/applyToContext';
2+
import { build } from '../../../src/middleware/applyToContext';
3+
import { describe, expect, test } from 'vitest';
34

45
describe('ApplyToContext tests', () => {
56
test('test apply to context applies context', () => {

src/tests/__tests__/middleware/testBasicRouter.ts test/__tests__/middleware/basicRouter.test.ts

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as url_module from 'url';
22

3-
import { BasicRouter, Route, BasicRouterURL } from '../../../middleware/basicRouter';
4-
import Road from '../../../core/road';
5-
import Response from '../../../core/response';
6-
import { Context, NextCallback } from '../../../core/road';
3+
import { BasicRouter, Route, BasicRouterURL } from '../../../src/middleware/basicRouter';
4+
import Road from '../../../src/core/road';
5+
import Response from '../../../src/core/response';
6+
import { Context, NextCallback } from '../../../src/core/road';
7+
8+
import { describe, expect, test, assert } from 'vitest';
79

810
const router_file_test_path = `${__dirname }/../../resources/_router_file_test.js`;
911

@@ -82,9 +84,7 @@ describe('Basic Router Tests', () => {
8284
};
8385

8486
router.addRoute('PUT', path, fn);
85-
router.addRoute('POST', path, () => {
86-
fail('POST route should not run');
87-
});
87+
router.addRoute('POST', path, () => assert.fail('POST route should not run'));
8888
router['_middleware'](router['_routes'], method, path, '', {
8989
'x-http-method-override': 'PUT'
9090
}, next);
@@ -109,9 +109,7 @@ describe('Basic Router Tests', () => {
109109
};
110110

111111
router.addRoute('GET', path, fn);
112-
router.addRoute('PUT', path, () => {
113-
fail('PUT route should not run');
114-
});
112+
router.addRoute('PUT', path, () => assert.fail('PUT route should not run'));
115113
router['_middleware'](router['_routes'], method, path, '', {
116114
'x-http-method-override': 'PUT'
117115
}, next);
@@ -136,9 +134,7 @@ describe('Basic Router Tests', () => {
136134
};
137135

138136
router.addRoute('PUT', path, fn);
139-
router.addRoute('POST', path, () => {
140-
fail('POST route should not run');
141-
});
137+
router.addRoute('POST', path, () => assert.fail('POST route should not run'));
142138
router['_middleware'](router['_routes'], method, `${path}?_method=PUT`, '', {}, next);
143139

144140
expect(route_hit).toEqual(true);
@@ -161,9 +157,7 @@ describe('Basic Router Tests', () => {
161157
};
162158

163159
router.addRoute('GET', path, fn);
164-
router.addRoute('PUT', path, () => {
165-
fail('PUT route should not run');
166-
});
160+
router.addRoute('PUT', path, () => assert.fail('PUT route should not run'));
167161

168162
router['_middleware'](router['_routes'], method, `${path}?_method=PUT`, '', {}, next);
169163

src/tests/__tests__/middleware/testCookie.ts test/__tests__/middleware/cookie.test.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { serverMiddleware, buildClientMiddleware } from '../../../middleware/cookieMiddleware';
2+
import { serverMiddleware, buildClientMiddleware } from '../../../src/middleware/cookieMiddleware';
33

4-
import { CookieContext } from '../../../middleware/cookieMiddleware';
5-
import Response from '../../../core/response';
4+
import { CookieContext } from '../../../src/middleware/cookieMiddleware';
5+
import Response from '../../../src/core/response';
6+
7+
import { describe, expect, test } from 'vitest';
68

79
describe('cookie tests', () => {
810
test('test cookie middleware parses cookies into context', () => {

src/tests/__tests__/middleware/testCors.ts test/__tests__/middleware/cors.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { build } from '../../../middleware/cors';
1+
import { build } from '../../../src/middleware/cors';
2+
import { describe, expect, test } from 'vitest';
23

34
describe('Cors tests', () => {
45
test('test cors middleware doesn\'t break normal', () => {

src/tests/__tests__/middleware/testLastModified.ts test/__tests__/middleware/lastModified.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22

3-
import { middleware, ModifiedSinceContext } from '../../../middleware/modifiedSince';
4-
import Response from '../../../core/response';
3+
import { middleware, ModifiedSinceContext } from '../../../src/middleware/modifiedSince';
4+
import Response from '../../../src/core/response';
5+
6+
import { describe, expect, test } from 'vitest';
57

68
describe('modified sine tests', () => {
79
test('test not-yet-updated endpoints return 304', async () => {

src/tests/__tests__/middleware/testParseRequestBody.ts test/__tests__/middleware/parseRequestBody.test.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { middleware } from '../../../middleware/parseBody';
2+
import { middleware } from '../../../src/middleware/parseBody';
33

4-
import { Context, Middleware as MiddlewareType } from '../../../core/road';
5-
import { Road } from '../../../index';
6-
import Response from '../../../core/response';
4+
import { Context, Middleware as MiddlewareType } from '../../../src/core/road';
5+
import { Road } from '../../../src/index';
6+
import Response from '../../../src/core/response';
7+
8+
import { describe, expect, test, assert } from 'vitest';
79

810
describe('Parse Request Body tests', () => {
911
test('test request with valid json body', () => {
@@ -26,7 +28,7 @@ describe('Parse Request Body tests', () => {
2628

2729
// eslint-disable-next-line @typescript-eslint/no-empty-function
2830
const response = middleware.call(context, '', '', body, {'content-type': 'application/json'}, () => {
29-
fail('Next should not be called if the request body can not be parsed');
31+
assert.fail('Next should not be called if the request body can not be parsed');
3032
});
3133

3234
expect(context.body).toBe(undefined);

src/tests/__tests__/middleware/testRemoveTrailingSlashes.ts test/__tests__/middleware/removeTrailingSlashes.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { middleware } from '../../../middleware/removeTrailingSlash';
1+
import { middleware } from '../../../src/middleware/removeTrailingSlash';
22

3-
import Response from '../../../core/response';
3+
import Response from '../../../src/core/response';
4+
5+
import { describe, expect, test } from 'vitest';
46

57
describe('KillSlashes tests', () => {
68
test('test remove slash doesn\'t break normal', () => {

src/tests/__tests__/middleware/testReroute.ts test/__tests__/middleware/reroute.test.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22

3-
import { build } from '../../../middleware/reroute';
4-
import Road from '../../../core/road';
5-
import { Context, Middleware as MiddlewareType } from '../../../core/road';
6-
import Response from '../../../core/response';
3+
import { build } from '../../../src/middleware/reroute';
4+
import Road from '../../../src/core/road';
5+
import { Context, Middleware as MiddlewareType } from '../../../src/core/road';
6+
import Response from '../../../src/core/response';
7+
8+
import { describe, expect, test } from 'vitest';
79

810
describe('Reroute middleware tests', () => {
911
/**

src/tests/__tests__/middleware/testStoreVals.ts test/__tests__/middleware/storeVals.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { middleware } from '../../../middleware/storeVals';
2+
import { middleware } from '../../../src/middleware/storeVals';
33

4+
import { describe, expect, test } from 'vitest';
45

56
describe('Store Values', () => {
67

src/tests/__tests__/testResponse.ts test/__tests__/response.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Response } from '../../index';
1+
import { Response } from '../../src/index';
2+
3+
import { describe, expect, test } from 'vitest';
24

35
describe('response tests', () => {
46
/**

src/tests/__tests__/road/testBuildNext.ts test/__tests__/road/buildNext.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { Road } from '../../../index';
2-
import { Response } from '../../../index';
1+
import { Road } from '../../../src/index';
2+
import { Response } from '../../../src/index';
3+
4+
import { describe, expect, test } from 'vitest';
35

46
// Note: This file used to have many more tests, but a recent roads change invalidated most of them, and the
57
// migration to jest made it clear that many of them were

src/tests/__tests__/road/testRoadContext.ts test/__tests__/road/roadContext.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

2-
import { Road } from '../../../index';
3-
import { Context } from '../../../core/road';
2+
import { Road } from '../../../src/index';
3+
import { Context } from '../../../src/core/road';
4+
5+
import { describe, expect, test } from 'vitest';
46

57
interface confirmContext {
68
confirmString: () => string

src/tests/__tests__/road/testRoadRequest.ts test/__tests__/road/roadRequest.test.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { Road } from '../../../index';
2-
import { Context, Middleware } from '../../../core/road';
3-
import { Response } from '../../../index';
1+
import { Road } from '../../../src/index';
2+
import { Context, Middleware } from '../../../src/core/road';
3+
import { Response } from '../../../src/index';
4+
5+
import { describe, expect, test } from 'vitest';
46

57
describe('road request', () => {
68
/**

src/tests/__tests__/road/testRoadUse.ts test/__tests__/road/roadUse.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Road } from '../../../index';
1+
import { Road } from '../../../src/index';
2+
3+
import { describe, expect, test } from 'vitest';
24

35
describe('road use', () => {
46
/**
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)