Skip to content

Commit

Permalink
Use node test runner (#1845)
Browse files Browse the repository at this point in the history
## Checklist
use `node --test` instead of `jest`
See: #1837 (comment)

- [X] I have ensured my pull request is not behind the main or master
branch of the original repository.
- [X] I have rebased all commits where necessary so that reviewing this
pull request can be done without having to merge it first.
- [X] I have written a commit message that passes commitlint linting.
- [X] I have ensured that my code changes pass linting tests.
- [X] I have ensured that my code changes pass unit tests.
- [X] I have described my pull request and the reasons for code changes
along with context if necessary.
  • Loading branch information
sirenkovladd authored Oct 28, 2024
1 parent 988ebec commit ce6b3b6
Show file tree
Hide file tree
Showing 79 changed files with 1,842 additions and 4,828 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
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 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([]))
})
})

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 => {
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

0 comments on commit ce6b3b6

Please sign in to comment.