Installations
npm install js-factories
Releases
Unable to fetch releases
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
NPM Version
1.4.22
Statistics
26 Stars
34 Commits
4 Forks
4 Watching
1 Branches
3 Contributors
Updated on 09 Nov 2021
Languages
CoffeeScript (99.23%)
Shell (0.77%)
Total Downloads
Cumulative downloads
Total Downloads
673,914
Last day
16.3%
399
Compared to previous day
Last week
10.3%
1,911
Compared to previous week
Last month
0.3%
7,890
Compared to previous month
Last year
-38.1%
123,275
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
4
js-factories
js-factories is a library to use dynamic fixtures using Factories in javascript/coffeescript. Ideal to combine using Mocha/chai frameworks and test rich classes for MV** Frameworks like Backbone.js
Usage
Include js-factories.js
in your test suite.
Factory support is added to quickly be able to build models or other objects as you see fit:
1 Factory.define('user', function(attributes) { 2 if (attributes == null) { 3 attributes = {}; 4 } 5 return new User(attributes); 6 }); 7 8 Factory.create('user', { name: 'Matthijs' }) 9 Factory.createList(10, 'user', { name: 'Matthijs' })
Traits
you can also use 'traits'. Traits are flags that are set when the user calls create with the factory name prefixed with terms separated by dashes.
Like: 'female-admin-user'
This will call the 'user' factory, and provide the terms 'female' and 'admin' as traits for this user
this list is accessible in the factory callback using this.traits
There are 2 helper methods to help check if traits are set:
1this.trait('returns', 'one', 'of', 'these', 'values')
and
1this.is('admin') // returns a boolean value
Extended example:
1 Factory.define('user', function(attributes) { 2 var returningClass; 3 if (attributes == null) { 4 attributes = {}; 5 } 6 attributes.gender = this.trait('male', 'female') || 'male'; 7 returningClass = User; 8 if (this.is('admin')) { 9 returningClass = AdminUser; 10 } 11 return new returningClass(attributes); 12 }); 13 14 Factory.create('user', { name: 'Matthijs' }) // => new User name: 'Matthijs' 15 Factory.create('male-user', { name: 'Matthijs' }) // => new User name: 'Matthijs', gender: 'male' 16 Factory.create('male-admin-user', { name: 'Matthijs' }) // => new AdminUser name: 'Matthijs', gender: 'male' 17 Factory.create('female-user', { name: 'Beppie' }) // => new User name: 'Beppie', gender: 'female'
Sequences
Sequences are also supported:
1 Factory.define('counter', function() { 2 return { 3 amount: this.sequence('amount'), 4 other: this.sequence('other') 5 }; 6 });
This does not conflict with similar names in other factory definitions.
You can also yield results:
1 Factory.define('abc', function() { 2 return this.sequence(function(i) { 3 return ['a', 'b', 'c'][i]; 4 }); 5 }); 6 7 // results in: 8 Factory.create('abc') // => 'a' 9 Factory.create('abc') // => 'b'
Sampling
You can sample a value from a list
1 Factory.define('sampler', function() { 2 return this.sample('a', 'b', 'c'); 3 });
Will randomly return a, b or c every time
License
Copyright (c) 2012-2014 Matthijs Groen
MIT License (see the LICENSE file)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 4/26 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 9 are checked with a SAST tool
Score
3.2
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to js-factories
chai-js-factories
js-factories integration with Chai
karma-chai-js-factories
chai-js-factories plugin for Karma
react-dom-factories
React package for DOM factory methods.
fishery
A library for setting up JavaScript factories to help build objects as test data, with full TypeScript support