Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true

# TypeScript, JavaScript
[*.{ts,tsx,js,jsx}]
indent_style = space
indent_size = 4

# JSON files
[*.json]
indent_style = space
indent_size = 2

# YAML files
[*.{yml,yaml}]
indent_style = space
indent_size = 2

# Markdown files
[*.md]
trim_trailing_whitespace = false
21 changes: 18 additions & 3 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@ name: CI checks

on:
push:
pull_request:

jobs:
checks:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- uses: bahmutov/npm-install@v1
- uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'yarn'

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Lint
run: yarn run lint

- name: Run Tests
run: yarn run test:unit

- name: Build TS
run: yarn run build:tsc --noEmit

- name: Build
run: yarn run build
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ npm-debug.log
/dist
/test-failed-*.png
/output
/coverage
*.log
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"semi": false,
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "all",
"printWidth": 100,
"arrowParens": "always"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018-2025 Nathan Stitt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Uses Proxy objects to create a DSL for object construction. Requires Proxy supp
```javascript

import { sample } from 'lodash';
import faker from 'faker'; // https://github.com/marak/Faker.js
Factory.defaults = { // counld also be a function
import { faker } from '@faker-js/faker';
Factory.defaults = { // could also be a function
driver: 'Speedy',
};
Factory.define('manufacturer')
Expand All @@ -20,7 +20,7 @@ Factory.define('wheel')

Factory.define('car')
.id(Factory.sequence) // driver is set in defaults
.owner(({ diver }) => driver || faker.name.findName())
.owner(({ driver }) => driver || faker.person.fullName())
.manufacturer(Factory.reference('manufacturer'))
.wheels(Factory.reference('wheel', { count: 4 }));

Expand Down
4 changes: 0 additions & 4 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@ module.exports = {
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose": false }]
],
};
24 changes: 24 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
preset: undefined,
testEnvironment: 'node',
roots: ['<rootDir>/src'],
testMatch: ['**/__tests__/**/*.ts', '**/?(*.)+(spec|test).ts'],
transform: {
'^.+\\.ts$': 'babel-jest',
},
collectCoverageFrom: [
'src/**/*.ts',
'!src/**/*.test.ts',
'!src/**/*.d.ts',
],
coverageDirectory: 'coverage',
coverageReporters: ['text', 'lcov', 'html'],
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
},
},
};
61 changes: 36 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@
"name": "object-factory-bot",
"version": "0.8.2",
"description": "Building objects using proxies",
"keywords": [
"factory",
"test",
"testing",
"fixtures",
"proxy",
"object-factory",
"factory-bot",
"test-data"
],
"repository": "https://github.com/nathanstitt/object-factory-bot",
"homepage": "https://github.com/nathanstitt/object-factory-bot#readme",
"bugs": {
"url": "https://github.com/nathanstitt/object-factory-bot/issues"
},
"author": "Nathan Stitt",
"license": "MIT",
"typings": "dist/factory.d.ts",
Expand All @@ -15,48 +29,45 @@
"unpkg": "dist/object-factory-bot.umd.js",
"source": "src/factory.ts",
"engines": {
"node": ">=10"
"node": ">=18"
},
"scripts": {
"build": "microbundle --no-compress",
"build:tsc": "tsc",
"build:tsc:watch": "tsc --watch",
"start": "microbundle-crl watch --no-compress --format modern,cjs",
"test:unit": "jest",
"test:coverage": "jest --coverage",
"test": "run-s test:unit lint test:build",
"test:build": "run-s build",
"lint": "eslint src/*ts"
},
"peerDependencies": {},
"devDependencies": {
"@babel/core": "^7.13.10",
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/plugin-proposal-decorators": "^7.13.5",
"@babel/preset-env": "^7.13.10",
"@babel/preset-typescript": "^7.13.0",
"@testing-library/jest-dom": "^5.11.9",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.33",
"@typescript-eslint/eslint-plugin": "^4.17.0",
"@typescript-eslint/parser": "^4.17.0",
"babel-jest": "^26.6.3",
"@babel/core": "^7.24.0",
"@babel/preset-env": "^7.24.0",
"@babel/preset-typescript": "^7.24.0",
"@testing-library/jest-dom": "^6.4.0",
"@types/jest": "^29.5.0",
"@types/node": "^20.11.0",
"@typescript-eslint/eslint-plugin": "^7.0.0",
"@typescript-eslint/parser": "^7.0.0",
"babel-jest": "^29.7.0",
"cross-env": "^7.0.3",
"eslint": "7.21.0",
"eslint-config-argosity": "3.1.0",
"eslint-config-prettier": "^8.1.0",
"eslint-config-standard": "^16.0.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-promise": "^4.3.1",
"jest": "^26.6.3",
"microbundle": "^0.13.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-prettier": "^5.1.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"microbundle": "^0.15.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.2.1",
"typescript": "^4.2.3"
"prettier": "^3.2.0",
"typescript": "^5.3.0"
},
"files": [
"dist"
],
"dependencies": {}
"dependencies": {},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
4 changes: 2 additions & 2 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ const Factory: FactoryI = {
},

create(factoryName: string, ctx: Record<string, any> = {}): Record<string, any> {
const object = {}
const object: Record<string, any> = {}
const defaults = 'function' === typeof Factory.defaults ?
Factory.defaults(factoryName, ctx) : Factory.defaults
const context = Object.assign({ object }, defaults, ctx)
const factory = getFactory(factoryName, false)
const factory = getFactory(factoryName, false) as Record<string, any>
Object.keys(factory).forEach((key) => {
context.key = key
if (factory[key].isReference || 'undefined' === typeof ctx[key]) {
Expand Down
2 changes: 1 addition & 1 deletion src/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Reference {
}

factory(factory: Factory, propertyName: string): any {
const func = (context: object) => this.create(factory, Object.assign(
const func = (context: Record<string, any>) => this.create(factory, Object.assign(
context[propertyName] || {},
{ parent: context, parentProperty: propertyName },
));
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"allowSyntheticDefaultImports": true,
"target": "es5",
"target": "ES2020",
"allowJs": true,
"skipLibCheck": true,
"strict": true,
Expand Down
Loading