Skip to main content

Testing

Testing is an essential part of the development process, ensuring that your application functions as expected and that changes do not introduce regressions. In AxiSpark.js, you can use the dependency injection system to facilitate testing by allowing you to easily swap out dependencies with mock implementations during tests.

In order to test your application, you can use popular testing frameworks like Jest or Mocha. These frameworks provide a rich set of features for writing and running tests, including assertions, mocking, and test runners.

Test package​

AxiSpark.js provides a test package (@axisparkjs/test) that includes an AxiSparkTestFactory class, which allows you to create a test context for your application. This test context can be used to initialize your application and its dependencies in a controlled environment, making it easier to write tests for your application components.

axispark-test-factory.ts
/**
* A factory for creating test instances of AxiSparkCore.
*/
class AxiSparkTestFactory {
public create(config?: AxiSparkTestConfig): AxiSparkCore;
}

/**
* Configuration options for creating a test instance of AxiSparkCore.
*/
interface AxiSparkTestConfig {
/**
* An array of providers or class types to be used in the test context. These can be mock implementations or real implementations of your application's dependencies.
*/
providers?: (Provider | ClassType)[];

/**
* An optional instance of AxiSparkCore to be used as the application context.
*/
app?: AxiSparkCore;
}

Usage​

First, install the test package as a development dependency:

npm install --save-dev @axisparkjs/test

Next, you can use the AxiSparkTestFactory to create a test context for your application. You can provide mock implementations of your application's dependencies using the providers option. Here's an example of how to set up a test for a plugin that depends on a logger (This test uses Jest as the testing framework, but you can use any testing framework of your choice):

example.test.ts
import { AxiSparkTestFactory } from '@axisparkjs/test';
import { AxiSparkCore } from '@axisparkjs/core';
import { Logger } from '@axisparkjs/logger';

let axiSparkCore: AxiSparkCore;
let testComponent: TestComponent;
const mockLogger = {
info: jest.fn(),
};

describe('TestComponent', () => {
beforeAll(async () => {
axiSparkCore = AxiSparkTestFactory.create({
providers: [{ token: Logger, useValue: mockLogger }]
});

testComponent = axiSparkCore.get(TestComponent);
});
});

You can also use the app option to provide an existing instance of AxiSparkCore if you want to test your component in the context of a running application or if you want to test your entire application with its dependencies and plugins.

example.test.ts
import { AxiSparkTestFactory } from '@axisparkjs/test';
import { AxiSparkCore } from '@axisparkjs/core';

let axiSparkCore: AxiSparkCore;
const existingApp = new AxiSparkCore();

describe('Test e2e', () => {
beforeAll(async () => {
axiSparkCore = AxiSparkTestFactory.create({
app: existingApp
});
});
});

Test frameworks​

AxiSpark.js does not enforce a specific testing framework, allowing you to choose the one that best fits your needs. Popular choices include Jest, Mocha, and Jasmine. Each of these frameworks provides a rich set of features for writing and running tests, including assertions, mocking, and test runners.

When choosing a testing framework, consider factors such as ease of use, community support, and integration with your development workflow. Regardless of the framework you choose, AxiSpark.js's dependency injection system will help you create testable components and facilitate the testing process.

Here are some popular testing frameworks you can use with AxiSpark.js:

  • Jest: A popular testing framework for JavaScript and TypeScript, providing a rich set of features for writing and running tests, including assertions, mocking, and test runners.
  • Mocha: A flexible testing framework for JavaScript and TypeScript, allowing you to write tests in a variety of styles and providing support for asynchronous testing.
  • Jasmine: A behavior-driven development (BDD) testing framework for JavaScript and TypeScript, providing a clean syntax for writing tests and built-in support for assertions and mocking.