How to define traits when not using typescript?
Closed this issue · 3 comments
I am not using typescript, so the documentation is a bit hard for me to read. How would I define a trait for a simple factory that I export here:
import { Factory } from 'fishery';
export default Factory.define(({ sequence }) => ({
id: sequence,
manga_series_id: 1,
name: 'MangaDex',
seriesURL: 'example.com',
lastVolumeAvailable: 1,
lastChapterAvailable: 2,
}));
@doutatsu here is an example of using traits with just JavaScript. The idea is to subclass Factory
, and add your trait methods to that subclass:
class UserFactory extends Factory {
admin() {
return this.params({ admin: true });
}
}
const factory = UserFactory.define(() => {
return {
admin: false,
name: 'Jan'
}
})
const user = factory.admin().build()
user.admin // true
Let me know if that helps or if you have further questions!
Finally got around to trying this out @stevehanson and it worked like a charm! Thanks a lot
Hi @stevehanson - does there need to be anything special for the factory to live in a separate file?
The above example works when it's in a single file but when I try to break it out into
const { Factory } = require('fishery');
class DatasetFactory extends Factory {}
const factory = DatasetFactory.define(({ teamId, storageProviderId }) => ({
teamId,
storageProviderId,
isPublic: false,
}));
module.export = factory;
const assert = require('chai').assert;
const DatasetFactory = require('test/factories/dataset');
describe.only('dataset', () => {
it('does stuff', async () => {
const dataset = DatasetFactory.build({
teamId: 1,
storageProviderId: 1,
});
assert.isNotNull(dataset);
});
});
I get a error saying
1) dataset
does stuff:
TypeError: DatasetFactory.build is not a function
Maybe this is just a JS issue I'm tripping over. Thanks for your help so far!