Simple JavaScript testing framework for browsers and node.js
npx @tessl/cli install tessl/npm-jasmine-core@4.6.0Jasmine 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.
npm install jasmine-coreNode.js with global setup:
const jasmine = require('jasmine-core');
jasmine.boot(); // Adds describe, it, expect, etc. to global scopeNode.js without globals:
const { describe, it, expect, beforeEach, afterEach } = require('jasmine-core').noGlobals();Browser (script tags):
<script src="node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script src="node_modules/jasmine-core/lib/jasmine-core/boot0.js"></script>
<script src="node_modules/jasmine-core/lib/jasmine-core/boot1.js"></script>const { describe, it, expect, beforeEach } = require('jasmine-core').noGlobals();
describe('Calculator', () => {
let calculator;
beforeEach(() => {
calculator = {
add: (a, b) => a + b,
multiply: (a, b) => a * b
};
});
it('should add two numbers correctly', () => {
expect(calculator.add(2, 3)).toEqual(5);
});
it('should multiply two numbers correctly', () => {
expect(calculator.multiply(4, 5)).toEqual(20);
});
});Jasmine Core is built around several key components:
describe (suites) and it (specs) for organizing testsexpect() with chainable matchersCore functions for defining test suites, individual tests, and setup/teardown hooks. Provides the fundamental structure for organizing and executing tests.
function describe(description: string, specDefinitions: () => void): Suite;
function it(description: string, testFunction?: () => void | Promise<void>, timeout?: number): Spec;
function beforeEach(action: () => void | Promise<void>, timeout?: number): void;
function afterEach(action: () => void | Promise<void>, timeout?: number): void;
function beforeAll(action: () => void | Promise<void>, timeout?: number): void;
function afterAll(action: () => void | Promise<void>, timeout?: number): void;Assertion system for verifying expected behavior. Includes synchronous and asynchronous expectations with a comprehensive set of built-in matchers.
function expect(actual: any): Expectation;
function expectAsync(actual: Promise<any>): AsyncExpectation;
interface Expectation {
toBe(expected: any): boolean;
toEqual(expected: any): boolean;
toBeCloseTo(expected: number, precision?: number): boolean;
toContain(expected: any): boolean;
toMatch(expected: string | RegExp): boolean;
toThrow(expected?: any): boolean;
// ... many more matchers
}Mock and spy functionality for testing object interactions, method calls, and return values. Includes spy creation, behavior configuration, and call tracking.
function spyOn(obj: object, methodName: string): Spy;
function spyOnProperty(obj: object, propertyName: string, accessType?: 'get' | 'set'): Spy;
interface Spy {
and: SpyStrategy;
calls: CallTracker;
}
interface SpyStrategy {
returnValue(value: any): Spy;
callFake(fn: Function): Spy;
callThrough(): Spy;
throwError(msg?: string): Spy;
}Test execution environment management including configuration options, randomization settings, and timeout controls.
interface jasmine {
getEnv(): Environment;
DEFAULT_TIMEOUT_INTERVAL: number;
}
interface Environment {
configure(options: EnvConfig): void;
execute(): void;
}
interface EnvConfig {
random?: boolean;
seed?: string | number;
stopOnSpecFailure?: boolean;
failSpecWithNoExpectations?: boolean;
}System for extending Jasmine with custom matchers, equality testers, object formatters, and spy strategies.
interface jasmine {
addMatchers(matchers: { [matcherName: string]: MatcherFactory }): void;
addAsyncMatchers(matchers: { [matcherName: string]: AsyncMatcherFactory }): void;
addCustomEqualityTester(tester: EqualityTester): void;
addCustomObjectFormatter(formatter: ObjectFormatter): void;
}Additional testing utilities including asymmetric equality testers, clock mocking, and helper functions for complex testing scenarios.
interface jasmine {
any(constructor: Function): AsymmetricEqualityTester;
anything(): AsymmetricEqualityTester;
objectContaining(sample: object): AsymmetricEqualityTester;
clock(): Clock;
}
interface Clock {
install(): void;
uninstall(): void;
tick(milliseconds: number): void;
mockDate(date?: Date): void;
}interface Suite {
id: number;
description: string;
fullName: string;
}
interface Spec {
id: number;
description: string;
fullName: string;
result: SpecResult;
}
interface SpecResult {
status: 'passed' | 'failed' | 'pending';
description: string;
fullName: string;
failedExpectations: ExpectationResult[];
passedExpectations: ExpectationResult[];
}
interface ExpectationResult {
matcherName: string;
message: string;
stack: string;
passed: boolean;
}