Skip to content

Commit e396760

Browse files
committed
chore: minimal switch [1870]
1 parent 995f160 commit e396760

1 file changed

Lines changed: 78 additions & 90 deletions

File tree

test/test.js

Lines changed: 78 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict'
22

3-
const test = require('node:test')
4-
const assert = require('node:assert')
3+
/* eslint-env jest */
4+
const { describe, it, beforeEach, after } = require('node:test');
55
const compose = require('..')
6+
const assert = require('assert')
67

78
function wait (ms) {
89
return new Promise((resolve) => setTimeout(resolve, ms || 1))
@@ -12,8 +13,8 @@ function isPromise (x) {
1213
return x && typeof x.then === 'function'
1314
}
1415

15-
async function testBaseFunctionality () {
16-
await test('should work', async (t) => {
16+
function testBaseFunctionality() {
17+
it('should work', async () => {
1718
const arr = []
1819
const stack = []
1920

@@ -45,7 +46,7 @@ async function testBaseFunctionality () {
4546
assert.deepStrictEqual(arr.sort(), [1, 2, 3, 4, 5, 6])
4647
})
4748

48-
await test('should be able to be called twice', async (t) => {
49+
it('should be able to be called twice', () => {
4950
const stack = []
5051

5152
stack.push(async (context, next) => {
@@ -77,13 +78,15 @@ async function testBaseFunctionality () {
7778
const ctx2 = { arr: [] }
7879
const out = [1, 2, 3, 4, 5, 6]
7980

80-
await fn(ctx1)
81-
assert.deepStrictEqual(ctx1.arr, out)
82-
await fn(ctx2)
83-
assert.deepStrictEqual(ctx2.arr, out)
81+
return fn(ctx1).then(() => {
82+
assert.deepEqual(out, ctx1.arr)
83+
return fn(ctx2)
84+
}).then(() => {
85+
assert.deepEqual(out, ctx2.arr)
86+
})
8487
})
8588

86-
await test('should create next functions that return a Promise', async (t) => {
89+
it('should create next functions that return a Promise', function () {
8790
const stack = []
8891
const arr = []
8992
for (let i = 0; i < 5; i++) {
@@ -97,19 +100,19 @@ async function testBaseFunctionality () {
97100
compose(stack)({})
98101

99102
for (const next of arr) {
100-
assert.strictEqual(isPromise(next), true, 'one of the functions next is not a Promise')
103+
assert(isPromise(next), 'one of the functions next is not a Promise')
101104
}
102105
})
103106

104-
await test('should work with 0 middleware', async (t) => {
105-
await Promise.all([
107+
it('should work with 0 middleware', function () {
108+
return Promise.all([
106109
compose()({}),
107110
compose([])({}),
108111
compose([], [])({})
109112
])
110113
})
111114

112-
await test('should only accept middleware as functions', (t) => {
115+
it('should only accept middleware as functions', () => {
113116
const badTypes = [1, true, 'string', {}, undefined, Symbol('test')];
114117
[...badTypes, []].forEach((badType) => {
115118
assert.throws(() => compose([badType]), TypeError)
@@ -119,7 +122,7 @@ async function testBaseFunctionality () {
119122
})
120123
})
121124

122-
await test('should work when yielding at the end of the stack', async (t) => {
125+
it('should work when yielding at the end of the stack', async () => {
123126
const stack = []
124127
let called = false
125128

@@ -132,18 +135,18 @@ async function testBaseFunctionality () {
132135
assert(called)
133136
})
134137

135-
await test('should reject on errors in middleware', async (t) => {
138+
it('should reject on errors in middleware', () => {
136139
const stack = []
137140

138141
stack.push(() => { throw new Error() })
139142

140-
await assert.rejects(
143+
return assert.rejects(
141144
compose(stack)({}),
142145
/Error/
143146
)
144147
})
145148

146-
await test('should keep the context', async (t) => {
149+
it('should keep the context', () => {
147150
const ctx = {}
148151

149152
const stack = []
@@ -163,10 +166,10 @@ async function testBaseFunctionality () {
163166
assert.deepStrictEqual(ctx2, ctx)
164167
})
165168

166-
await compose(stack)(ctx)
169+
return compose(stack)(ctx)
167170
})
168171

169-
await test('should catch downstream errors', async (t) => {
172+
it('should catch downstream errors', async () => {
170173
const arr = []
171174
const stack = []
172175

@@ -191,32 +194,34 @@ async function testBaseFunctionality () {
191194
assert.deepStrictEqual(arr, [1, 6, 4, 2, 3])
192195
})
193196

194-
await test('should compose w/ next', async (t) => {
197+
it('should compose w/ next', () => {
195198
let called = false
196199

197-
await compose([])({}, async () => {
200+
return compose([])({}, async () => {
198201
called = true
202+
}).then(function () {
203+
assert(called)
199204
})
200-
assert(called)
201205
})
202206

203-
await test('should handle errors in wrapped non-async functions', async (t) => {
207+
it('should handle errors in wrapped non-async functions', () => {
204208
const stack = []
205209

206210
stack.push(function () {
207211
throw new Error()
208212
})
209213

210-
await assert.rejects(
214+
return assert.rejects(
211215
compose(stack)({}),
212216
/Error/
213217
)
214218
})
215219

216-
await test('should compose w/ other compositions', async (t) => {
220+
// https://github.com/koajs/compose/pull/27#issuecomment-143109739
221+
it('should compose w/ other compositions', () => {
217222
const called = []
218223

219-
await compose([
224+
return compose([
220225
compose([
221226
(ctx, next) => {
222227
called.push(1)
@@ -231,14 +236,12 @@ async function testBaseFunctionality () {
231236
called.push(3)
232237
return next()
233238
}
234-
])({})
235-
236-
assert.deepStrictEqual(called, [1, 2, 3])
239+
])({}).then(() => assert.deepEqual(called, [1, 2, 3]))
237240
})
238241

239-
await test('should return a valid middleware', async (t) => {
242+
it('should return a valid middleware', () => {
240243
let val = 0
241-
await compose([
244+
return compose([
242245
compose([
243246
(ctx, next) => {
244247
val++
@@ -253,12 +256,12 @@ async function testBaseFunctionality () {
253256
val++
254257
return next()
255258
}
256-
])({})
257-
258-
assert.strictEqual(val, 3)
259+
])({}).then(function () {
260+
assert.strictEqual(val, 3)
261+
})
259262
})
260263

261-
await test('should return last return value', async (t) => {
264+
it('should return last return value', () => {
262265
const stack = []
263266

264267
stack.push(async (context, next) => {
@@ -274,29 +277,30 @@ async function testBaseFunctionality () {
274277
})
275278

276279
const next = () => 0
277-
const result = await compose(stack)({}, next)
278-
assert.strictEqual(result, 1)
280+
return compose(stack)({}, next).then(function (val) {
281+
assert.strictEqual(val, 1)
282+
})
279283
})
280284

281-
await test('should not affect the original middleware array', (t) => {
285+
it('should not affect the original middleware array', () => {
282286
const middleware = []
283287
const fn1 = (ctx, next) => {
284288
return next()
285289
}
286290
middleware.push(fn1)
287291

288292
for (const fn of middleware) {
289-
assert.strictEqual(fn, fn1)
293+
assert.equal(fn, fn1)
290294
}
291295

292296
compose(middleware)
293297

294298
for (const fn of middleware) {
295-
assert.strictEqual(fn, fn1)
299+
assert.equal(fn, fn1)
296300
}
297301
})
298302

299-
await test('should not get stuck on the passed in next', async (t) => {
303+
it('should not get stuck on the passed in next', () => {
300304
const middleware = [(ctx, next) => {
301305
ctx.middleware++
302306
return next()
@@ -306,57 +310,34 @@ async function testBaseFunctionality () {
306310
next: 0
307311
}
308312

309-
await compose(middleware)(ctx, (ctx, next) => {
313+
return compose(middleware)(ctx, (ctx, next) => {
310314
ctx.next++
311315
return next()
316+
}).then(() => {
317+
assert.strictEqual(ctx.middleware, 1)
312318
})
313-
314-
assert.deepStrictEqual(ctx, { middleware: 1, next: 1 })
315-
})
316-
317-
await test('production mode', async (t) => {
318-
const OLD_ENV = process.env.NODE_ENV
319-
process.env.NODE_ENV = 'production'
320-
321-
const arr = []
322-
const stack = []
323-
324-
stack.push(async (context, next) => {
325-
arr.push(1)
326-
await next()
327-
arr.push(3)
328-
})
329-
330-
stack.push(async (context, next) => {
331-
arr.push(2)
332-
await next()
333-
})
334-
335-
await compose(stack)({})
336-
assert.deepStrictEqual(arr, [1, 2, 3])
337-
338-
process.env.NODE_ENV = OLD_ENV
339319
})
340320
}
341321

342-
async function testDevErrors () {
343-
await test('should only accept middleware as functions (dev)', (t) => {
322+
function testDevErrors() {
323+
it('should only accept middleware as functions', () => {
344324
assert.throws(() => compose([{}]), TypeError)
345325
})
346326

347-
await test('should throw if next() is called multiple times', async (t) => {
348-
await assert.rejects(
349-
compose([
350-
async (ctx, next) => {
351-
await next()
352-
await next()
353-
}
354-
])({}),
355-
/multiple times/
356-
)
327+
it('should throw if next() is called multiple times', () => {
328+
return compose([
329+
async (ctx, next) => {
330+
await next()
331+
await next()
332+
}
333+
])({}).then(() => {
334+
throw new Error('boom')
335+
}, (err) => {
336+
assert(/multiple times/.test(err.message))
337+
})
357338
})
358339

359-
await test('should detect disconnected promise chains', async (t) => {
340+
it('should detect disconnected promise chains', async () => {
360341
const middleware = [
361342
(ctx, next) => next(),
362343
(ctx, next) => {
@@ -373,15 +354,22 @@ async function testDevErrors () {
373354
)
374355
})
375356
}
357+
describe('Koa Compose', function () {
358+
const OLD_ENV = process.env;
359+
360+
beforeEach(() => {
361+
delete require.cache[require.resolve('..')] // Most important - it clears the cache
362+
process.env = { ...OLD_ENV, NODE_ENV: "production" }; // Make a copy
363+
});
364+
365+
after(() => {
366+
process.env = OLD_ENV; // Restore old environment
367+
});
376368

377-
test('Koa Compose', async (t) => {
378-
const OLD_ENV = process.env.NODE_ENV
379-
process.env.NODE_ENV = 'production'
380-
await testBaseFunctionality()
381-
process.env.NODE_ENV = OLD_ENV
369+
testBaseFunctionality()
382370
})
383371

384-
test('Koa Compose - Dev', async (t) => {
385-
await testBaseFunctionality()
386-
await testDevErrors()
372+
describe('Koa Compose - Dev', function () {
373+
testBaseFunctionality()
374+
testDevErrors()
387375
})

0 commit comments

Comments
 (0)