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

maineffectjs

v0.4.5

Published

Dependency Injection for Javascript Functions

Downloads

8

Readme

Maineffect

Reflection based testing for Javascript

Maineffect enables you to write tests faster by helping you easily isolate the test execution path. It does so by using staticially analyzing the code and isolating the function under test. This enables one to ignore required modules and their dependencies.

Maineffect "parses" the module under test into it's AST representation. From there on it lets one traverse the AST to find functions they intend to test.

Demo

Watch the video

Installation

$ npm install maineffectjs

Example #1

Parse/Load the file (Do not require or import). Find the function you want to test by name and callWith arguments.

math.js

import log from 'logger'

const add = (a,b) => a + b

math.test.js

import { parseFn } from '../maineffect'
const math = parseFn(require.resolve('./math'))

describe('add', () => {
	it('should return 2 when called with 1, 1', () => {
		const { result } = math.find('add').callWith(1, 1);
		expect(result).to.equal(2);
	})
})

Here, we wanted to test the add function of math.js. Generally we import the file into our test and call add. However with Maineffect, we parse the raw file, and find the add function. Just like finding a div element in the DOM. We then call it with our arguments.

Advantages

  • We can now test private functions. In math.js above we did not even export add.
  • We dot care about dependencies in the test. Like above, we don't even have a logger module installed.

Example #2

Provide a variable with any value.

taxes.js

import log from 'Logger'
import getTaxeRate from 'irs'

const getAmountAfterTaxes = async (amount) => {
  log('Inside getTaxes')
  const taxRate = await getTaxeRate()
  return amount - amount * taxRate
}

taxes.test.js

import { expect } from 'chai'
import { parseFn } from '../src/maineffect'

const taxes = parseFn(require.resolve('./taxes'))

describe('getAmountAfterTaxes', () => {
    it('should return 50 when called with 100 and a rate of 0.5', async () => {
      	const { result } = taxes.find('getAmountAfterTaxes')
                        .provide({
							log: jest.fn(),
							getTaxeRate: async () => 0.5
						})
                        .callWith(100)
      	expect(await result).to.equal(50)
    })
})

Here, we want to test the getAmountAfterTaxes function of taxes.js. Once we find the function, we provide log as a jest mock and getTaxRate as a function the returns 0.5 and call the function.

Advantages

  • We can mock dependencies like log

Example #3

Stubs provide a fast way to stub out chained function calls.

greet.js

const getGreeting = async () => {
  const greet = greeter.man('Joe').good().greeting();
  return greet;
};

greet.test.js

import { parseFn, Stubs } from '../src/maineffect';

const hello = parseFn(require.resolve('./hello'));

describe('getGreeting', () => {
    it('should call man when greeting', async () => {
		const stubs = Stubs(jest.fn);
      	hello.find('getGreeting')
                        .stub('greeter.man().good().greeting()', stubs.createStub)
                        .callWith();
      	expect(stubs.getStubs().man).toBeCalledWith("Joe");
    });
});

Here, we want to test a side-effect. We want to make sure the function man is called with "Joe". Instead of stubbing the value with an object that looks like the one below ..

{
	greeter: {
		man: jest.fn().mockReturnValue({
				good: jest.fn().mockReturnValue({
					greeting: jest.fn()
				})
			})
	} 
}

We can simply simply say this instead ..

.stub('greeter.man().good().greeting()', stubs.createStub)

Here the stub function take two arguments, a stub-keys and stub creator function. The stub-keys is simply a string that tells maineffectjs what keys should be stubbed with either an object or a stub. If you want a stub, make sure the key ends with a "()". The second parameter is a stub creator. This is wrapper for a Sinon (sinon.stub) or Jest (jest.fn) implementation of mock functions.

Advantages

  • Provide stubs to your tests faster and easier.
  • No need to reconstruct the entire fixture in the test and reset.

Contributions

Build

npx webpack --config webpack.config.js

Test

npm run test

Contact

Reach out to me at @buzzarvind on Twitter for anything. I'll do my best to help out.

License

The MIT License

Copyright (c) 2019-2024 Arvind Naidu https://twitter.com/buzzarvind