assertron 7.1.3 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{lib → cjs}/assert-order/AssertOrder.d.ts +1 -1
- package/{lib → cjs}/assert-order/AssertOrder.d.ts.map +1 -1
- package/cjs/assert-order/AssertOrder.js +148 -0
- package/cjs/assert-order/InvalidOrder.d.ts +10 -0
- package/cjs/assert-order/InvalidOrder.d.ts.map +1 -0
- package/cjs/assert-order/InvalidOrder.js +44 -0
- package/{lib → cjs}/assert-order/StateMachine.d.ts +0 -0
- package/{lib → cjs}/assert-order/StateMachine.d.ts.map +1 -1
- package/cjs/assert-order/StateMachine.js +111 -0
- package/{lib → cjs}/assert-order/index.d.ts +0 -0
- package/{lib → cjs}/assert-order/index.d.ts.map +0 -0
- package/{lib → cjs}/assert-order/index.js +3 -2
- package/{lib → cjs}/assert-order/interfaces.d.ts +0 -0
- package/{lib → cjs}/assert-order/interfaces.d.ts.map +0 -0
- package/{lib → cjs}/assert-order/interfaces.js +0 -0
- package/{lib → cjs}/assertron/assertron.d.ts +0 -0
- package/cjs/assertron/assertron.d.ts.map +1 -0
- package/{lib → cjs}/assertron/assertron.js +5 -4
- package/{lib → cjs}/assertron/falsy.d.ts +0 -0
- package/{lib → cjs}/assertron/falsy.d.ts.map +0 -0
- package/{lib → cjs}/assertron/falsy.js +4 -3
- package/{lib → cjs}/assertron/index.d.ts +0 -0
- package/{lib → cjs}/assertron/index.d.ts.map +0 -0
- package/{lib → cjs}/assertron/index.js +2 -2
- package/{lib → cjs}/assertron/pathEqual.d.ts +0 -0
- package/{lib → cjs}/assertron/pathEqual.d.ts.map +0 -0
- package/cjs/assertron/pathEqual.js +13 -0
- package/{lib → cjs}/assertron/rejects.d.ts +0 -0
- package/{lib → cjs}/assertron/rejects.d.ts.map +0 -0
- package/cjs/assertron/rejects.js +14 -0
- package/{lib → cjs}/assertron/repeat.d.ts +5 -3
- package/{lib → cjs}/assertron/repeat.d.ts.map +1 -1
- package/cjs/assertron/repeat.js +44 -0
- package/{lib → cjs}/assertron/resolves.d.ts +0 -0
- package/{lib → cjs}/assertron/resolves.d.ts.map +0 -0
- package/cjs/assertron/resolves.js +17 -0
- package/cjs/assertron/satisfies.d.ts +10 -0
- package/cjs/assertron/satisfies.d.ts.map +1 -0
- package/cjs/assertron/satisfies.js +19 -0
- package/{lib → cjs}/assertron/throws.d.ts +0 -0
- package/{lib → cjs}/assertron/throws.d.ts.map +0 -0
- package/cjs/assertron/throws.js +62 -0
- package/{lib → cjs}/assertron/truthy.d.ts +0 -0
- package/{lib → cjs}/assertron/truthy.d.ts.map +0 -0
- package/{lib → cjs}/assertron/truthy.js +4 -3
- package/{lib → cjs}/errors/NotSatisfied.d.ts +0 -0
- package/{lib → cjs}/errors/NotSatisfied.d.ts.map +0 -0
- package/cjs/errors/NotSatisfied.js +28 -0
- package/{lib → cjs}/errors/index.d.ts +0 -0
- package/{lib → cjs}/errors/index.d.ts.map +0 -0
- package/cjs/errors/index.js +11 -0
- package/{lib → cjs}/errors/interfaces.d.ts +0 -0
- package/{lib → cjs}/errors/interfaces.d.ts.map +0 -0
- package/{lib → cjs}/errors/interfaces.js +0 -0
- package/{lib → cjs}/errors/notEqualMessage.d.ts +0 -0
- package/{lib → cjs}/errors/notEqualMessage.d.ts.map +0 -0
- package/{lib → cjs}/errors/notEqualMessage.js +3 -2
- package/{lib → cjs}/errors/notRejectedMessage.d.ts +0 -0
- package/{lib → cjs}/errors/notRejectedMessage.d.ts.map +0 -0
- package/{lib → cjs}/errors/notRejectedMessage.js +3 -2
- package/{lib → cjs}/errors/notThrownMessage.d.ts +0 -0
- package/{lib → cjs}/errors/notThrownMessage.d.ts.map +0 -0
- package/{lib → cjs}/errors/notThrownMessage.js +3 -2
- package/{lib → cjs}/errors/returnNotRejectedMessage.d.ts +0 -0
- package/{lib → cjs}/errors/returnNotRejectedMessage.d.ts.map +0 -0
- package/{lib → cjs}/errors/returnNotRejectedMessage.js +3 -2
- package/{lib → cjs}/errors/unexpectedErrorMessage.d.ts +0 -0
- package/{lib → cjs}/errors/unexpectedErrorMessage.d.ts.map +0 -0
- package/cjs/errors/unexpectedErrorMessage.js +10 -0
- package/{lib → cjs}/errors/util.d.ts +0 -0
- package/{lib → cjs}/errors/util.d.ts.map +0 -0
- package/{lib → cjs}/errors/util.js +2 -1
- package/{lib → cjs}/index.d.ts +0 -0
- package/{lib → cjs}/index.d.ts.map +0 -0
- package/{lib → cjs}/index.js +3 -3
- package/{libm → esm}/assert-order/AssertOrder.js +1 -1
- package/esm/assert-order/InvalidOrder.js +31 -0
- package/{libm → esm}/assert-order/StateMachine.js +3 -2
- package/{libm → esm}/assert-order/index.js +2 -1
- package/{libm → esm}/assert-order/interfaces.js +1 -0
- package/{libm → esm}/assertron/assertron.js +1 -1
- package/{libm → esm}/assertron/falsy.js +0 -0
- package/{libm → esm}/assertron/index.js +0 -0
- package/{libm → esm}/assertron/pathEqual.js +1 -1
- package/{libm → esm}/assertron/rejects.js +1 -1
- package/{libm → esm}/assertron/repeat.js +8 -8
- package/{libm → esm}/assertron/resolves.js +1 -1
- package/{libm → esm}/assertron/satisfies.js +1 -1
- package/{libm → esm}/assertron/throws.js +5 -3
- package/{libm → esm}/assertron/truthy.js +0 -0
- package/{libm → esm}/errors/NotSatisfied.js +0 -0
- package/{libm → esm}/errors/index.js +2 -1
- package/{libm → esm}/errors/interfaces.js +1 -0
- package/{libm → esm}/errors/notEqualMessage.js +0 -0
- package/{libm → esm}/errors/notRejectedMessage.js +0 -0
- package/{libm → esm}/errors/notThrownMessage.js +0 -0
- package/{libm → esm}/errors/returnNotRejectedMessage.js +0 -0
- package/{libm → esm}/errors/unexpectedErrorMessage.js +0 -0
- package/{libm → esm}/errors/util.js +0 -0
- package/{libm → esm}/index.js +0 -0
- package/package.json +47 -32
- package/src/assert-order/AssertOrder.ts +1 -1
- package/src/assert-order/InvalidOrder.ts +4 -4
- package/src/assert-order/StateMachine.ts +2 -1
- package/src/assertron/assertron.spec.ts +3 -3
- package/src/assertron/assertron.ts +9 -9
- package/src/assertron/falsy.spec.ts +2 -2
- package/src/assertron/pathEqual.spec.ts +3 -3
- package/src/assertron/pathEqual.ts +3 -3
- package/src/assertron/rejects.spec.ts +7 -7
- package/src/assertron/rejects.ts +2 -2
- package/src/assertron/repeat.spec.ts +6 -6
- package/src/assertron/repeat.ts +6 -6
- package/src/assertron/resolves.spec.ts +4 -4
- package/src/assertron/resolves.ts +2 -2
- package/src/assertron/satisfies.spec.ts +4 -4
- package/src/assertron/satisfies.ts +15 -5
- package/src/assertron/throws.spec.ts +14 -7
- package/src/assertron/throws.ts +4 -2
- package/src/assertron/truthy.spec.ts +3 -3
- package/src/testUtils.ts +17 -13
- package/lib/assert-order/AssertOrder.js +0 -147
- package/lib/assert-order/InvalidOrder.d.ts +0 -6
- package/lib/assert-order/InvalidOrder.d.ts.map +0 -1
- package/lib/assert-order/InvalidOrder.js +0 -43
- package/lib/assert-order/StateMachine.js +0 -109
- package/lib/assertron/assertron.d.ts.map +0 -1
- package/lib/assertron/pathEqual.js +0 -12
- package/lib/assertron/rejects.js +0 -13
- package/lib/assertron/repeat.js +0 -43
- package/lib/assertron/resolves.js +0 -16
- package/lib/assertron/satisfies.d.ts +0 -6
- package/lib/assertron/satisfies.d.ts.map +0 -1
- package/lib/assertron/satisfies.js +0 -18
- package/lib/assertron/throws.js +0 -59
- package/lib/errors/NotSatisfied.js +0 -27
- package/lib/errors/index.js +0 -10
- package/lib/errors/unexpectedErrorMessage.js +0 -9
- package/libm/assert-order/InvalidOrder.js +0 -31
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assertron",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "A supplementary assertion
|
|
3
|
+
"version": "8.0.0",
|
|
4
|
+
"description": "A supplementary assertion cjsrary",
|
|
5
5
|
"homepage": "https://github.com/unional/assertron",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/unional/assertron/issues"
|
|
@@ -16,31 +16,30 @@
|
|
|
16
16
|
"email": "homawong@gmail.com"
|
|
17
17
|
},
|
|
18
18
|
"sideEffects": false,
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "cjs/index.js",
|
|
21
|
+
"module": "esm/index.js",
|
|
22
|
+
"jsnext:main": "esm/index.js",
|
|
22
23
|
"browser": {
|
|
23
24
|
"perf_hooks": false
|
|
24
25
|
},
|
|
25
|
-
"typings": "
|
|
26
|
+
"typings": "cjs/index.d.ts",
|
|
26
27
|
"files": [
|
|
27
|
-
"
|
|
28
|
-
"
|
|
28
|
+
"cjs",
|
|
29
|
+
"esm",
|
|
29
30
|
"src"
|
|
30
31
|
],
|
|
31
32
|
"scripts": {
|
|
32
|
-
"build": "yarn build:
|
|
33
|
-
"build:
|
|
34
|
-
"build:
|
|
33
|
+
"build": "yarn build:esm && yarn build:cjs",
|
|
34
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
35
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
35
36
|
"build:watch": "tsc --watch",
|
|
36
|
-
"clean": "rimraf .nyc_output .ts coverage
|
|
37
|
-
"codacy": "cat coverage/lcov.info | codacy-coverage",
|
|
37
|
+
"clean": "rimraf .nyc_output .ts coverage cjs esm",
|
|
38
38
|
"coverage": "jest --coverage",
|
|
39
39
|
"depcheck": "yarn dependency-check",
|
|
40
|
-
"dependency-check": "dependency-check . --unused --no-dev -i global-store -i @types/node -i standard-log-color && dependency-check . --missing --no-dev",
|
|
40
|
+
"dependency-check": "dependency-check . --unused --no-dev -i type-plus -i global-store -i @types/node -i standard-log-color && dependency-check . --missing --no-dev",
|
|
41
41
|
"lint": "eslint --ext=js,ts .",
|
|
42
42
|
"nuke": "yarn clean && rimraf node_modules",
|
|
43
|
-
"semantic-release": "semantic-release",
|
|
44
43
|
"test": "jest",
|
|
45
44
|
"test:watch": "jest --watch",
|
|
46
45
|
"verify": "yarn lint && yarn build && yarn dependency-check && yarn coverage",
|
|
@@ -54,26 +53,42 @@
|
|
|
54
53
|
"dependencies": {
|
|
55
54
|
"assertion-error": "^1.1.0",
|
|
56
55
|
"is-node": "^1.0.2",
|
|
57
|
-
"is-promise": "^
|
|
56
|
+
"is-promise": "^4.0.0",
|
|
58
57
|
"path-equal": "^1.1.1",
|
|
59
|
-
"satisfier": "^5.1.
|
|
60
|
-
"tersify": "^3.7.
|
|
61
|
-
"tslib": "^
|
|
58
|
+
"satisfier": "^5.1.1",
|
|
59
|
+
"tersify": "^3.7.9",
|
|
60
|
+
"tslib": "^2.3.1",
|
|
61
|
+
"type-plus": "^3.13.1"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@babel/core": "^7.
|
|
65
|
-
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.
|
|
66
|
-
"@babel/plugin-proposal-optional-chaining": "^7.
|
|
67
|
-
"@babel/preset-env": "^7.
|
|
68
|
-
"@babel/preset-typescript": "^7.
|
|
69
|
-
"@commitlint/cli": "^
|
|
70
|
-
"@commitlint/config-conventional": "^
|
|
64
|
+
"@babel/core": "^7.12.16",
|
|
65
|
+
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.12.13",
|
|
66
|
+
"@babel/plugin-proposal-optional-chaining": "^7.12.16",
|
|
67
|
+
"@babel/preset-env": "^7.12.16",
|
|
68
|
+
"@babel/preset-typescript": "^7.12.16",
|
|
69
|
+
"@commitlint/cli": "^15.0.0",
|
|
70
|
+
"@commitlint/config-conventional": "^15.0.0",
|
|
71
|
+
"@types/jest": "^27.0.3",
|
|
71
72
|
"@types/lodash": "^4.14.149",
|
|
72
|
-
"@
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
73
|
+
"@types/node": "^16.11.11",
|
|
74
|
+
"@typescript-eslint/eslint-plugin": "^5.5.0",
|
|
75
|
+
"@typescript-eslint/parser": "^5.5.0",
|
|
76
|
+
"babel-jest": "^27.4.2",
|
|
77
|
+
"dependency-check": "^4.1.0",
|
|
78
|
+
"eslint": "^8.4.0",
|
|
79
|
+
"eslint-plugin-harmony": "^6.0.0",
|
|
80
|
+
"husky": "^7.0.4",
|
|
81
|
+
"jest": "^27.4.3",
|
|
82
|
+
"jest-junit": "^13.0.0",
|
|
83
|
+
"jest-progress-tracker": "^3.0.1",
|
|
84
|
+
"jest-validate": "^27.4.2",
|
|
85
|
+
"jest-watch-repeat": "^2.0.0",
|
|
86
|
+
"jest-watch-suspend": "^1.1.2",
|
|
87
|
+
"jest-watch-toggle-config": "^2.0.1",
|
|
88
|
+
"jest-watch-typeahead": "^1.0.0",
|
|
89
|
+
"node-supported-releases": "^0.0.0",
|
|
90
|
+
"rimraf": "^3.0.2",
|
|
91
|
+
"typescript": "^4.1.5",
|
|
92
|
+
"update-notifier": "^5.1.0"
|
|
78
93
|
}
|
|
79
94
|
}
|
|
@@ -84,7 +84,7 @@ export class AssertOrder {
|
|
|
84
84
|
this.state.move()
|
|
85
85
|
return steps[index]
|
|
86
86
|
}
|
|
87
|
-
onAny(steps: number[], ...asserts:
|
|
87
|
+
onAny(steps: number[], ...asserts: Array<(...args: any[]) => any>) {
|
|
88
88
|
steps.forEach(step => {
|
|
89
89
|
this.state.on(step, () => {
|
|
90
90
|
let firstError: any
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import AssertionError from 'assertion-error'
|
|
2
|
-
import { State } from './interfaces'
|
|
1
|
+
import AssertionError from 'assertion-error'
|
|
2
|
+
import { State } from './interfaces'
|
|
3
3
|
|
|
4
|
-
export class InvalidOrder extends AssertionError {
|
|
5
|
-
constructor(state: State, method: string, ssf:
|
|
4
|
+
export class InvalidOrder extends AssertionError<{ method: string, state: State, args: any[] }> {
|
|
5
|
+
constructor(state: State, method: string, ssf: (...args: any[]) => any, ...args: any[]) {
|
|
6
6
|
const message = method === 'end' ? getEndMessage(state) : getExpectingMessage(state, method, args)
|
|
7
7
|
super(message, { method, state, args }, ssf)
|
|
8
8
|
}
|
|
@@ -3,7 +3,7 @@ import { State } from './interfaces'
|
|
|
3
3
|
|
|
4
4
|
// istanbul ignore next
|
|
5
5
|
function nodeVersionIsOrAbove(major: number, minor = 0, patch = 0) {
|
|
6
|
-
// without this,
|
|
6
|
+
// without this, systemJS will complain `process is not defined`
|
|
7
7
|
if (!global.process)
|
|
8
8
|
return false
|
|
9
9
|
const versionString = process.version.startsWith('v') ? process.version.slice(1) : process.version
|
|
@@ -14,6 +14,7 @@ function nodeVersionIsOrAbove(major: number, minor = 0, patch = 0) {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
// istanbul ignore next
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
17
18
|
const performance = isNode && nodeVersionIsOrAbove(8, 5) ? require('perf_hooks').performance : undefined
|
|
18
19
|
|
|
19
20
|
let timeTracker: { start(): void, taken(): number }
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import AssertionError from 'assertion-error'
|
|
2
|
-
import a from '..'
|
|
3
|
-
import { assertThrows, noStackTraceFor } from '../testUtils'
|
|
1
|
+
import AssertionError from 'assertion-error'
|
|
2
|
+
import a from '..'
|
|
3
|
+
import { assertThrows, noStackTraceFor } from '../testUtils'
|
|
4
4
|
|
|
5
5
|
describe('false()', () => {
|
|
6
6
|
test('non-false statement throws', () => {
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import AssertionError from 'assertion-error'
|
|
2
|
-
import { falsy } from './falsy'
|
|
3
|
-
import { pathEqual } from './pathEqual'
|
|
4
|
-
import { rejects } from './rejects'
|
|
5
|
-
import { repeat } from './repeat'
|
|
6
|
-
import { resolves } from './resolves'
|
|
7
|
-
import { satisfies } from './satisfies'
|
|
8
|
-
import { throws } from './throws'
|
|
9
|
-
import { truthy } from './truthy'
|
|
1
|
+
import AssertionError from 'assertion-error'
|
|
2
|
+
import { falsy } from './falsy'
|
|
3
|
+
import { pathEqual } from './pathEqual'
|
|
4
|
+
import { rejects } from './rejects'
|
|
5
|
+
import { repeat } from './repeat'
|
|
6
|
+
import { resolves } from './resolves'
|
|
7
|
+
import { satisfies } from './satisfies'
|
|
8
|
+
import { throws } from './throws'
|
|
9
|
+
import { truthy } from './truthy'
|
|
10
10
|
|
|
11
11
|
export const assertron = {
|
|
12
12
|
false(value: any) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import a from '..'
|
|
1
|
+
import a from '..'
|
|
2
2
|
import AssertionError from 'assertion-error'
|
|
3
|
-
import { assertThrows, noStackTraceFor } from '../testUtils'
|
|
3
|
+
import { assertThrows, noStackTraceFor } from '../testUtils'
|
|
4
4
|
|
|
5
5
|
test('non-false statement throws', () => {
|
|
6
6
|
assertThrows(() => a.falsy(true), AssertionError)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import AssertionError from 'assertion-error'
|
|
2
|
-
import assertron from '..'
|
|
3
|
-
import { assertThrows, noStackTraceFor } from '../testUtils'
|
|
1
|
+
import AssertionError from 'assertion-error'
|
|
2
|
+
import assertron from '..'
|
|
3
|
+
import { assertThrows, noStackTraceFor } from '../testUtils'
|
|
4
4
|
|
|
5
5
|
test('path with same style passes', () => {
|
|
6
6
|
assertron.pathEqual('a', 'a')
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import AssertionError from 'assertion-error'
|
|
2
|
-
import { pathEqual as isPathEqual } from 'path-equal'
|
|
3
|
-
import { notEqualMessage } from '../errors'
|
|
1
|
+
import AssertionError from 'assertion-error'
|
|
2
|
+
import { pathEqual as isPathEqual } from 'path-equal'
|
|
3
|
+
import { notEqualMessage } from '../errors'
|
|
4
4
|
|
|
5
5
|
export function pathEqual(actual: string, expected: string) {
|
|
6
6
|
if (!isPathEqual(actual, expected))
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import t from 'assert'
|
|
2
|
-
import AssertionError from 'assertion-error'
|
|
3
|
-
import a from '..'
|
|
4
|
-
import { assertAsyncThrows, noStackTraceFor } from '../testUtils'
|
|
1
|
+
import t from 'assert'
|
|
2
|
+
import AssertionError from 'assertion-error'
|
|
3
|
+
import a from '..'
|
|
4
|
+
import { assertAsyncThrows, noStackTraceFor } from '../testUtils'
|
|
5
5
|
|
|
6
6
|
test('pass on rejected promise', async () => {
|
|
7
7
|
await a.rejects(Promise.reject(1))
|
|
@@ -12,17 +12,17 @@ test('throws on resolved string promise', async () => {
|
|
|
12
12
|
t.strictEqual(err.message, `Expected promise to reject, but it resolves with 'abc'`)
|
|
13
13
|
})
|
|
14
14
|
|
|
15
|
-
test('throws on
|
|
15
|
+
test('throws on resolved boolean promise', async () => {
|
|
16
16
|
const err = await a.throws(a.rejects(Promise.resolve(true)))
|
|
17
17
|
t.strictEqual(err.message, `Expected promise to reject, but it resolves with true`)
|
|
18
18
|
})
|
|
19
19
|
|
|
20
|
-
test('throws on
|
|
20
|
+
test('throws on resolved object promise', async () => {
|
|
21
21
|
const err = await a.throws(a.rejects(Promise.resolve(2)))
|
|
22
22
|
t.strictEqual(err.message, `Expected promise to reject, but it resolves with 2`)
|
|
23
23
|
})
|
|
24
24
|
|
|
25
|
-
test('throws on
|
|
25
|
+
test('throws on resolved object promise', async () => {
|
|
26
26
|
const err = await a.throws(a.rejects(Promise.resolve({ a: 1 })))
|
|
27
27
|
t.strictEqual(err.message, `Expected promise to reject, but it resolves with { a: 1 }`)
|
|
28
28
|
})
|
package/src/assertron/rejects.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import a from '..'
|
|
2
|
-
import {
|
|
2
|
+
import { FailOnOccurrence } from './repeat'
|
|
3
3
|
|
|
4
4
|
test('repeat function n times', () => {
|
|
5
5
|
let count = 0
|
|
@@ -28,22 +28,22 @@ test('error is captured as inner error and indicate which call failed', () => {
|
|
|
28
28
|
const err = a.throws(() => a.repeat(() => {
|
|
29
29
|
++count
|
|
30
30
|
if (count === 5) throw new Error('failed')
|
|
31
|
-
}, 10),
|
|
31
|
+
}, 10), FailOnOccurrence)
|
|
32
32
|
|
|
33
|
-
expect(err.
|
|
33
|
+
expect(err.occurrence).toBe(5)
|
|
34
34
|
expect(err.error.message).toBe('failed')
|
|
35
35
|
})
|
|
36
36
|
|
|
37
37
|
test('rejected error is captured as inner error and indicate which call failed', async () => {
|
|
38
38
|
let count = 0
|
|
39
|
-
const err = await a.throws(a.repeat(() => new Promise((a, r) => {
|
|
39
|
+
const err = await a.throws(a.repeat(() => new Promise<void>((a, r) => {
|
|
40
40
|
setTimeout(() => {
|
|
41
41
|
++count
|
|
42
42
|
if (count === 5) r(new Error('failed'))
|
|
43
43
|
else a()
|
|
44
44
|
}, Math.random() * 10)
|
|
45
|
-
}), 10),
|
|
45
|
+
}), 10), FailOnOccurrence)
|
|
46
46
|
|
|
47
|
-
expect(err.
|
|
47
|
+
expect(err.occurrence).toBe(5)
|
|
48
48
|
expect(err.error.message).toBe('failed')
|
|
49
49
|
})
|
package/src/assertron/repeat.ts
CHANGED
|
@@ -17,7 +17,7 @@ function repeatRecur<R>(fn: () => R | (() => Promise<R>), i: number, times: numb
|
|
|
17
17
|
if (isPromise(result)) {
|
|
18
18
|
return result.then(
|
|
19
19
|
() => repeatRecur(fn, i + 1, times),
|
|
20
|
-
(e) => { throw new
|
|
20
|
+
(e) => { throw new FailOnOccurrence(i, e) }
|
|
21
21
|
) as any
|
|
22
22
|
}
|
|
23
23
|
else {
|
|
@@ -25,13 +25,13 @@ function repeatRecur<R>(fn: () => R | (() => Promise<R>), i: number, times: numb
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
catch (e) {
|
|
28
|
-
if (e instanceof
|
|
29
|
-
throw new
|
|
28
|
+
if (e instanceof FailOnOccurrence) throw e
|
|
29
|
+
throw new FailOnOccurrence(i, e)
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
export class
|
|
34
|
-
constructor(public
|
|
35
|
-
super(`Failed on ${
|
|
33
|
+
export class FailOnOccurrence extends AssertionError<{ occurrence: number }> {
|
|
34
|
+
constructor(public occurrence: number, public error: any) {
|
|
35
|
+
super(`Failed on ${occurrence} occurrence`)
|
|
36
36
|
}
|
|
37
37
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import t from 'assert'
|
|
2
|
-
import a from '..'
|
|
3
|
-
import AssertionError from 'assertion-error'
|
|
4
|
-
import { assertAsyncThrows, noStackTraceFor } from '../testUtils'
|
|
1
|
+
import t from 'assert'
|
|
2
|
+
import a from '..'
|
|
3
|
+
import AssertionError from 'assertion-error'
|
|
4
|
+
import { assertAsyncThrows, noStackTraceFor } from '../testUtils'
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
test('pass on resolved promise', async () => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import AssertionError from 'assertion-error'
|
|
2
|
-
import { every } from 'satisfier'
|
|
3
|
-
import a from '..'
|
|
4
|
-
import { assertThrows, noStackTraceFor } from '../testUtils'
|
|
1
|
+
import AssertionError from 'assertion-error'
|
|
2
|
+
import { every } from 'satisfier'
|
|
3
|
+
import a from '..'
|
|
4
|
+
import { assertThrows, noStackTraceFor } from '../testUtils'
|
|
5
5
|
|
|
6
6
|
test('primitive types will pass', () => {
|
|
7
7
|
a.satisfies(1, 1)
|
|
@@ -1,13 +1,23 @@
|
|
|
1
|
-
import AssertionError from 'assertion-error'
|
|
2
|
-
import { createSatisfier } from 'satisfier'
|
|
3
|
-
import {
|
|
1
|
+
import AssertionError from 'assertion-error'
|
|
2
|
+
import { createSatisfier } from 'satisfier'
|
|
3
|
+
import { And, If, PrimitiveTypes, IsExtend } from 'type-plus'
|
|
4
|
+
import { notSatisfiedMessage } from '../errors'
|
|
5
|
+
|
|
6
|
+
export type SatisfyExpectation<T> = If<
|
|
7
|
+
And<IsExtend<T, PrimitiveTypes>, IsExtend<T, Array<any>>>,
|
|
8
|
+
T | ((v: T) => boolean),
|
|
9
|
+
{
|
|
10
|
+
[P in keyof T]?: T[P] extends string
|
|
11
|
+
? SatisfyExpectation<T[P]> | ((v: T[P]) => boolean) | RegExp
|
|
12
|
+
: SatisfyExpectation<T[P]> | ((v: T[P]) => boolean)
|
|
13
|
+
}>
|
|
4
14
|
|
|
5
15
|
/**
|
|
6
16
|
* Check if `actual` satisfies criteria in `expected`.
|
|
7
17
|
* @param expected All properties can be a value which will be compared to the same property in `actual`, RegExp, or a predicate function that will be used to check against the property.
|
|
8
18
|
*/
|
|
9
|
-
export function satisfies<Actual
|
|
10
|
-
const diff = createSatisfier(expected).exec(actual);
|
|
19
|
+
export function satisfies<Actual>(actual: Actual, expected: SatisfyExpectation<Actual>) {
|
|
20
|
+
const diff = createSatisfier(expected as any).exec(actual);
|
|
11
21
|
if (diff) {
|
|
12
22
|
throw new AssertionError(
|
|
13
23
|
notSatisfiedMessage(diff),
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import t from 'assert'
|
|
2
|
-
import AssertionError from 'assertion-error'
|
|
3
|
-
import a from '..'
|
|
4
|
-
import { assertAsyncThrows, assertIsError, assertIsPromise, assertThrows, noStackTraceFor } from '../testUtils'
|
|
1
|
+
import t from 'assert'
|
|
2
|
+
import AssertionError from 'assertion-error'
|
|
3
|
+
import a from '..'
|
|
4
|
+
import { assertAsyncThrows, assertIsError, assertIsPromise, assertThrows, noStackTraceFor } from '../testUtils'
|
|
5
5
|
|
|
6
6
|
test('when value is PromiseLike returns Promise', async () => {
|
|
7
7
|
let actual = a.throws(Promise.reject(new Error()), Error)
|
|
@@ -94,12 +94,16 @@ test('thrown error does not contain internal stack track if function does not',
|
|
|
94
94
|
})
|
|
95
95
|
|
|
96
96
|
test('throws if function returns resolved promise', async () => {
|
|
97
|
-
const err = await assertAsyncThrows<AssertionError<{ value: string }>>(
|
|
97
|
+
const err = await assertAsyncThrows<AssertionError<{ value: string }>>(
|
|
98
|
+
() => a.throws(() => Promise.resolve('ok')),
|
|
99
|
+
AssertionError)
|
|
98
100
|
t.strictEqual(err.value, 'ok')
|
|
99
101
|
})
|
|
100
102
|
|
|
101
103
|
test('thrown error does not contain internal stack trace if function returns resolved promise', async () => {
|
|
102
|
-
const err = await assertAsyncThrows<AssertionError<{ value: string }>>(
|
|
104
|
+
const err = await assertAsyncThrows<AssertionError<{ value: string }>>(
|
|
105
|
+
() => a.throws(() => Promise.resolve('ok')),
|
|
106
|
+
AssertionError)
|
|
103
107
|
noStackTraceFor('throws.ts', err)
|
|
104
108
|
})
|
|
105
109
|
|
|
@@ -119,7 +123,10 @@ test('throws if function returns rejected promise not passing valdation', async
|
|
|
119
123
|
})
|
|
120
124
|
|
|
121
125
|
test('thrown error does not contain stack trace if function returns rejected promise not passing valdation', async () => {
|
|
122
|
-
const err = await assertAsyncThrows<
|
|
126
|
+
const err = await assertAsyncThrows<
|
|
127
|
+
AssertionError<{ actual: any }>
|
|
128
|
+
>(() => a.throws(() => Promise.reject('ok'), err => err !== 'ok'))
|
|
129
|
+
// console.log('got err', err)
|
|
123
130
|
noStackTraceFor('throws.ts', err)
|
|
124
131
|
})
|
|
125
132
|
|
package/src/assertron/throws.ts
CHANGED
|
@@ -10,7 +10,7 @@ export function throws<T, R = any>(value: (...args: any[]) => R, validator?: Err
|
|
|
10
10
|
export function throws(value: PromiseLike<any> | (() => any | PromiseLike<any>), validator?: ErrorValidator | ErrorConstructor<any>): any {
|
|
11
11
|
if (typeof value !== 'function' && !isPromise(value)) {
|
|
12
12
|
throw new AssertionError(
|
|
13
|
-
'`assertron.throws()` must be called with a
|
|
13
|
+
'`assertron.throws()` must be called with a function or promise.',
|
|
14
14
|
undefined,
|
|
15
15
|
throws
|
|
16
16
|
)
|
|
@@ -81,11 +81,13 @@ function validateError(validator: ErrorValidator | ErrorConstructor<any> | undef
|
|
|
81
81
|
throws
|
|
82
82
|
)
|
|
83
83
|
}
|
|
84
|
-
else if (!validator(error))
|
|
84
|
+
else if (!validator(error)) {
|
|
85
|
+
// console.log('msg:', unexpectedErrorMessage(error, validator))
|
|
85
86
|
throw new AssertionError(
|
|
86
87
|
unexpectedErrorMessage(error, validator),
|
|
87
88
|
{ actual: error, expected: validator },
|
|
88
89
|
throws
|
|
89
90
|
)
|
|
91
|
+
}
|
|
90
92
|
}
|
|
91
93
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import AssertionError from 'assertion-error'
|
|
2
|
-
import a from '..'
|
|
3
|
-
import { assertThrows, noStackTraceFor } from '../testUtils'
|
|
1
|
+
import AssertionError from 'assertion-error'
|
|
2
|
+
import a from '..'
|
|
3
|
+
import { assertThrows, noStackTraceFor } from '../testUtils'
|
|
4
4
|
|
|
5
5
|
test('falsy statement throws', () => {
|
|
6
6
|
assertThrows(() => a.truthy(false), AssertionError)
|
package/src/testUtils.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
// istanbul ignore file
|
|
2
|
-
import t from 'assert'
|
|
3
|
-
import AssertionError from 'assertion-error'
|
|
4
|
-
import isPromise from 'is-promise'
|
|
5
|
-
import { isError } from 'lodash'
|
|
6
|
-
import { ErrorConstructor } from './errors'
|
|
2
|
+
import t from 'assert'
|
|
3
|
+
import AssertionError from 'assertion-error'
|
|
4
|
+
import isPromise from 'is-promise'
|
|
5
|
+
import { isError } from 'lodash'
|
|
6
|
+
import { ErrorConstructor } from './errors'
|
|
7
7
|
|
|
8
|
-
export
|
|
8
|
+
export type AnyFunction = (...args: any[]) => any
|
|
9
|
+
|
|
10
|
+
export function runAsync(fn: AnyFunction) {
|
|
9
11
|
return new Promise((resolve, reject) => {
|
|
10
12
|
setTimeout(() => {
|
|
11
13
|
try {
|
|
@@ -18,7 +20,7 @@ export function runAsync(fn) {
|
|
|
18
20
|
})
|
|
19
21
|
}
|
|
20
22
|
|
|
21
|
-
export function runSequentialAsync(...fns) {
|
|
23
|
+
export function runSequentialAsync(...fns: AnyFunction[]) {
|
|
22
24
|
return new Promise((resolve, reject) => {
|
|
23
25
|
setTimeout(() => {
|
|
24
26
|
try {
|
|
@@ -35,7 +37,7 @@ export function runSequentialAsync(...fns) {
|
|
|
35
37
|
})
|
|
36
38
|
}
|
|
37
39
|
|
|
38
|
-
export function runParallelAsync(...fns) {
|
|
40
|
+
export function runParallelAsync(...fns: AnyFunction[]) {
|
|
39
41
|
return Promise.all(fns.map(fn => runAsync(fn)))
|
|
40
42
|
}
|
|
41
43
|
|
|
@@ -48,11 +50,11 @@ export function assertIsError(err: Error) {
|
|
|
48
50
|
t(isError(err))
|
|
49
51
|
}
|
|
50
52
|
|
|
51
|
-
export function assertThrows<E extends Error>(fn, ErrorType?: ErrorConstructor<E>): E {
|
|
53
|
+
export function assertThrows<E extends Error>(fn: AnyFunction, ErrorType?: ErrorConstructor<E>): E {
|
|
52
54
|
try {
|
|
53
55
|
fn()
|
|
54
56
|
}
|
|
55
|
-
catch (e) {
|
|
57
|
+
catch (e: any) {
|
|
56
58
|
if (ErrorType && !(e instanceof ErrorType)) {
|
|
57
59
|
throw new Error(`thrown error ${e} is not of expected type ${ErrorType}`)
|
|
58
60
|
}
|
|
@@ -61,21 +63,23 @@ export function assertThrows<E extends Error>(fn, ErrorType?: ErrorConstructor<E
|
|
|
61
63
|
throw new Error(`${fn} does not throw as expected`)
|
|
62
64
|
}
|
|
63
65
|
|
|
64
|
-
export async function assertAsyncThrows<E extends Error>(fn, ErrorType?: ErrorConstructor<E>): Promise<E> {
|
|
66
|
+
export async function assertAsyncThrows<E extends Error>(fn: AnyFunction, ErrorType?: ErrorConstructor<E>): Promise<E> {
|
|
65
67
|
try {
|
|
66
68
|
await fn()
|
|
67
69
|
}
|
|
68
|
-
catch (e) {
|
|
70
|
+
catch (e: any) {
|
|
71
|
+
// console.log('catch clause', e)
|
|
69
72
|
if (ErrorType && !(e instanceof ErrorType)) {
|
|
70
73
|
throw new Error(`thrown error ${e} is not of expected type ${ErrorType}`)
|
|
71
74
|
}
|
|
75
|
+
// console.log('returning e')
|
|
72
76
|
return e
|
|
73
77
|
}
|
|
74
78
|
throw new Error(`${fn} does not throw as expected`)
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
|
|
78
|
-
export function noStackTraceFor(filename: string, err: AssertionError) {
|
|
82
|
+
export function noStackTraceFor(filename: string, err: AssertionError<any>) {
|
|
79
83
|
if (err.stack && RegExp(filename).test(err.stack))
|
|
80
84
|
throw new Error(`contains internal stack: \n${err.stack}`)
|
|
81
85
|
}
|