
Jest – Painless JavaScript Testing - vjeux
http://facebook.github.io/jest/index.html
======
spicyj
As someone who's become a React core contributor since React was released a
year ago and had the opportunity to work with a handful of people from the
Facebook frontend team, I want to say that I'm really impressed with the
quality of the JavaScript libraries that Facebook has released. React is the
obvious big one, but other smaller projects like regenerator and jstransform
are awesome too. I'm excited to see Jest join the party.

Congrats guys. :)

------
chenglou
Shorter feedback loop is always great. Running tests in parallel is a non-
brainer.

Leveraging Jasmine is great too. Glad to see it's not reinventing the wheel!

Here's another interesting part, on dependency injection:
[http://facebook.github.io/jest/docs/common-js-
testing.html#c...](http://facebook.github.io/jest/docs/common-js-
testing.html#content)

~~~
nawitus
I would've built Jest on top of Mocha. It's a slightly better testing
framework, especially when it comes to async tests.

~~~
vjeux
Mock functions are a really powerful tool. They let you test asynchronous
functions in an entirely synchronous way. Because of that, Jest doesn't need
to provide special support for asynchronous testing

[http://facebook.github.io/jest/docs/tutorial.html#content](http://facebook.github.io/jest/docs/tutorial.html#content)

~~~
couchand
That might work for simple cases but I'd be worried that switching all your
asynchronous calls to synchronous has the potential to introduce bugs that
your tests would then miss. Testing asynchronous functions is hard for lots of
reasons, and only one of them is that you have to wait for it to finish.

------
nlh
Sorry for the dumb question, but as a JS (and JS testing) newbie, could
someone explain how Jest is different from Jasmine (from which it's built on)
or, say, from Mocha?

~~~
vjeux
Jasmine provides you with the testing primitives such as describe(), it(),
expect(value).toEqual(other):
[http://facebook.github.io/jest/docs/api.html#globally-
inject...](http://facebook.github.io/jest/docs/api.html#globally-injected-
variables)

Jest adds multiple layers on-top:

\- A mock library (also called stubs) in order to create fake functions and
assert that they are called as you want:
[http://facebook.github.io/jest/docs/mock-
functions.html#cont...](http://facebook.github.io/jest/docs/mock-
functions.html#content)

\- A way to provide an alternate implementation for dependencies using
require(): [http://facebook.github.io/jest/docs/common-js-
testing.html#c...](http://facebook.github.io/jest/docs/common-js-
testing.html#content)

\- It inspects the shape of your dependencies and return a mocked version of
them: [http://facebook.github.io/jest/docs/automatic-
mocking.html#c...](http://facebook.github.io/jest/docs/automatic-
mocking.html#content)

\- Finally, it works in node instead of the browser and uses jsdom to let you
test code related to the DOM. (It also runs tests in parallel)

~~~
kumar303
Can someone copy/paste this into the docs? :) It was really helpful, thanks.

~~~
jeffmo
I went ahead and added it to the main page of the website:

[http://facebook.github.io/jest/](http://facebook.github.io/jest/)

------
etrinh
This looks cool. Manual mocking has always been, if not a pain, a major
annoyance.

On a side note, I got a good chuckle from the "Bobby Tables" XKCD reference[1]
in the Jest tutorial[2].

[1]: [http://xkcd.com/327/](http://xkcd.com/327/) [2]:
[http://facebook.github.io/jest/docs/tutorial.html](http://facebook.github.io/jest/docs/tutorial.html)

------
pajtai
Wish all it was was a mocking library... w/o the testing built in.

That way you could use it easily with Mocha (and should, etc).

~~~
jeffmo
(Hi, I'm the fb eng who wrote Jest)

Yea, I'd really like to add support for other testing frameworks as well.

Right now we use jasmine internally, so I was pretty focused on getting it
running with that. However, it is (quite intentionally) built such that the
'jasmine' parts are pretty isolated similar to a plug-in; So it should be
possible to build plug-ins for other frameworks as well (like Mocha, QUnit,
etc) in the future.

~~~
glenjamin
This looks really neat, having to re-specify the interface of the
collaborators is generally a bunch of boilerplate you don't want to need!

Did you consider building on top of Sinon.JS and only having jest do the
require/detect/build-mock part? Your mock API seems very similar.

~~~
jeffmo
I hadn't looked at Sinon.JS before, but it does seem worth checking
into...thanks!

------
rmrfrmrf
Wow, Facebook has been rocking the house lately with their JS contributions.
Great work and thank you!

------
peteratt
Might be a silly question, but has anyone tried Jest along with AngularJS and
survived to tell the story? Mocking stuff has always been one of my biggest
pain points when testing on Angular and I'm really itching to try this!
Congrats folks at Facebook.

~~~
camus2
Explain me how mocking is difficult with Angular? when Angular has a mocking
framework.

~~~
peteratt
Where did I say "difficult"? I said "pain point", which is different. In my
experience, same as your sibling comment, you end up having a lot of
boilerplate and it's just convoluted. Mocking out stuff should be simpler, in
my opinion. How about auto-generating Jasmine spies, for example?

------
polskibus
Resharper added jasmine support in v.7 as far as I remember. I wonder if Jest
would work with Resharper out of the box, would be helpful in .NET world to
have the Jest runner integrated with VS.

------
Cthulhu_
Looks interesting: the test suite for our current AngularJS project is slowly
slowing down, in part because there's just more tests, but the major
performance bottlenecks are:

* No parallelisation, even if test suites are all independent * DOM tests, which cause a lot of GC pauses * (probably) PhantomJS startup and initialisation (not measured)

I've done a simple optimization where my tests get split in the middle and run
in two separate terminals (during development, continuous testing), but it's
kinda iffy.

------
elwell
I wonder if they are using CoffeeScript anywhere at Facebook?

[http://facebook.github.io/jest/docs/tutorial-
coffeescript.ht...](http://facebook.github.io/jest/docs/tutorial-
coffeescript.html#content)

~~~
vjeux
We're not using CoffeeScript inside of Facebook. But we are using many ES6
features that make JS look a lot like CoffeeScript such as classes, arrow
functions, rest arguments, destructuring assignment ... I figured that
CoffeeScript would be a good example to show that Jest supports languages that
transpile to JS :)

~~~
skeeterbug
Curious - What are you using to transpile your ES6? Traceur?

~~~
jeffmo
Internally we use jstransform (github.com/facebook/jstransform) because it
allows us to plug in individual transforms more easily. It's also what React
uses for it's jsx transformations.

jstransform also ships with a few es6 features out of the box (classes, arrow
functions, etc -- and the number is growing!), but Traceur definitely has
more.

------
camus2
I think auto mocking modules is the most horrible trick a testing framework
can pull.

So because you're too lazy to write modules with a minimum of sane dependency
injection,you hack require (which is not dependency injection) to mock
dependencies?

This is actually the most awfull anti pattern i've seen in javascript land
yet.Unfortunatly, it's becoming the "way to go" for most developpers. Instead
of making your modules pluggable and decoupled,you negates everything testing
is for,you just dont write testable code.

~~~
ericclemmons
This isn't so bad. Before DI got popular in PHP via Symfony2, we used
Zend_Registry. "require" is pretty much the same thing in this context.

------
yazaddaruvala
Is it just me, or does having the background tinted red make you anxious too?

~~~
spicyj
I toned it down a little bit -- hope that helps. :)

~~~
saraid216
What it really needs is a GIF of Harley Quinn dancing in the background.

(This is a joke. Please don't do it.)

