Comments (5)
I prefer the factory approach. it's cleaner and remove irrelevant/repetitive LOCs from the test.
Also, I want to "call and raise" this idea.
What I'd really like, is a declarative way to do this to include sub resources, for example:
createMeeting({
name: '2020 Summary',
startsAt: new Date(),
topics: [
{ }, // create topic with default values
{
name: 'topic 2',
items: 2 // create 2 items with default values inside topic 2
}
]
});
Now, I'm not sure if it's worth the effort. but our sub-resources starts with:
it ('should...', () => {
// Arrange
const meeting = await createMeeting();
const [topic1, topic 2] = Promise.all([createTopic({ meetingId: meeting,id }), createTopic({ meetingId: meeting,id, name: 'topic2' })]);
const items = Promise.all([createItem({ topicId: topic1.id }), createItem({ topicId: topic2.id })]);
});
...
And so on.
Sometimes we create some file-scoped main resources (like meeting and topics) and use them in all tests but not sure if this the best approach.
WDYT?
p.s: I thought about this "declarative API" (😅) while writing this comment so don't stick to this specific implementation.. but the idea in general.
from nodejs-integration-tests-best-practices.
Whenever there is a big and nested object, a factory is the only way. There are multiple design dimensions to consider:
-
Stateless vs stateful - The factory can generate the next sequence vs the test/factory uses some random to create unique data
-
Home-made factory vs 3rd like Rosie. The later also dictates more boilerplate code to define the schema
-
Test is aware of the structure vs agnostic - Should we need to craft a meeting, our factory may fully abstract the structure of it:
testHelper.factorMetting(howManyTopics, meetingValuesOverrides: {name: 'Kickoff'}) // Using this style, the tests are really short and not bogged down with the objects schema.
That said, their flexibility to alter the structure is diminished, so you'll more often see stuff like:
theFactoredMeeting.topics.push(...)
Another style is what you exemplified where the tests are very involved in the object graph and might get quite long
In your example, what createMeeting function does given that it already gets almost the entire object?
from nodejs-integration-tests-best-practices.
It get only values you want.. (otherwise it will take the default values)
And it creates the resources for the test.
from nodejs-integration-tests-best-practices.
@mikicho @DanielGluskin As I experimented with this, I believe that random is the only way to go because it is unique across files, processes and time (unlike rosie increment). WDYT?
module.exports.getShortUnique = () => {
const now = new Date();
// We add this weak random just to cover the case where two test started at the very same millisecond
const aBitOfMoreSalt = Math.ceil(Math.random() * 99);
return `${process.pid}${aBitOfMoreSalt}${now.getMilliseconds()}`;
};
from nodejs-integration-tests-best-practices.
LGTM
we can also use nano id instead
from nodejs-integration-tests-best-practices.
Related Issues (20)
- Make TOC and recipe list (homepage) clickable
- Write last 3 best practices
- In Collab section - Write about yourself
- Create contributors table
- There are 2 api.js
- Create my-first-pr issues HOT 2
- MQ best practices HOT 2
- Create stale bot
- Create contributors bot
- Add more mySQL optimization flags
- Close sequalize connection in stopWebServer HOT 1
- Node Best
- Why mark `async` in the callback of `beforeAll`? HOT 1
- Nock with remote swagger HOT 1
- Lazy watch plugin
- VSCode snippets for integration tests
- DB cleanup recepie
- Create Fastify example HOT 1
- Put code examples
- put collaborators section HOT 1
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 nodejs-integration-tests-best-practices.