Skip to content

Commit

Permalink
test: use native test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
sirenkovladd committed Sep 20, 2024
1 parent 7aad3d3 commit 89bc549
Show file tree
Hide file tree
Showing 76 changed files with 1,820 additions and 4,837 deletions.
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
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22
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 => {
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
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 @@ -662,7 +663,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 +691,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 +743,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,8 +1,11 @@
'use strict'

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

process.env.NODE_ENV = 'test'

describe('app.toJSON()', () => {
it('should work', () => {
const app = new Koa()
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
26 changes: 2 additions & 24 deletions __tests__/context/onerror.js → __tests__/context/onerror.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 request = require('supertest')
const Koa = require('../..')
Expand Down Expand Up @@ -70,29 +71,6 @@ describe('ctx.onerror(err)', () => {
assert.strictEqual(Object.prototype.hasOwnProperty.call(res.headers, 'x-csrf-token'), false)
})

it('should ignore error after headerSent', done => {
const app = new Koa()

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

app.use(async ctx => {
ctx.status = 200
ctx.set('X-Foo', 'Bar')
ctx.flushHeaders()
await Promise.reject(new Error('mock error'))
ctx.body = 'response'
})

request(app.callback())
.get('/')
.expect('X-Foo', 'Bar')
.expect(200, () => {})
})

it('should set status specified in the error using statusCode', () => {
const app = new Koa()

Expand Down Expand Up @@ -252,7 +230,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
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
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 request = require('../../test-helpers/context').request
const assert = require('assert')

Expand Down
3 changes: 2 additions & 1 deletion __tests__/request/href.js → __tests__/request/href.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 Stream = require('stream')
const http = require('http')
Expand All @@ -24,7 +25,7 @@ describe('ctx.href', () => {
assert.strictEqual(ctx.href, 'http://localhost/users/1?next=/dashboard')
})

it('should work with `GET http://example.com/foo`', done => {
it('should work with `GET http://example.com/foo`', (t, done) => {
const app = new Koa()
app.use(ctx => {
ctx.body = ctx.href
Expand Down
Loading

0 comments on commit 89bc549

Please sign in to comment.