Skip to content

Commit

Permalink
test: check every explicit warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 5, 2024
1 parent 0a0da4b commit e0a0eed
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"esbuild": "^0.18.20",
"esbuild-register": "^3.5.0",
"eslint": "^8.57.0",
"mocha": "^9.2.2",
"shx": "^0.3.4",
"tsx": "patch:tsx@npm%3A4.7.0#./.yarn/patches/tsx-npm-4.7.0-86d7b66640.patch",
"typescript": "^5.4.3",
Expand Down
8 changes: 6 additions & 2 deletions packages/core/tests/fork.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context } from '../src'
import { noop } from 'cosmokit'
import { defineProperty, noop } from 'cosmokit'
import { expect } from 'chai'
import { mock } from 'node:test'
import { event, Filter, Session, filter } from './utils'
Expand Down Expand Up @@ -92,6 +92,8 @@ describe('Fork', () => {
it('deferred execution', () => {
const root = new Context()
root.provide('foo')
const warning = mock.fn()
root.on('internal/warning', warning)
const listener = mock.fn()
const callback = mock.fn((ctx: Context) => {
ctx.on(event, listener)
Expand All @@ -105,8 +107,10 @@ describe('Fork', () => {

root.plugin(plugin)
expect(callback.mock.calls).to.have.length(0)
expect(warning.mock.calls).to.have.length(0)
root.plugin(plugin)
expect(callback.mock.calls).to.have.length(0)
expect(warning.mock.calls).to.have.length(1) // FIXME should be 0 maybe?
root.emit(event)
expect(listener.mock.calls).to.have.length(0)

Expand All @@ -133,7 +137,7 @@ describe('Fork', () => {

it('state.uid', () => {
const root = new Context()
const callback1 = mock.fn()
const callback1 = defineProperty(mock.fn(), 'reusable', true)
expect(root.state.uid).to.equal(0)

const fork1 = root.plugin(callback1)
Expand Down
4 changes: 4 additions & 0 deletions packages/core/tests/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ describe('Plugin', () => {
it('apply duplicate plugin', () => {
const root = new Context()
const callback = mock.fn()
const warning = mock.fn()
root.on('internal/warning', warning)
root.plugin({ apply: callback })
expect(callback.mock.calls).to.have.length(1)
expect(warning.mock.calls).to.have.length(0)
root.plugin({ apply: callback }) // duplicate
expect(callback.mock.calls).to.have.length(1)
expect(warning.mock.calls).to.have.length(1)
})

it('apply plugin when dispose', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/tests/service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,20 @@ describe('Service', () => {
}

const root = new Context()
const warning = mock.fn()
root.on('internal/warning', warning)
root.plugin(Foo)
root.foo.increse()
expect(root.foo.size).to.equal(1)

const fork = root.plugin((ctx) => {
const fork = root.inject(['foo'], (ctx) => {
ctx.foo.increse()
expect(ctx.foo.size).to.equal(2)
})

fork.dispose()
expect(root.foo.size).to.equal(1)
expect(warning.mock.calls).to.have.length(0)
})

it('dependency update', async () => {
Expand Down
13 changes: 13 additions & 0 deletions packages/core/tests/status.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ describe('Status', () => {
it('invalid config (main)', async () => {
const root = new Context()
const callback = mock.fn()
const error = mock.fn()
root.on('internal/error', error)
const apply = mock.fn((ctx: Context) => {
ctx.on(event, callback)
})
Expand All @@ -19,12 +21,14 @@ describe('Status', () => {
expect(fork.status).to.equal(ScopeStatus.FAILED)
expect(fork.runtime.status).to.equal(ScopeStatus.FAILED)
expect(apply.mock.calls).to.have.length(0)
expect(error.mock.calls).to.have.length(1)

fork.update({ foo: true })
await checkError(root)
expect(fork.status).to.equal(ScopeStatus.ACTIVE)
expect(fork.runtime.status).to.equal(ScopeStatus.ACTIVE)
expect(apply.mock.calls).to.have.length(1)
expect(error.mock.calls).to.have.length(1)

root.emit(event)
expect(callback.mock.calls).to.have.length(1)
Expand All @@ -33,6 +37,8 @@ describe('Status', () => {
it('invalid plugin (fork)', async () => {
const root = new Context()
const callback = mock.fn()
const error = mock.fn()
root.on('internal/error', error)
const apply = mock.fn((ctx: Context) => {
ctx.on(event, callback)
})
Expand All @@ -48,6 +54,7 @@ describe('Status', () => {
expect(fork1.status).to.equal(ScopeStatus.FAILED)
expect(fork2.status).to.equal(ScopeStatus.ACTIVE)
expect(apply.mock.calls).to.have.length(1)
expect(error.mock.calls).to.have.length(1)

root.emit(event)
expect(callback.mock.calls).to.have.length(1)
Expand All @@ -56,6 +63,8 @@ describe('Status', () => {
it('plugin error (main)', async () => {
const root = new Context()
const callback = mock.fn()
const error = mock.fn()
root.on('internal/error', error)
const apply = mock.fn((ctx: Context) => {
ctx.on(event, callback)
throw new Error('plugin error')
Expand All @@ -66,6 +75,7 @@ describe('Status', () => {
expect(fork.runtime.status).to.equal(ScopeStatus.FAILED)
expect(fork.status).to.equal(ScopeStatus.ACTIVE)
expect(apply.mock.calls).to.have.length(1)
expect(error.mock.calls).to.have.length(1)

root.emit(event)
expect(callback.mock.calls).to.have.length(0)
Expand All @@ -74,6 +84,8 @@ describe('Status', () => {
it('plugin error (fork)', async () => {
const root = new Context()
const callback = mock.fn()
const error = mock.fn()
root.on('internal/error', error)
const apply = mock.fn((ctx: Context, config: { foo?: boolean } | undefined) => {
ctx.on(event, callback)
if (!config?.foo) throw new Error('plugin error')
Expand All @@ -85,6 +97,7 @@ describe('Status', () => {
expect(fork1.status).to.equal(ScopeStatus.FAILED)
expect(fork2.status).to.equal(ScopeStatus.ACTIVE)
expect(apply.mock.calls).to.have.length(2)
expect(error.mock.calls).to.have.length(1)

root.emit(event)
expect(callback.mock.calls).to.have.length(1)
Expand Down
6 changes: 6 additions & 0 deletions packages/core/tests/update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ describe('Update', () => {
it('update runtime', () => {
const root = new Context()
const dispose = mock.fn(noop)
const warning = mock.fn(noop)
root.on('internal/warning', warning)

const plugin = mock.fn((ctx: Context, config: Config) => {
ctx.on('dispose', dispose)
ctx.on(event, () => {
Expand All @@ -24,16 +27,19 @@ describe('Update', () => {

root.plugin(plugin, { foo: 1 })
expect(dispose.mock.calls).to.have.length(0)
expect(warning.mock.calls).to.have.length(0)
expect(plugin.mock.calls).to.have.length(1)

// update config, should trigger reload
root.emit(event)
expect(dispose.mock.calls).to.have.length(1)
expect(warning.mock.calls).to.have.length(1)
expect(plugin.mock.calls).to.have.length(2)

// update config, should not trigger reload
root.emit(event)
expect(dispose.mock.calls).to.have.length(1)
expect(warning.mock.calls).to.have.length(2)
expect(plugin.mock.calls).to.have.length(2)

expect(plugin.mock.calls[0].arguments[0]).to.equal(plugin.mock.calls[1].arguments[0])
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"module": "esnext",
"moduleResolution": "bundler",
"sourceMap": true,
"declaration": true,
"emitDeclarationOnly": true,
Expand Down

0 comments on commit e0a0eed

Please sign in to comment.