0
# Jasmine Core
1
2
Jasmine Core is a behavior-driven development (BDD) testing framework for JavaScript that runs anywhere JavaScript can run. It provides an intuitive describe/it syntax for organizing test suites, powerful assertion matchers, flexible spy functionality for mocking, and asynchronous testing support. The framework operates independently of browsers, DOM, or any specific JavaScript framework.
3
4
## Package Information
5
6
- **Package Name**: jasmine-core
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install jasmine-core`
10
11
## Core Imports
12
13
**Node.js with global setup:**
14
```javascript
15
const jasmine = require('jasmine-core');
16
jasmine.boot(); // Adds describe, it, expect, etc. to global scope
17
```
18
19
**Node.js without globals:**
20
```javascript
21
const { describe, it, expect, beforeEach, afterEach } = require('jasmine-core').noGlobals();
22
```
23
24
**Browser (script tags):**
25
```html
26
<script src="node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
27
<script src="node_modules/jasmine-core/lib/jasmine-core/boot0.js"></script>
28
<script src="node_modules/jasmine-core/lib/jasmine-core/boot1.js"></script>
29
```
30
31
## Basic Usage
32
33
```javascript
34
const { describe, it, expect, beforeEach } = require('jasmine-core').noGlobals();
35
36
describe('Calculator', () => {
37
let calculator;
38
39
beforeEach(() => {
40
calculator = {
41
add: (a, b) => a + b,
42
multiply: (a, b) => a * b
43
};
44
});
45
46
it('should add two numbers correctly', () => {
47
expect(calculator.add(2, 3)).toEqual(5);
48
});
49
50
it('should multiply two numbers correctly', () => {
51
expect(calculator.multiply(4, 5)).toEqual(20);
52
});
53
});
54
```
55
56
## Architecture
57
58
Jasmine Core is built around several key components:
59
60
- **Test Organization**: Hierarchical structure using `describe` (suites) and `it` (specs) for organizing tests
61
- **Expectation System**: Fluent assertion API using `expect()` with chainable matchers
62
- **Spy System**: Comprehensive mocking and spying functionality for testing object interactions
63
- **Asynchronous Testing**: Built-in support for promises, callbacks, and async/await patterns
64
- **Environment Management**: Configurable test execution environment with randomization and timeout controls
65
- **Reporter Interface**: Pluggable reporting system for various output formats
66
67
## Capabilities
68
69
### Test Organization and Structure
70
71
Core functions for defining test suites, individual tests, and setup/teardown hooks. Provides the fundamental structure for organizing and executing tests.
72
73
```javascript { .api }
74
function describe(description: string, specDefinitions: () => void): Suite;
75
function it(description: string, testFunction?: () => void | Promise<void>, timeout?: number): Spec;
76
function beforeEach(action: () => void | Promise<void>, timeout?: number): void;
77
function afterEach(action: () => void | Promise<void>, timeout?: number): void;
78
function beforeAll(action: () => void | Promise<void>, timeout?: number): void;
79
function afterAll(action: () => void | Promise<void>, timeout?: number): void;
80
```
81
82
[Test Organization](./test-organization.md)
83
84
### Expectations and Matchers
85
86
Assertion system for verifying expected behavior. Includes synchronous and asynchronous expectations with a comprehensive set of built-in matchers.
87
88
```javascript { .api }
89
function expect(actual: any): Expectation;
90
function expectAsync(actual: Promise<any>): AsyncExpectation;
91
92
interface Expectation {
93
toBe(expected: any): boolean;
94
toEqual(expected: any): boolean;
95
toBeCloseTo(expected: number, precision?: number): boolean;
96
toContain(expected: any): boolean;
97
toMatch(expected: string | RegExp): boolean;
98
toThrow(expected?: any): boolean;
99
// ... many more matchers
100
}
101
```
102
103
[Expectations and Matchers](./expectations-matchers.md)
104
105
### Spy System
106
107
Mock and spy functionality for testing object interactions, method calls, and return values. Includes spy creation, behavior configuration, and call tracking.
108
109
```javascript { .api }
110
function spyOn(obj: object, methodName: string): Spy;
111
function spyOnProperty(obj: object, propertyName: string, accessType?: 'get' | 'set'): Spy;
112
113
interface Spy {
114
and: SpyStrategy;
115
calls: CallTracker;
116
}
117
118
interface SpyStrategy {
119
returnValue(value: any): Spy;
120
callFake(fn: Function): Spy;
121
callThrough(): Spy;
122
throwError(msg?: string): Spy;
123
}
124
```
125
126
[Spy System](./spy-system.md)
127
128
### Environment and Configuration
129
130
Test execution environment management including configuration options, randomization settings, and timeout controls.
131
132
```javascript { .api }
133
interface jasmine {
134
getEnv(): Environment;
135
DEFAULT_TIMEOUT_INTERVAL: number;
136
}
137
138
interface Environment {
139
configure(options: EnvConfig): void;
140
execute(): void;
141
}
142
143
interface EnvConfig {
144
random?: boolean;
145
seed?: string | number;
146
stopOnSpecFailure?: boolean;
147
failSpecWithNoExpectations?: boolean;
148
}
149
```
150
151
[Environment and Configuration](./environment-config.md)
152
153
### Custom Extensions
154
155
System for extending Jasmine with custom matchers, equality testers, object formatters, and spy strategies.
156
157
```javascript { .api }
158
interface jasmine {
159
addMatchers(matchers: { [matcherName: string]: MatcherFactory }): void;
160
addAsyncMatchers(matchers: { [matcherName: string]: AsyncMatcherFactory }): void;
161
addCustomEqualityTester(tester: EqualityTester): void;
162
addCustomObjectFormatter(formatter: ObjectFormatter): void;
163
}
164
```
165
166
[Custom Extensions](./custom-extensions.md)
167
168
### Test Utilities
169
170
Additional testing utilities including asymmetric equality testers, clock mocking, and helper functions for complex testing scenarios.
171
172
```javascript { .api }
173
interface jasmine {
174
any(constructor: Function): AsymmetricEqualityTester;
175
anything(): AsymmetricEqualityTester;
176
objectContaining(sample: object): AsymmetricEqualityTester;
177
clock(): Clock;
178
}
179
180
interface Clock {
181
install(): void;
182
uninstall(): void;
183
tick(milliseconds: number): void;
184
mockDate(date?: Date): void;
185
}
186
```
187
188
[Test Utilities](./test-utilities.md)
189
190
## Types
191
192
```javascript { .api }
193
interface Suite {
194
id: number;
195
description: string;
196
fullName: string;
197
}
198
199
interface Spec {
200
id: number;
201
description: string;
202
fullName: string;
203
result: SpecResult;
204
}
205
206
interface SpecResult {
207
status: 'passed' | 'failed' | 'pending';
208
description: string;
209
fullName: string;
210
failedExpectations: ExpectationResult[];
211
passedExpectations: ExpectationResult[];
212
}
213
214
interface ExpectationResult {
215
matcherName: string;
216
message: string;
217
stack: string;
218
passed: boolean;
219
}
220
```