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

gulpplugintesthelpers

v2.0.3

Published

Helper functions for testing a gulp plugin

Downloads

5

Readme

Exported functions to make testing a gulp plugin easy

The main function which all the others call in to

Creates a stream that writes the file argument and pipes through to the transform being tested. Upon end or error will ensure that null files are passed through and for buffer/stream files will ensure they transformed to the same type ( Buffer=>Buffer, Stream=>Stream). If these conditions are not met the promise rejects with GulpRuleError with the type property one of DidNotPassThroughNull,TransformedBufferToStreamOrNull,TransformedStreamToBufferOrNull. If the conditions are met the expectation callback is called. If the expectation throws the Promise is rejected with a PluginTestError.

export declare type GulpStream = NodeJS.ReadWriteStream;

export function pluginTest(
    gulpStream:GulpStream,
    file:File,
    expectation:(files:File[],error?:Error)=>void|Promise<void>
):Promise<void>

Examples for all functions at the end.

Transform to single file test helper

These tests will throw if there is not a single transformed file. The reason that there is a buffer and stream version is two fold. Firstly, you can pass a string and the appropriate file will be created and if you provide a file it checks that you did indeed provide the appropriate file. If you do not pass the appropriate file the function will reject.

These tests will call the expectation if there is an error, or if there is no error will provide the transformed file contents to the expectation.

type SingleTransformationExpectation = (
    transformedContents:string|undefined,
    originalFile:File,
    transformedFile:File|undefined,
    err?:Error)=>void

export function singleBufferTransformContentsTest(
    gulpStream:GulpStream,
    fileContentsOrBufferFile:string|File,
    expectation:SingleTransformationExpectation
):Promise<void>

export function singleStreamTransformContentsTest(
    gulpStream:GulpStream,
    fileContentsOrBufferFile:string|File,
    expectation:SingleTransformationExpectation
):Promise<void>

Ignores / Filters

The ignoreFileTest function will resolve when the transform calls the callback without error and without changing the file. The filtersFileTest function will resolve when the transform calls the callback without error or file argument.

export function ignoresFileTest(gulpStream:GulpStream,file:File):Promise<void>
export function filtersFileTest(gulpStream:GulpStream,file:File):Promise<void>

Transforms with files

This function resolves when resolves and there are transformed files

export function transformsWithFilesTest(gulpStream:GulpStream,file:File):Promise<void>

File contents type

This will pass the plugin a file of the unsupported type, it will resolve if the plugin errors with an Error object.

export function throwsErrorOnUnsupportedContentTypeTest(gulpStream:GulpStream,streamNotSupported=true):Promise<void>

There is a stronger version of throwsErrorOnUnsupportedContentTypeTest. To resolve your plugin needs to throw a PluginError with a BufferNotSupported property with value equal to the streamNotSupported argument. The module 'th-gulpHelpers' exports the function cbErrorIfContentsTypeNotSupported that can be used for this functionality.

export function throwsPluginErrorOnUnsupportedContentTypeTest(gulpStream:GulpStream,streamNotSupported=true):Promise<void>

Example usage

Using jest.

describe("My Hello World Gulp Plugin",()=>{
    class GulpHelloWorld extends Transform {
        constructor(){
            super({objectMode:true})
        }
        async _transform(file:File,encoding:string,cb:TransformCallback){
            const threw=cbErrorIfContentsTypeNotSupported("GulpHelloWorld",file,cb,false,true);
            if(threw) return;
            
            const fileContents=await getFileContents(file);
            switch(fileContents){
                case "hello":
                    file.contents=Buffer.from("hello world");
                    this.push(file);
                    break;
                case "hello hello":
                    file.contents=Buffer.from("hello world");
                    this.push(file);
                    const clone=file.clone();
                    clone.contents=Buffer.from("hello world");
                    this.push(clone);
                    break;
                case "goodbye":
                    break;
                default:
                    this.push(file);
                    break; 
            }
            cb();
            
        }
    }
    
    it("should accept buffers only",()=>{
        return PluginHelpers.throwsPluginErrorOnUnsupportedContentTypeTest(new GulpHelloWorld(),true);
    })
    
    it("should pass through null files",()=>{
        return PluginHelpers.pluginTest(new GulpHelloWorld(),new File(),()=>{});
    })
    it("should transform buffer to buffer", ()=>{
        return PluginHelpers.pluginTest(new GulpHelloWorld(),createBufferFile(""),()=>{});
    })
    it("should append world if contents are hello",()=>{
        return PluginHelpers.singleBufferTransformContentsTest(new GulpHelloWorld(),"hello",(contents)=>{
            expect(contents).toEqual("hello world");
        });
    })
    it("should transform with files if hello hello",()=>{
        return PluginHelpers.transformsWithFilesTest(new GulpHelloWorld(),createBufferFile("hello hello"));
    })
    it("should create two hello world if hello hello",()=>{
        return PluginHelpers.pluginTest(new GulpHelloWorld(),createBufferFile("hello hello"),async (files,error)=>{
            expect(files.length).toBe(2);
            const file1Contents = await getFileContents(files[0]);
            const file2Contents = await getFileContents(files[1]);
            expect(file1Contents).toBe("hello world");
            expect(file2Contents).toBe("hello world");
        });
    })
    it("should filter goodbye",()=>{
        return PluginHelpers.filtersFileTest(new GulpHelloWorld(),createBufferFile("goodbye"));
    })
    it("should ignore all other text",()=>{
        return PluginHelpers.ignoresFileTest(new GulpHelloWorld(),createBufferFile("ignore"));
    })
    
})

File helpers

export function createStreamFile(streamContent:string,cb?:(file:File)=>void):File //StreamFile
export function createBufferFile(bufferContent:string,cb?:(file:File)=>void):File //BufferFile
export function createStreamOrBufferFile(content:string,isStream:boolean,cb?:(file:File)=>void):File

export enum FileType{Buffer,Stream,Null}
export function getFileType(file:File):FileType

export async function getFileContents(file:File):Promise<string|null>