-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test-utils): built-in chai extension (#98)
- Loading branch information
Showing
26 changed files
with
109 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "chai-extended", | ||
"private": true, | ||
"main": "dist/index.js", | ||
"typings": "index.d.ts", | ||
"author": "Shigma <1700011071@pku.edu.cn>", | ||
"license": "MIT", | ||
"files": [ | ||
"index.d.ts", | ||
"dist" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { use } from 'chai' | ||
import { inspect } from 'util' | ||
|
||
use(({ Assertion }) => { | ||
function checkShape(expect, actual, path) { | ||
if (actual === expect || Number.isNaN(expect) && Number.isNaN(actual)) return | ||
|
||
function formatError(expect, actual) { | ||
return `expected to have ${expect} but got ${actual} at path ${path}` | ||
} | ||
|
||
if (!expect || ['string', 'number', 'boolean', 'bigint'].includes(typeof expect)) { | ||
return formatError(inspect(expect), inspect(actual)) | ||
} | ||
|
||
// dates | ||
if (expect instanceof Date) { | ||
if (!(actual instanceof Date) || +expect !== +actual) { | ||
return formatError(inspect(expect), inspect(actual)) | ||
} | ||
return | ||
} | ||
|
||
if (actual === null) { | ||
const type = Object.prototype.toString.call(expect).slice(8, -1).toLowerCase() | ||
return formatError(`a ${type}`, 'null') | ||
} | ||
|
||
// array / object | ||
for (const prop in expect) { | ||
if (typeof actual[prop] === 'undefined' && typeof expect[prop] !== 'undefined') { | ||
return `expected "${prop}" field to be defined at path ${path}` | ||
} | ||
const message = checkShape(expect[prop], actual[prop], `${path}${prop}/`) | ||
if (message) return message | ||
} | ||
} | ||
|
||
Assertion.addMethod('shape', function (expect) { | ||
const message = checkShape(expect, this._obj, '/') | ||
if (message) this.assert(false, message, undefined, expect, this._obj) | ||
}) | ||
}) | ||
|
||
use(require('chai-as-promised')) |
2 changes: 1 addition & 1 deletion
2
packages/chai-extended/tsconfig.json → ...ages/koishi-test-utils/chai/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,7 @@ | ||
import * as utils from './koishi' | ||
import * as memory from './memory' | ||
import { mockModule, actualModule } from './module' | ||
import memory from './memory' | ||
|
||
export { utils, memory } | ||
export { memory } | ||
|
||
export * from './app' | ||
export * from './database' | ||
export * from './memory' | ||
|
||
mockModule('koishi-utils', () => { | ||
const utils1 = actualModule('koishi-utils') | ||
const utils2 = actualModule('./koishi') | ||
return { ...utils1, ...utils2 } | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'koishi-test-utils' | ||
import { expect } from 'chai' | ||
import { Random } from 'koishi-utils' | ||
|
||
describe('Chai Extensions', () => { | ||
it('shape', () => { | ||
expect(1).to.have.shape(1) | ||
expect(NaN).to.have.shape(NaN) | ||
expect(true).to.have.shape(true) | ||
expect('foo').to.have.shape('foo') | ||
|
||
const random = Random.int(1 << 20) | ||
expect(new Date(random)).to.have.shape(new Date(random)) | ||
expect({ a: 1, b: 2 }).to.have.shape({ a: 1 }) | ||
expect([1, 2]).to.have.shape([1]) | ||
|
||
expect(() => expect(1).to.have.shape(2)) | ||
.to.throw('expected to have 2 but got 1 at path /') | ||
expect(() => expect(null).to.have.shape(/(?:)/)) | ||
.to.throw('expected to have a regexp but got null at path /') | ||
expect(() => expect(new Date(0)).to.have.shape(new Date(1000))) | ||
.to.throw('expected to have 1970-01-01T00:00:01.000Z but got 1970-01-01T00:00:00.000Z at path /') | ||
expect(() => expect({ a: 1 }).to.have.shape({ a: 1, b: 2 })) | ||
.to.throw('expected "b" field to be defined at path /') | ||
expect(() => expect([1]).to.have.shape([1, 2])) | ||
.to.throw('expected "1" field to be defined at path /') | ||
expect(() => expect([[1, 2], { a: 3, b: 4 }]).to.have.shape([[1], { a: 4 }])) | ||
.to.throw('expected to have 4 but got 3 at path /1/a/') | ||
}) | ||
}) |
Oops, something went wrong.