Coder Social home page Coder Social logo

Comments (5)

mikicho avatar mikicho commented on May 20, 2024

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.

goldbergyoni avatar goldbergyoni commented on May 20, 2024

@mikicho

Whenever there is a big and nested object, a factory is the only way. There are multiple design dimensions to consider:

  1. Stateless vs stateful - The factory can generate the next sequence vs the test/factory uses some random to create unique data

  2. Home-made factory vs 3rd like Rosie. The later also dictates more boilerplate code to define the schema

  3. 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.

mikicho avatar mikicho commented on May 20, 2024

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.

goldbergyoni avatar goldbergyoni commented on May 20, 2024

@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.

mikicho avatar mikicho commented on May 20, 2024

LGTM
we can also use nano id instead

from nodejs-integration-tests-best-practices.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.