Skip to content

Instantly share code, notes, and snippets.

@nikku
Last active May 7, 2026 08:54
Show Gist options
  • Select an option

  • Save nikku/1dc2edb553565238c7bf to your computer and use it in GitHub Desktop.

Select an option

Save nikku/1dc2edb553565238c7bf to your computer and use it in GitHub Desktop.
Good unit / integration tests. A template.

Good Unit Tests

... are structured into given, (assume), when and then.

given

What do I actually assume to be already working / what needs to be set up?

assume

(Optional) I assume this setup for my tests to work.

when

Time to execute something!

then

Assert that stuff you executed actually works as expected.

FAQ

I have multiple when / then blocks!

You are probably doing it wrong. Tests should be concise, self contained and focused.

Try to write multiple test cases for each when/then block.

I cannot get this structure running in JavaScript

What about using something like this as your test template?

// given
var x = 10;

// when
var y = 10 + x;

// then
expect(y).to.eql(20);

I need to provide explanatory text for what I assert!

Ensure you only document the necessary - more content is not better. If needed, add an additional comment above an assertion to document things.

// given
var element = ...;
var parent = element.parent;

// when
moveElement(element, { x: 300 });

// then
// element moved out of parent, must have a new parent now
expect(element.parent).not.to.eql(parent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment