essyad
AuthorsQuotesAbout← Portfolio
AuthorsQuotesAbout← Portfolio
Language
© 2026
RSSPortfolioTwitterGitHub
← Writing

Why Your Node Tests Are Failing (And It's Not Your Code)

Getting 'Unable to deserialize cloned data' errors in Node tests? Here's why storing Mongoose documents at suite level breaks serialization—and the simple fix that saved my New Year's.

Ahmed essyad
ByAhmed essyadJanuary 20262 min read
—

it's 00:45. new year's day 2026. everyone's out celebrating and i'm here staring at this:

Error: Unable to deserialize cloned data due to invalid or unsupported version.

great. just great.

320 tests passing. then two random ones decide to break. didn't even touch that code. what happened?

turns out i'm an idiot

been storing mongoose documents at the suite level. like this:

describe('BadgeService', () => {
  let testUserId;
  let testUser;  // yeah this is the problem

  beforeEach(async () => {
    testUser = await createTestUser({...});
    testUserId = testUser._id.toString();
  });

  test('does something', async () => {
    testUser.donations_given.push({...});
    await testUser.save();
  });
});

looks fine right? nope.

why it breaks

node's test runner tries to serialize everything. mongoose documents? they're full of circular references, functions, internal state, all kinds of stuff that can't be serialized.

so node just dies trying to clone it between tests.

the fix

don't store complex objects. just don't.

describe('BadgeService', () => {
  let testUserId;  // just the id

  beforeEach(async () => {
    const testUser = await createTestUser({...});
    testUserId = testUser._id.toString();
  });

  test('does something', async () => {
    const testUser = await User.findById(testUserId);  // fetch it fresh
    testUser.donations_given.push({...});
    await testUser.save();
  });
});

store the id. fetch when you need it. that's it.

other stuff that'll break

  • mongoose/sequelize/any orm instances
  • database connections
  • class instances
  • anything with circular references
  • functions
  • basically anything that's not a plain string/number/object

the rule

only store simple stuff at the suite level. need something complex? store an id and fetch it in the test.

bonus: your tests are actually more isolated now anyway. so you're doing it right by accident.

anyway

if you get weird serialization errors, check what you're storing at the suite level. probably that.

now if you'll excuse me, i have a new year to catch the tail end of.

✳

written at 00:45, january 1st 2026, because apparently this is how i spend new year's now

Ahmed essyad

Ahmed essyad

the owner of this space

A nerd? Yeah, the typical kind—nah, not really.

View all articles by Ahmed essyad→

Comments

If this resonated

I write essays like this monthly.

Continue reading

Next →

The "Wait, Should I Fix the Code or the Test?" Moment ; AI Finally Gets It