Skip to main content
Tweeted twitter.com/StackProgrammer/status/702937797938782209
added 132 characters in body
Source Link

I am looking to get some input from some more experienced testers than what I am. :)

I am trying to make my node modules testable, allowing for dependency spying/stubbing/mocking without the need to use a magic library such as rewire or mockery to intercept my module import/require statements.

I was thinking of using an approach whereby I would create a factory function within each module. The factory function accepts the required dependencies for the module to export. The default export would call the factory function using the standard import statements from the module. I would also export the factory function itself allowing for my tests to inject whatever dependencies they like.

Here is an example (which is completely contrived and simplified to highlight the design approach):

import { foo } from './utils/foo'; // We export our factory, exposing it to consumers. export const factory = (dependencies = {}) => { const { $foo = foo // defaults to standard imp if none provided. } = dependencies; return function bar() { return $foo(); } }   // The default implementation, which would end up using default deps. export default factory(); 

Thoughts? Is this too verbose? Is there a better mechanism of achieving the same? I am slightly concerned about the boiler plate additions that I will be adding to my project, but perhaps the trade off is worth it.

I am looking to get some input from some more experienced testers than what I am. :)

I am trying to make my node modules testable, allowing for dependency spying/stubbing/mocking without the need to use a magic library such as rewire or mockery to intercept my module import/require statements.

I was thinking of using an approach whereby I would create a factory function within each module. The factory function accepts the required dependencies for the module to export. The default export would call the factory function using the standard import statements from the module. I would also export the factory function itself allowing for my tests to inject whatever dependencies they like.

Here is an example (which is completely contrived and simplified to highlight the design approach):

import { foo } from './utils/foo'; export const factory = (dependencies = {}) => { const { $foo = foo // defaults to standard imp if none provided. } = dependencies; return function bar() { return $foo(); } } export default factory(); 

Thoughts? Is this too verbose? Is there a better mechanism of achieving the same? I am slightly concerned about the boiler plate additions that I will be adding to my project, but perhaps the trade off is worth it.

I am looking to get some input from some more experienced testers than what I am. :)

I am trying to make my node modules testable, allowing for dependency spying/stubbing/mocking without the need to use a magic library such as rewire or mockery to intercept my module import/require statements.

I was thinking of using an approach whereby I would create a factory function within each module. The factory function accepts the required dependencies for the module to export. The default export would call the factory function using the standard import statements from the module. I would also export the factory function itself allowing for my tests to inject whatever dependencies they like.

Here is an example (which is completely contrived and simplified to highlight the design approach):

import { foo } from './utils/foo'; // We export our factory, exposing it to consumers. export const factory = (dependencies = {}) => { const { $foo = foo // defaults to standard imp if none provided. } = dependencies; return function bar() { return $foo(); } }   // The default implementation, which would end up using default deps. export default factory(); 

Thoughts? Is this too verbose? Is there a better mechanism of achieving the same? I am slightly concerned about the boiler plate additions that I will be adding to my project, but perhaps the trade off is worth it.

deleted 6 characters in body
Source Link

Okay Respectable design pattern for making node modules testableflexible/testable?

I am looking to get some input from some more experienced testers than what I am. :)

I am trying to make my node modules testable, allowing for dependency spying/stubbing/mocking without the need to use a magic library such as rewire or mockery to intercept my module import/require statements.

I was thinking of using an approach whereby I would create a factory function within each module. The factory function accepts the required dependencies for the module to export. The default export would call the factory function using the standard import statements from the module. I would also export the factory function itself allowing for my tests to inject whatever dependencies they like.

Here is an example (which is completely contrived and simplified to highlight the design approach):

import { foo } from './utils/foo'; export const barFactory = (_foo) => { return function bar() { return _foo(); } } export default barFactory(foo); 

Thoughts? Is this too verbose? Is there a better mechanism of achieving the same? I am slightly concerned about the boiler plate additions that I will be adding to my project, but perhaps the trade off is worth it.

Update

The below approach could also be interesting, allowing for named dependency replacement as well as defaults implementation via destructing. Handy if I don't want to replace every dependency.

import { foo } from './utils/foo'; export const factory = (dependencies = {}) => { const { $foo = foo // defaults to standard imp if none provided. } = dependencies; return function bar() { return $foo(); } } export default factory(); 

Thoughts? Is this too verbose? Is there a better mechanism of achieving the same? I am slightly concerned about the boiler plate additions that I will be adding to my project, but perhaps the trade off is worth it.

Okay design pattern for making node modules testable?

I am looking to get some input from some more experienced testers than what I am. :)

I am trying to make my node modules testable, allowing for dependency spying/stubbing/mocking without the need to use a magic library such as rewire or mockery to intercept my module import/require statements.

I was thinking of using an approach whereby I would create a factory function within each module. The factory function accepts the required dependencies for the module to export. The default export would call the factory function using the standard import statements from the module. I would also export the factory function itself allowing for my tests to inject whatever dependencies they like.

Here is an example (which is completely contrived and simplified to highlight the design approach):

import { foo } from './utils/foo'; export const barFactory = (_foo) => { return function bar() { return _foo(); } } export default barFactory(foo); 

Thoughts? Is this too verbose? Is there a better mechanism of achieving the same? I am slightly concerned about the boiler plate additions that I will be adding to my project, but perhaps the trade off is worth it.

Update

The below approach could also be interesting, allowing for named dependency replacement as well as defaults implementation via destructing. Handy if I don't want to replace every dependency.

import { foo } from './utils/foo'; export const factory = (dependencies = {}) => { const { $foo = foo // defaults to standard imp if none provided. } = dependencies; return function bar() { return $foo(); } } export default factory(); 

Respectable design pattern for making node modules flexible/testable?

I am looking to get some input from some more experienced testers than what I am. :)

I am trying to make my node modules testable, allowing for dependency spying/stubbing/mocking without the need to use a magic library such as rewire or mockery to intercept my module import/require statements.

I was thinking of using an approach whereby I would create a factory function within each module. The factory function accepts the required dependencies for the module to export. The default export would call the factory function using the standard import statements from the module. I would also export the factory function itself allowing for my tests to inject whatever dependencies they like.

Here is an example (which is completely contrived and simplified to highlight the design approach):

import { foo } from './utils/foo'; export const factory = (dependencies = {}) => { const { $foo = foo // defaults to standard imp if none provided. } = dependencies; return function bar() { return $foo(); } } export default factory(); 

Thoughts? Is this too verbose? Is there a better mechanism of achieving the same? I am slightly concerned about the boiler plate additions that I will be adding to my project, but perhaps the trade off is worth it.

deleted 6 characters in body
Source Link

I am looking to get some input from some more experienced testers than what I am. :)

I am trying to make my node modules testable, allowing for dependency spying/stubbing/mocking without the need to use a magic library such as rewire or mockery to intercept my module import/require statements.

I was thinking of using an approach whereby I would create a factory function within each module. The factory function accepts the required dependencies for the module to export. The default export would call the factory function using the standard import statements from the module. I would also export the factory function itself allowing for my tests to inject whatever dependencies they like.

Here is an example (which is completely contrived and simplified to highlight the design approach):

import { foo } from './utils/foo'; export const barFactory = (_foo) => { return function bar() { return _foo(); } } export default barFactory(foo); 

Thoughts? Is this too verbose? Is there a better mechanism of achieving the same? I am slightly concerned about the boiler plate additions that I will be adding to my project, but perhaps the trade off is worth it.

Update

The below approach could also be interesting, allowing for named dependency replacement as well as defaults implementation via destructing. Handy if I don't want to replace every dependency.

import { foo } from './utils/foo'; export const barFactoryfactory = (dependencies: ?Object= {}) => { const { _foo$foo = foo // defaults to standard imp if none provided. } = dependencies || {};dependencies; return function bar() { return _foo$foo(); } } export default barFactoryfactory(); 

I am looking to get some input from some more experienced testers than what I am. :)

I am trying to make my node modules testable, allowing for dependency spying/stubbing/mocking without the need to use a magic library such as rewire or mockery to intercept my module import/require statements.

I was thinking of using an approach whereby I would create a factory function within each module. The factory function accepts the required dependencies for the module to export. The default export would call the factory function using the standard import statements from the module. I would also export the factory function itself allowing for my tests to inject whatever dependencies they like.

Here is an example (which is completely contrived and simplified to highlight the design approach):

import { foo } from './utils/foo'; export const barFactory = (_foo) => { return function bar() { return _foo(); } } export default barFactory(foo); 

Thoughts? Is this too verbose? Is there a better mechanism of achieving the same? I am slightly concerned about the boiler plate additions that I will be adding to my project, but perhaps the trade off is worth it.

Update

The below approach could also be interesting, allowing for named dependency replacement as well as defaults implementation via destructing. Handy if I don't want to replace every dependency.

import { foo } from './utils/foo'; export const barFactory = (dependencies: ?Object) => { const { _foo = foo // defaults to standard if none provided. } = dependencies || {}; return function bar() { return _foo(); } } export default barFactory(); 

I am looking to get some input from some more experienced testers than what I am. :)

I am trying to make my node modules testable, allowing for dependency spying/stubbing/mocking without the need to use a magic library such as rewire or mockery to intercept my module import/require statements.

I was thinking of using an approach whereby I would create a factory function within each module. The factory function accepts the required dependencies for the module to export. The default export would call the factory function using the standard import statements from the module. I would also export the factory function itself allowing for my tests to inject whatever dependencies they like.

Here is an example (which is completely contrived and simplified to highlight the design approach):

import { foo } from './utils/foo'; export const barFactory = (_foo) => { return function bar() { return _foo(); } } export default barFactory(foo); 

Thoughts? Is this too verbose? Is there a better mechanism of achieving the same? I am slightly concerned about the boiler plate additions that I will be adding to my project, but perhaps the trade off is worth it.

Update

The below approach could also be interesting, allowing for named dependency replacement as well as defaults implementation via destructing. Handy if I don't want to replace every dependency.

import { foo } from './utils/foo'; export const factory = (dependencies = {}) => { const { $foo = foo // defaults to standard imp if none provided. } = dependencies; return function bar() { return $foo(); } } export default factory(); 
added 532 characters in body
Source Link
Loading
Source Link
Loading