Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use node test runner #1845

Merged
merged 5 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run lint
- run: npm test -- --coverage --maxWorkers 2
- run: npm run test:coverage
- run: npx codecov
continue-on-error: true
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')

describe('app', () => {
// ignore test on Node.js v18
(/^v18\./.test(process.version) ? it.skip : it)('should handle socket errors', done => {
(/^v18\./.test(process.version) ? it.skip : it)('should handle socket errors', (t, done) => {
const app = new Koa()

app.use((ctx, next) => {
Expand All @@ -24,28 +25,6 @@ describe('app', () => {
.end(() => {})
})

it('should not .writeHead when !socket.writable', done => {
kevinpeno marked this conversation as resolved.
Show resolved Hide resolved
const app = new Koa()

app.use((ctx, next) => {
// set .writable to false
ctx.socket.writable = false
ctx.status = 204
// throw if .writeHead or .end is called
ctx.res.writeHead =
ctx.res.end = () => {
throw new Error('response sent')
}
})

// hackish, but the response should occur in a single tick
setImmediate(done)

request(app.callback())
.get('/')
.end(() => {})
})

it('should set development env when NODE_ENV missing', () => {
const NODE_ENV = process.env.NODE_ENV
process.env.NODE_ENV = ''
Expand Down Expand Up @@ -80,7 +59,7 @@ describe('app', () => {

it('should set compose from the constructor', () => {
const compose = () => (ctx) => {}
const app = new Koa({ compose })
const app = new Koa.default({ compose }) // eslint-disable-line new-cap
assert.strictEqual(app.compose, compose)
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const util = require('util')
const Koa = require('../..')

process.env.NODE_ENV = 'test'
const app = new Koa()

describe('app.inspect()', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it, mock } = require('node:test')
const assert = require('assert')
const Koa = require('../..')

Expand Down Expand Up @@ -30,21 +31,21 @@ describe('app.onerror(err)', () => {

err.status = 404

const spy = jest.spyOn(console, 'error')
const spy = mock.method(console, 'error', () => {})
app.onerror(err)
expect(spy).not.toHaveBeenCalled()
spy.mockRestore()
assert.strictEqual(spy.mock.calls.length, 0)
spy.mock.restore()
})

it('should do nothing if .silent', () => {
const app = new Koa()
app.silent = true
const err = new Error()

const spy = jest.spyOn(console, 'error')
const spy = mock.method(console, 'error', () => {})
app.onerror(err)
expect(spy).not.toHaveBeenCalled()
spy.mockRestore()
assert.strictEqual(spy.mock.calls.length, 0)
spy.mock.restore()
})

it('should log the error to stderr', () => {
Expand All @@ -54,9 +55,9 @@ describe('app.onerror(err)', () => {
const err = new Error()
err.stack = 'Foo'

const spy = jest.spyOn(console, 'error')
const spy = mock.method(console, 'error', () => {})
app.onerror(err)
expect(spy).toHaveBeenCalled()
spy.mockRestore()
assert.notStrictEqual(spy.mock.calls.length, 0)
spy.mock.restore()
})
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const statuses = require('statuses')
const assert = require('assert')
Expand Down Expand Up @@ -555,6 +556,20 @@ describe('app.respond', () => {
.expect(200)
.expect('content-type', 'application/octet-stream')
})

it('should respond hello', async () => {
const app = new Koa()

app.use(async ctx => {
ctx.body = new Blob(['hello']).stream()
})

return request(app.callback())
.get('/')
.expect(200)
.expect('content-type', 'application/octet-stream')
.expect(Buffer.from('hello'))
})
})

describe('when .body is a Response', () => {
Expand All @@ -577,13 +592,14 @@ describe('app.respond', () => {

app.use(ctx => {
ctx.body = new Response(null, { status: 200, statusText: 'OK' })
console.log(ctx)
})

return request(app.callback())
.head('/')
.get('/')
.expect(200)
.expect('content-type', 'application/octet-stream')
.expect('content-length', '2')
.expect(Buffer.from([]))
kevinpeno marked this conversation as resolved.
Show resolved Hide resolved
})
})

Expand Down Expand Up @@ -662,7 +678,7 @@ describe('app.respond', () => {
assert.deepStrictEqual(res.body, pkg)
})

it('should handle errors', done => {
it('should handle errors', (t, done) => {
const app = new Koa()

app.use(ctx => {
Expand Down Expand Up @@ -690,7 +706,7 @@ describe('app.respond', () => {
.expect(204)
})

it('should handle all intermediate stream body errors', done => {
it('should handle all intermediate stream body errors', (t, done) => {
const app = new Koa()

app.use(ctx => {
Expand Down Expand Up @@ -742,7 +758,7 @@ describe('app.respond', () => {
})

describe('when an error occurs', () => {
it('should emit "error" on the app', done => {
it('should emit "error" on the app', (t, done) => {
const app = new Koa()

app.use(ctx => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const Koa = require('../..')

describe('app.toJSON()', () => {
it('should work', () => {
const app = new Koa()
const app = new Koa({ env: 'test' })
const obj = app.toJSON()

assert.deepStrictEqual({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const context = require('../../test-helpers/context')
const assert = require('assert')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const request = require('supertest')
const Koa = require('../..')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const prototype = require('../../lib/context')
const assert = require('assert')
const util = require('util')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const request = require('supertest')
const Koa = require('../..')
Expand Down Expand Up @@ -70,12 +71,13 @@ describe('ctx.onerror(err)', () => {
assert.strictEqual(Object.prototype.hasOwnProperty.call(res.headers, 'x-csrf-token'), false)
})

it('should ignore error after headerSent', done => {
kevinpeno marked this conversation as resolved.
Show resolved Hide resolved
it('should ignore error after headerSent', (t, done) => {
const app = new Koa()

app.on('error', err => {
app.on('error', (err, { res }) => {
assert.strictEqual(err.message, 'mock error')
assert.strictEqual(err.headerSent, true)
res.end()
done()
})

Expand Down Expand Up @@ -252,7 +254,7 @@ describe('ctx.onerror(err)', () => {
assert.strictEqual(removed, 2)
})

it('should stringify error if it is an object', done => {
it('should stringify error if it is an object', (t, done) => {
const app = new Koa()

app.on('error', err => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const context = require('../../test-helpers/context')
const assert = require('assert')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const context = require('../../test-helpers/context')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { describe, it } = require('node:test')
const sp = require('../../lib/search-params')
const assert = require('assert')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { describe, it, beforeAll } = require('node:test')
const assert = require('assert')

let importESM = () => {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const Accept = require('accepts')
const assert = require('assert')
const context = require('../../test-helpers/context')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const context = require('../../test-helpers/context')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const context = require('../../test-helpers/context')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const context = require('../../test-helpers/context')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const context = require('../../test-helpers/context')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('../../test-helpers/context').request
const assert = require('assert')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const context = require('../../test-helpers/context')

Expand Down
1 change: 1 addition & 0 deletions __tests__/request/get.js → __tests__/request/get.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const context = require('../../test-helpers/context')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const request = require('../../test-helpers/context').request

Expand Down
Loading
Loading