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

@manuth/extended-yo-generator-test

v14.0.2

Published

Provides tools for testing generators created using `@manuth/extended-yo-generator`

Downloads

56

Readme

ExtendedYoGeneratorTest

Provides tools for testing generators created using @manuth/extended-yo-generator

status-badge

Getting Started

To get started with ExtendedYoGeneratorTest, install the package using following command:

npm install --save @manuth/extended-yo-generator-test

Usage

The key feature of this package is the TestContext class which allows you to spawn new generators easily.

Creating a TestContext object

The TestContext-constructor takes one argument generatorDirectory which must equal the directory containing the generator:

import path = require("path");
import { TestContext } from "@manuth/extended-yo-generator-test";
import AppGenerator = require("./generators/app");

let context: TestContext<AppGenerator> = new TestContext(path.join(__dirname, "generators", "app"));

Inspecting the Generator

The Generator property allows you to retrieve a default instance of the generator. This allows you to inspect the default settings and default behavior of your generator.

(
    async () =>
    {
        let generator = await context.Generator;
        console.log(generator.modulePath(".gitignore"));
        console.log(generator.Settings);
    })();

Wrap Values in Promises or Functions

As the Generator works with resolvable values quite often, the TestContext provides functions for wrapping values into functions or promises.

let value = "hello world";

(
    async () =>
    {
        console.log(await context.CreatePromise(value));            // Output: "hello world"
        console.log(context.CreateFunction(value)());               // Output: "hello world"
        console.log(await context.CreatePromiseFunction(value)());  // Output: "hello world"
    })();

Executing the Generator

There's a method which allows you to execute a new instance of the generator.

let runContext = testContext.ExecuteGenerator();
await runContext.toPromise();

Testing Generator-Components

If you want to test generator-components such as file-mappings or questions, you might want to use the TestContext.Default-context for injecting said components.

import { TestContext } from "@manuth/extended-yo-generator-test";

let testContext = TestContext.Default;

testContext.ExecuteGenerator(
    {
        FileMappings: [
            new MyCustomFileMapping()
        ]
    });

Testing File-Mappings

You might want to make assertions about the content of file-mappings or check the integrity of resulting .json or .js-files.

The new classes FileMappingTester, JavaScriptFileMappingTester and JSONFileMappingTester might fit your needs perfectly.

import Assert = require("assert");
import { TestContext, FileMappingTester } from "@manuth/extended-yo-generator-test";

let tester = new FileMappingTester(await TestContext.Default.Generator, new MyCustomFileMapping());

await tester.Run(); // Execute the file-mapping and commit the result to the file-system
await tester.AssertContent("expected content"); // or
Assert.strictEqual(await tester.Content, "expected content");

JSONFileMappingTester

The JSONFileMappingTester parses the content of the destination-file and allows you to make assertions about the metadata.

import Assert = require("assert");
import { TestContext, JSONFileMappingTester } from "@manuth/extended-yo-generator-test";

let tester = new JSONFileMappingTester(await TestContext.Default.Generator, new MyCustomFileMapping());

await tester.Run();
Assert.strictEqual((await tester.Metadata).name, "my-awesome-module");

JavaScriptFileMappingTester

Using the file-mapping tester you can check whether the destination-file exports the expected members. This is especially useful because the file is automatically deleted from the require-cache in order to hot-reload changes made to the destination-file.

import Assert = require("assert");
import { TestContext, JavaScriptFileMappingTester } from "@manuth/extended-yo-generator-test";

let tester = new JavaScriptFileMappingTester(await TestContext.Default.Generator, new MyCustomFileMapping());

await tester.WriteOutput("module.exports = 1;");
console.log(await tester.Require()); // logs `1`
await tester.WriteOutput("module.exports = 2;");
console.log(await tester.Require()); // logs `2` as the content of the destination-file has been hot-reloaded