Socket
Socket
Sign inDemoInstall

karma-jasmine

Package Overview
Dependencies
138
Maintainers
5
Versions
44
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    karma-jasmine

A Karma plugin - adapter for Jasmine testing framework.


Version published
Weekly downloads
2.1M
increased by5.29%
Maintainers
5
Install size
391 kB
Created
Weekly downloads
 

Package description

What is karma-jasmine?

The karma-jasmine npm package is a plugin for the Karma test runner that allows you to run and write tests using the Jasmine framework. Jasmine is a behavior-driven development framework for testing JavaScript code. Karma serves as a test environment that can run tests in multiple real browsers, which is useful for integration and unit testing.

What are karma-jasmine's main functionalities?

Testing Framework Integration

Integrates Jasmine with Karma to run tests written in Jasmine style within a browser environment.

module.exports = function(config) {
  config.set({
    frameworks: ['jasmine'],
    files: [
      'src/**/*.js',
      'test/**/*.spec.js'
    ],
    browsers: ['Chrome'],
    singleRun: true
  });
};

Describe and It Blocks

Allows writing test suites and specifications using Jasmine's global functions 'describe' and 'it'.

describe('A suite', function() {
  it('contains a spec with an expectation', function() {
    expect(true).toBe(true);
  });
});

Matchers

Provides a rich set of matchers (assertions) that can be used to test expected outcomes.

describe('Matchers', function() {
  it('allows for using Jasmine matchers', function() {
    expect(1 + 2).toEqual(3);
    expect(true).toBe(true);
    expect([1, 2, 3]).toContain(2);
  });
});

Spies

Supports Jasmine spies to track calls to functions and their arguments.

describe('A spy', function() {
  var foo, bar = null;

  beforeEach(function() {
    foo = {
      setBar: function(value) {
        bar = value;
      }
    };

    spyOn(foo, 'setBar');

    foo.setBar(123);
    foo.setBar(456, 'another param');
  });

  it('tracks that the spy was called', function() {
    expect(foo.setBar).toHaveBeenCalled();
  });
});

Asynchronous Support

Handles asynchronous operations in tests with Jasmine's done function.

describe('Async code', function() {
  var value;
  beforeEach(function(done) {
    setTimeout(function() {
      value = 0;
      done();
    }, 1);
  });

  it('should support async execution of test preparation and expectations', function(done) {
    value++;
    expect(value).toBeGreaterThan(0);
    done();
  });
});

Other packages similar to karma-jasmine

Readme

Source

karma-jasmine

npm version npm downloads Release Workflow Status js-standard-style semantic-release

Adapter for the Jasmine testing framework.

Installation

npm install karma-jasmine --save-dev

Configuration

// karma.conf.js
module.exports = function(config) {
  config.set({
    frameworks: ['jasmine'],
    files: [
      '*.js'
    ]
  })
}

If you want to run only some tests whose name match a given pattern you can do this in the following way

$ karma start &
$ karma run -- --grep=<pattern>

where pattern is either a string (e.g --grep=#slow runs tests containing "#slow") or a Regex (e.g --grep=/^(?!.*#slow).*$/ runs tests not containing "#slow").

You can also pass it to karma.config.js:

module.exports = function(config) {
  config.set({
    // ...
    client: {
      args: ['--grep', '<pattern>'],
      // ...
    }
  })
}

If you want to pass configuration options directly to jasmine you can do this in the following way

module.exports = function(config) {
  config.set({
    client: {
      jasmine: {
        random: true,
        seed: '4321',
        oneFailurePerSpec: true,
        failFast: true,
        timeoutInterval: 1000
      }
    }
  })
}

Debug by URL

Failing tests print a debug URL with ?spec=. Use it with --no_single_run and paste it into your browser to focus on a single failing test.

Sharding

By setting config.client.shardIndex and config.client.totalShards, you can run a subset of the full set of specs. Complete sharding support needs to be done in the process that calls karma, and would need to support test result integration across shards.

Custom spec filter

Providing a custom spec filter is also supported.

Example:

// Users are able to set a custom specFilter themselves

jasmine.getEnv().configure({
  specFilter: function (spec) {
    return spec.getFullName() === 'spec that succeeds'
  }
})

describe('spec', () => {
  it('that fails', () => {
    fail('This spec should not run!')
  })

  it('that succeeds', () => {
    expect(1).toBe(1)
  })
})

For more information on Karma see the homepage.

Keywords

FAQs

Last updated on 16 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc