Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
**Features:**
- Filter tests by tags (HTML reporter). Addressed in [#66](https://github.com/scripterio-js/scripterio/issues/66).
- Assertion `toBeTypeOf()` — Check that a variable has a correct type. Addressed in [#63](https://github.com/scripterio-js/scripterio/issues/63).

***Contributors:***
- [Vadym Nastoiashchyi](https://github.com/VadimNastoyashchy)

## 1.11.0 - 2025-07-08
### Added
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ Use `expect(actual_value)` with assertions:
| `.toBeGreaterThan()` | Check actual value to be greater than expected value |
| `.toBeLessThan()` | Check actual value to be less than expected value |
| `.toContain()` | Use when you want to check that an item is in an array or a string. |
| `.toMatch()` | Use .toMatch() to check that a string matches a regular expression. |
| `.toMatch()` | Use to check that a string matches a regular expression. |
| `.toBeTypeOf()` | Use to check that a variable has a correct type |

---

Expand Down
13 changes: 13 additions & 0 deletions __tests__/assertions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,17 @@ describe('Unit tests for assertions', () => {
expect('test').toMatch('test')
expect('test').toMatch(/test/i)
})

test('Check assertion toBeType()', () => {
expect('Hello').toBeTypeOf('string')
expect(['ScripterI/O', 123]).toBeTypeOf('array')
expect(42).toBeTypeOf('number')
expect(true).toBeTypeOf('boolean')
expect({ key: 'value' }).toBeTypeOf('object')
expect(undefined).toBeTypeOf('undefined')
expect(null).toBeTypeOf('null')
expect(Symbol('sym')).toBeTypeOf('symbol')
expect(10n).toBeTypeOf('bigint')
expect(function () {}).toBeTypeOf('function')
})
})
Binary file modified assets/reporter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ Use `expect(actual_value)` with assertions:
| `.toBeGreaterThan()` | Check actual value to be greater than expected value |
| `.toBeLessThan()` | Check actual value to be less than expected value |
| `.toContain()` | Use when you want to check that an item is in an array or a string. |
| `.toMatch()` | Use .toMatch() to check that a string matches a regular expression. |
| `.toMatch()` | Use to check that a string matches a regular expression. |
| `.toBeTypeOf()` | Use to check that a variable has a correct type |

---

Expand Down
1 change: 1 addition & 0 deletions src/assertions/assertions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export { toBeGreaterThan } from './toBeGreaterThan.mjs'
export { toBeLessThan } from './toBeLessThan.mjs'
export { toContain } from './toContain.mjs'
export { toMatch } from './toMatch.mjs'
export { toBeTypeOf } from './toBeTypeOf.mjs'
173 changes: 173 additions & 0 deletions src/assertions/toBeTypeOf.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { AssertionError } from '../errors/assertion.mjs'
import { RunnerError } from '../errors/runner.mjs'
import { indentLine } from '../utils/transform.mjs'
import { TYPES } from '../core/constants.mjs'
import { EOL } from 'os'

export const toBeTypeOf = (actual, expected) => {
if (typeof expected !== 'string') {
throw new RunnerError(
indentLine(`Provided type: ${typeof expected} is not a string`)
)
}

if (!(expected in TYPES)) {
throw new RunnerError(
indentLine(`Expect type: ${expected} is not a valid type`)
)
}

if (expected === 'array') {
if (!Array.isArray(actual)) {
throw new AssertionError(
indentLine('Expected: <expected>') +
' has an array type' +
EOL +
indentLine('Received: <actual>') +
` has a ${Array.isArray(actual) ? 'array' : typeof actual} type`,
{
actual,
expected,
}
)
}
} else if (expected === 'null') {
if (actual !== null) {
throw new AssertionError(
indentLine('Expected: <expected>') +
' has a null type' +
EOL +
indentLine('Received: <actual>') +
` has a ${
actual === undefined
? 'undefined'
: actual === null
? 'null'
: Array.isArray(actual)
? 'array'
: typeof actual
} type`,
{
actual,
expected,
}
)
}
} else if (expected === 'string') {
if (typeof actual !== 'string') {
throw new AssertionError(
indentLine('Expected: <expected>') +
' has a string type' +
EOL +
indentLine('Received: <actual>') +
` has a ${typeof actual} type`,
{
actual,
expected,
}
)
}
} else if (expected === 'number') {
if (typeof actual !== 'number') {
throw new AssertionError(
indentLine('Expected: <expected>') +
' has a number type' +
EOL +
indentLine('Received: <actual>') +
` has a ${typeof actual} type`,
{
actual,
expected,
}
)
}
} else if (expected === 'boolean') {
if (typeof actual !== 'boolean') {
throw new AssertionError(
indentLine('Expected: <expected>') +
' has a boolean type' +
EOL +
indentLine('Received: <actual>') +
` has a ${typeof actual} type`,
{
actual,
expected,
}
)
}
} else if (expected === 'object') {
if (
typeof actual !== 'object' ||
actual === null ||
Array.isArray(actual)
) {
throw new AssertionError(
indentLine('Expected: <expected>') +
' has an object type' +
EOL +
indentLine('Received: <actual>') +
` has a ${typeof actual} type`,
{
actual,
expected,
}
)
}
} else if (expected === 'function') {
if (typeof actual !== 'function') {
throw new AssertionError(
indentLine('Expected: <expected>') +
' has a function type' +
EOL +
indentLine('Received: <actual>') +
` has a ${typeof actual} type`,
{
actual,
expected,
}
)
}
} else if (expected === 'undefined') {
if (typeof actual !== 'undefined') {
throw new AssertionError(
indentLine('Expected: <expected>') +
' has an undefined type' +
EOL +
indentLine('Received: <actual>') +
` has a ${typeof actual} type`,
{
actual,
expected,
}
)
}
} else if (expected === 'symbol') {
if (typeof actual !== 'symbol') {
throw new AssertionError(
indentLine('Expected: <expected>') +
' has a symbol type' +
EOL +
indentLine('Received: <actual>') +
` has a ${typeof actual} type`,
{
actual,
expected,
}
)
}
} else if (expected === 'bigint') {
if (typeof actual !== 'bigint') {
throw new AssertionError(
indentLine('Expected: <expected>') +
' has a bigint type' +
EOL +
indentLine('Received: <actual>') +
` has a ${typeof actual} type`,
{
actual,
expected,
}
)
}
}
}
13 changes: 13 additions & 0 deletions src/core/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,16 @@ export const REPORTERS = {
CONSOLE: 'console',
HTML: 'html',
}

export const TYPES = {
bigint: 'bigint',
boolean: 'boolean',
function: 'function',
number: 'number',
object: 'object',
string: 'string',
symbol: 'symbol',
array: 'array',
null: 'null',
undefined: 'undefined',
}
20 changes: 20 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,26 @@ type Assertions = {
* ```
*/
toMatch: (expected: any) => void

/**
* Use .toBeTypeOf() to check that a variable has a correct type
*
* **Usage**
*
* ```js
* expect('Hello').toBeTypeOf('string')
* expect(['ScripterI/O', 123]).toBeTypeOf('array')
* expect(42).toBeTypeOf('number')
* expect(true).toBeTypeOf('boolean')
* expect({ key: 'value' }).toBeTypeOf('object')
* expect(undefined).toBeTypeOf('undefined')
* expect(null).toBeTypeOf('null')
* expect(Symbol('sym')).toBeTypeOf('symbol')
* expect(10n).toBeTypeOf('bigint')
* expect(function () {}).toBeTypeOf('function')
* ```
*/
toBeTypeOf: (expected: any) => void
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export const afterAll = (body) => core.afterAll(body)
* @property {Function} toBeLessThan - Compare two numbers (received < expected).
* @property {Function} toContain - Check that an item is in an array or a string contains a substring.
* @property {Function} toMatch - Check that a string matches a regular expression.
* @property {Function} toBeTypeOf - Check that a variable has a correct type
*/

/**
Expand Down
Loading