Jasmine Core Testing Framework
jasmine-core is the foundational, standalone JavaScript testing framework that underpins the broader Jasmine ecosystem. It provides the core APIs for writing specs (tests), matchers, and the test runner logic itself. While `jasmine-core` can be used directly for programmatic testing in both browser and Node.js environments, it's more commonly consumed as a dependency by higher-level packages like `jasmine` (for CLI execution) or `jasmine-browser-runner` for a more integrated experience. The current stable version is 6.2.0, with a major version 7.0.0-pre.0 already in pre-release. Jasmine maintains a regular release cadence, typically releasing minor and patch versions every 1-2 months, and major versions annually or bi-annually. Its key differentiators include its behavior-driven development (BDD) syntax, built-in assertion library, and lack of external dependencies for core functionality, making it a simple, self-contained solution for unit and integration testing.
Common errors
-
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'jasmine-core' imported from ...
cause Attempting to `require('jasmine-core')` in an ESM context or `import` in CJS, or incorrect module resolution.fixEnsure your `package.json` `type` field is correctly set (e.g., `"type": "module"` for ESM files, or remove it for CJS). Use `import` statements for ESM and `require` for CJS. Verify the package is installed. -
TypeError: jasmine.getEnv is not a function
cause Attempting to access `jasmine.getEnv()` directly after upgrading to Jasmine v6.x, where it's deprecated.fixInstead of `jasmine.getEnv()`, configure Jasmine via the `Jasmine` class instance (e.g., `new Jasmine().configure(...)`) or a configuration file. If migrating, consider using `jasmine-browser-runner` or the `jasmine` CLI package which abstract this. -
TypeError: expectAsync is not a function
cause Attempting to use `expectAsync()` globally after upgrading to Jasmine v6.x without explicitly enabling it.fixEnsure your Jasmine configuration explicitly enables `expectAsync` if you need it as a global, or refactor to use `await expect()` for async matchers directly.
Warnings
- breaking Jasmine v6.0.0 dropped support for Node.js 14. Projects must now target Node.js 16 or newer. Node.js 18+ is required for the upcoming v7.0.0.
- breaking With Jasmine v6.0.0, the `default` export for `jasmine-core` was removed for ESM. You must now use named exports for ESM imports.
- deprecated `jasmine.DEFAULT_TIMEOUT_INTERVAL` and `jasmine.getEnv()` are deprecated in v6.0.0. While they still work, direct access is discouraged.
- breaking Global `expectAsync` is no longer available by default in Jasmine v6.0.0. It must be explicitly enabled if required.
- breaking `jasmine.Spy` constructor was removed in v6.0.0. Spies should be created using `spyOn`, `createSpy`, or `createSpyObj`.
- gotcha The `jasmine.anything()` matcher now returns `true` for `null` and `undefined` in v6.0.0, which was not the case in previous versions.
Install
-
npm install jasmine-core -
yarn add jasmine-core -
pnpm add jasmine-core
Imports
- jasmine
const jasmine = require('jasmine-core');import { jasmine } from 'jasmine-core'; - bootJasmine
import bootJasmine from 'jasmine-core/lib/boot.js';
import { bootJasmine } from 'jasmine-core'; - getJasmineRequireObj
import { getJasmineRequireObj } from 'jasmine-core';const { getJasmineRequireObj } = require('jasmine-core');
Quickstart
import Jasmine from 'jasmine';
const jasmine = new Jasmine();
// Configure Jasmine to use a custom reporter or default options
jasmine.loadConfig({
spec_dir: 'spec',
spec_files: [
'**/*[sS]pec.js'
],
helpers: [
'helpers/**/*.js'
],
random: false,
seed: null,
stopSpecOnExpectationFailure: false,
failFast: false
});
// Add a simple spec file example (usually in a 'spec' directory)
// This would typically be in a separate file, e.g., 'spec/my-spec.js'
// describe('My Feature', () => {
// it('should do something', () => {
// expect(true).toBe(true);
// });
// });
// For programmatic use, you can add a simple spec directly if not loading from files
// For a more complete programmatic example, you'd typically load files.
// Here's a placeholder for loading specs:
// jasmine.addSpecFile('spec/my-spec.js');
jasmine.onComplete(function(passed) {
if(passed) {
console.log('All specs have passed');
} else {
console.error('At least one spec has failed');
}
});
jasmine.execute();