npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@adityahegde/typescript-test-utils

v1.3.2

Published

Various testing utils for writing tests in TypeScript/JavaScript

Downloads

49

Readme

typescript-test-utils

Has decorator based utils for writing tests in TypeScript. Supports mocha and jest tests. Playwright support uses mocha to run test. Playwright is pending on https://github.com/microsoft/playwright/issues/7121

Usage

Install using npm i @adityahegde/typescript-test-utils --save-dev Packages for mocha, jest and Playwright need to be installed separately.

mocha

Install mocha related packages npm i mocha @types/mocha --save-dev

import should from "should";
import {MochaTestLibrary} from "@adityahegde/typescript-test-utils/dist/mocha/MochaTestLibrary";
import {DataProviderData, TestBase} from "@adityahegde/typescript-test-utils";

// TestBase.TestLibrary dictates what underlying library to use. MochaTestLibrary for mocha
@TestBase.Suite
// TestLibrary declaration should come after TestBase.Suite
@TestBase.TestLibrary(MochaTestLibrary)
export class SampleMochaTest extends TestBase {
  @TestBase.BeforeSuite()
  public setupSuite() {
    // code to setup suite
  }

  @TestBase.BeforeEachTest()
  public setupTest() {
    // code to setup test
  }

  // return DataProviderData structure with (string, string) args
  // the args should match the one from test method
  public dataProvider(): DataProviderData<[string, string]> {
    return {
      subData: [{
        title: "level0",
        subData: [{
          title: "level00",
          subData: [{
            title: "level001",
            args: ["arg0010", "arg0011"],
          }, {
            title: "level002",
            args: ["arg0020", "arg0021"],
          }],
        }, {
          args: ["arg010", "arg011"],
        }],
      }, {
        args: ["arg10", "arg11"],
      }],
    };
  }

  @TestBase.Test("dataProvider")
  public someTestUsingDataProvidor(arg0: string, arg1: string) {
    // test using args from data provider
  }

  @TestBase.Test()
  public someTest() {
    // a single test
  }

  @TestBase.AfterEachTest()
  public teardownTest() {
    // code to teardown test
  }

  @MochaTestBase.AfterSuite()
  public teardownSuite() {
    // code to teardown suite
  }
}

jest

Install jest related packages npm i jest ts-jest @types/jest --save-dev We can use the same test described in mocha section for jest by using JestTestBase

import should from "should";
import {JestTestLibrary} from "@adityahegde/typescript-test-utils/dist/jest/JestTestLibrary";
import {DataProviderData, TestBase} from "@adityahegde/typescript-test-utils";

// TestBase.TestLibrary dictates what underlying library to use. MochaTestLibrary for mocha
@TestBase.Suite
// TestLibrary declaration should come after TestBase.Suite
@TestBase.TestLibrary(JestTestLibrary)
export class SampleMochaTest extends TestBase {
  @TestBase.BeforeSuite()
  public setupSuite() {
    // code to setup suite
  }

  @TestBase.BeforeEachTest()
  public setupTest() {
    // code to setup test
  }

  // return DataProviderData structure with (string, string) args
  // the args should match the one from test method
  public dataProvider(): DataProviderData<[string, string]> {
    return {
      subData: [{
        // ...
      }]
    };
  }

  @TestBase.Test("dataProvider")
  public someTestUsingDataProvidor(arg0: string, arg1: string) {
    // test using args from data provider
  }

  @TestBase.Test()
  public someTest() {
    // a single test
  }

  @TestBase.AfterEachTest()
  public teardownTest() {
    // code to teardown test
  }

  @TestBase.AfterSuite()
  public teardownSuite() {
    // code to teardown suite
  }
}

playwright

Using playwright's runner is not supported until custom tsconfig is supported there. Uses mocha currently. Install playwright and mocha related packages npm i mocha playwright @types/mocha --save-dev Pass browserLaunchOptions in TestBase.ParameterizedSuite to override launch options.

import should from "should";
import {MochaTestLibrary} from "@adityahegde/typescript-test-utils/dist/mocha/MochaTestLibrary";
import {PlaywrightSuiteSetup} from "@adityahegde/typescript-test-utils/dist/playwright/PlaywrightSuiteSetup";
import {DataProviderData, TestBase} from "@adityahegde/typescript-test-utils";

// TestBase.TestLibrary dictates what underlying library to use. MochaTestLibrary for mocha
@TestBase.Suite
// TestLibrary declaration should come after TestBase.Suite. Uses mocha here, can be swapped with jest also.
@TestBase.TestLibrary(MochaTestLibrary)
// Adds a playwright implementation of TestSuiteSetup that starts a browser and creates a new page per test.
// Must come after TestBase.Suite
@TestBase.TestSuiteSetup(PlaywrightSuiteSetup)
export class PlaywrightTest extends TestBase {
  @TestBase.BeforeSuite()
  public setupSuite() {
    // code to setup suite
    // optionally start a server for the tests
  }

  @TestBase.BeforeEachTest()
  public setupTest() {
    // code to setup test
  }

  // return DataProviderData structure with (string, string) args
  // the args should match the one from test method
  public dataProvider(): DataProviderData<[string, string]> {
    return {
      subData: [{
        title: "level0",
        subData: [{
          title: "level00",
          subData: [{
            title: "level001",
            args: ["arg0010", "arg0011"],
          }, {
            title: "level002",
            args: ["arg0020", "arg0021"],
          }],
        }, {
          args: ["arg010", "arg011"],
        }],
      }, {
        args: ["arg10", "arg11"],
      }],
    };
  }

  @TestBase.Test("dataProvider")
  public someTestUsingDataProvidor(arg0: string, arg1: string, {page}) {
    // test using args from data provider
    // last arg is the test context with playwright's page object
  }

  @TestBase.Test()
  public someTest({page}) {
    // a single test
    // has one arg, test context with playwright's page object
  }

  @TestBase.AfterEachTest()
  public teardownTest() {
    // code to teardown test
  }

  @MochaTestBase.AfterSuite()
  public teardownSuite() {
    // code to teardown suite
    // optionally stop the server
  }
}

Parameterized Test

It is possible to create a single test class with parameters. This is helpful in integration tests of a composition based component. Real world example: https://github.com/AdityaHegde/typescript-framework/blob/main/test/functional/server/ResourceRestriction.test.ts#L23

import should from "should";
import {MochaTestLibrary} from "@adityahegde/typescript-test-utils/dist/mocha/MochaTestLibrary";
import {DataProviderData, TestBase} from "@adityahegde/typescript-test-utils";

// This class compares suite title and expected title.
// Can be anything here but should have the mandatory suiteTitle
// Use the param through suiteData variable in the class.
@TestBase.ParameterizedSuite([0, 1, 2].map(idx => {
  return {
    suiteTitle: `ParameterizedTest${idx}`,
    expectedTitle: `ParameterizedTest${idx}`,
  }
}))
// TestLibrary declaration should come after TestBase.ParameterizedSuite
@TestBase.TestLibrary(MochaTestLibrary)
export class ParameterizedTestSpec extends TestBase {
  @TestBase.BeforeSuite()
  public setupSuite() {
    // code to setup suite
  }

  @TestBase.BeforeEachTest()
  public setupTest() {
    // code to setup test
  }

  // return DataProviderData structure with (string, string) args
  // the args should match the one from test method
  public dataProvider(): DataProviderData<[string, string]> {
    return {
      subData: [{
        // ...
      }],
    };
  }

  @TestBase.Test("dataProvider")
  public someTestUsingDataProvidor(arg0: string, arg1: string) {
    // test using args from data provider
  }

  @TestBase.Test()
  public someTest() {
    // a single test
    should(this.suiteTitle).be.equal(this.suiteData.expectedTitle);
  }

  @TestBase.AfterEachTest()
  public teardownTest() {
    // code to teardown test
  }

  @TestBase.AfterSuite()
  public teardownSuite() {
    // code to teardown suite
  }
}

TestSuiteSetup

If inheritance based code sharing in tests is not desired, TestSuiteSetup can be used to add composition based isolated setup.

Implement respective methods to setup and teardown the suite and tests. setupTest and teardownTest are passed the testSuiteParameter from parameterised suits and a free form context object. Use this context to store test specific variables. The last param to each test will be this context. Check test/mocha/PlaywrightTest.spec.ts for more details on how the context is used.

import {TestSuiteSetup} from "@adityahegde/typescript-test-utils";

export class MyTestSuiteSetupImpl extends TestSuiteSetup {
  public setupSuite(testSuiteParameter: TestSuiteParameter): Promise<void> {
    // setup the suite using testSuiteParameter
    return Promise.resolve();
  }

  public setupTest(testSuiteParameter: TestSuiteParameter, testContext: Record<any, any>): Promise<void> {
    // setup the test using testSuiteParameter and store and variables needed in tests in testContext
    return Promise.resolve();
  }

  public teardownTest(testSuiteParameter: TestSuiteParameter, testContext: Record<any, any>): Promise<void> {
    // teardown the test using the variables in testContext
    return Promise.resolve();
  }

  public teardownSuite(testSuiteParameter: TestSuiteParameter): Promise<void> {
    // teardown the suite
    return Promise.resolve();
  }
}

Add these TestSuiteSetup using,

@TestBase.TestSuiteSetup(MyTestSuiteSetupImpl)
// Multiple TestSuiteSetup can be added
@TestBase.TestSuiteSetup(SomeOtherTestSuiteSetupImpl)
class MyTest extends TestBase {
  // ...
}