Comments (7)
@mattvperry should be fixed in 0.3.4. Thanks for providing a breaking test. 😄
from factory.ts.
I think that's possible, but it may require some markup on the factory. E.g.
const groceryStoreFactory = factory.makeFactory<IGroceryStore>({
aisle: factory.nested({
name: 'Junk Food Aisle',
typeOfFood: 'Junk Food',
budget: 3000
})
})
Also, if you have an optional property in a type, but a factory declares a default real value (e.g. 123
for type number?
), you will probably have to explicitly set it to undefined
when you call build() if you want to switch to not having a value.
Anyway, I'm super busy for the next month, but I will put this on my list for early 2018. I might have enough time to look at a pull request, though, if someone wants to jump in. ;)
from factory.ts.
I've taken a swing at this with Nested Partials. I'm mildly afraid I've broken other stuff, particularly on the compile time side of things (which is a bit harder to write a test for). But please pull it down and see if it works for you. I'm out of time to work on this today, but as soon as I can I'll do a bit more checking and then merge it in.
from factory.ts.
This has broken a different feature:
interface Entity {
a: string,
b?: string
}
const factory = Factory.makeFactory<Entity>({
a: Factory.each(i => `Entity_${i}`)
});
var e = factory.build({ b: 'test' })
console.log(e.b) // this should be 'test' but is undefined because the new code is only overwriting keys that it can find and not adding new keys.
from factory.ts.
@mattvperry Thanks for the heads up. I will try to have a fix out in the next few days.
from factory.ts.
@willryan thank you for the quick turn around!
from factory.ts.
@willryan what ever happened to this?
I've found this is pretty much a requirement for testing in TypeScript, and can't find any factory libraries for doing this.
Here's an example of the kind of types I'm working with (for a project using AwsLex):
interface LexIntent {
name: string;
slots: StringMap;
slotDetails: Record<keyof LexIntent['slots'], SlotDetails>;
confirmationStatus: 'Confirmed' | 'Denied' | 'None';
sourceLexNLUIntentInterpretation: unknown;
}
export interface LexInputEvent {
messageVersion: string;
invocationSource: string;
userId: string;
sessionAttributes: object;
requestAttributes: Nullable<Record<LexHeader, string>>;
bot: LexBot;
outputDialogMode: 'Text' | 'Voice';
currentIntent: LexIntent;
inputTranscript: string;
}
In Lex, LexInputEvent#currentIntent#name
is a key property to figure out what logic to actually do - naturally in my unit tests usually only few of the properties (root-level or nested) are being tested at a time, so I'm wanting to be able to do something like:
Factory.makeLexInputEvent({ currentIntent: { name: 'greeting' } });
without TypeScript having a fit at me 😂
Understandably I don't expect this to be a 100% painless process, since this is in a manner going against the whole point of TypeScript, but I think it would be great to have a library that'd make this as easy as possible!
It seems that this issue fits with what I'm after, but as I said, I can't see any evidence on this issue that the feature was actually implemented, as the README doesn't mention anything close to nested types.
On usage, I see that deep nesting works like a charm:
const LexInputEventFactory = Factory.makeFactory<LexInputEvent>({
messageVersion: '1.0',
invocationSource: 'DialogCodeHook',
userId: 'lex-user',
sessionAttributes: {},
requestAttributes: null,
bot: {
name: 'LexBot',
alias: 'Bot',
version: '$LATEST'
},
outputDialogMode: 'Text',
currentIntent: {
name: 'goodbye',
slots: {},
slotDetails: {},
confirmationStatus: 'None',
sourceLexNLUIntentInterpretation: null
},
inputTranscript: 'Goodbye'
});
console.log(LexInputEventFactory.build({ currentIntent: { name: 'hello world' } }));
// output:
/*
{ messageVersion: '1.0',
invocationSource: 'DialogCodeHook',
userId: 'lex-user',
sessionAttributes: {},
requestAttributes: null,
bot: { name: 'LexBot', alias: 'Bot', version: '$LATEST' },
outputDialogMode: 'Text',
currentIntent:
{ name: 'hello world',
slots: {},
slotDetails: {},
confirmationStatus: 'None',
sourceLexNLUIntentInterpretation: null },
inputTranscript: 'Goodbye' }
*/
I think you should add an example of this to the README, as like I said I think it's one of the most powerful & important features.
However, what's not supported is the ability to provide a factory
in a factory
:
const LexIntentFactory = Factory.makeFactory<LexIntent>({
name: 'goodbye',
slots: {},
slotDetails: {},
confirmationStatus: 'None',
sourceLexNLUIntentInterpretation: null
});
const LexInputEventFactory = Factory.makeFactory<LexInputEvent>({
messageVersion: '1.0',
invocationSource: 'DialogCodeHook',
userId: 'lex-user',
sessionAttributes: {},
requestAttributes: null,
bot: {
name: 'LexBot',
alias: 'Bot',
version: '$LATEST'
},
outputDialogMode: 'Text',
currentIntent: LexIntentFactory,
inputTranscript: 'Goodbye'
});
Let me know if you'd like this put into a separate issue!
from factory.ts.
Related Issues (20)
- How to randomize buildList results HOT 5
- [Feature request]: makeFactory accepts function parameter HOT 3
- [feature proposal] Allow to specify factory as field default value of another factory HOT 1
- Use value from another field HOT 5
- Current state of the library HOT 2
- Node version bump causes install to fail HOT 2
- Can't use Factory.each to custimize value of build HOT 1
- Derivation using the existing value HOT 2
- Feature request: `create` and `createList`
- Feature request: `resetAllSequenceNumbers`
- Feature request: pass to makeFactory function that returns Builder instead object Builder HOT 1
- This repo does not seem maintained anymore HOT 2
- Combine factories with optional field HOT 3
- Pipelines as generators HOT 1
- Question: How to create a factory outputs tuples? HOT 1
- Failed to parse source maps HOT 1
- How to randomise nested object key in `buildList` HOT 1
- Default merging of objects within factories?
- Possible to allow "transient" values? HOT 1
- Overriding string values with objects, gives an object with string characters as object keys
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from factory.ts.