What is Babel's purpose in relation to Jest? - javascript

I have an API project where I've been successfully using Jest (v26.x because code coverage doesn't work with v27.x on Windows 10 for me) for unit testing. I've run into a situation where I'm trying to achieve sufficient code coverage on a module I'm testing, but not all functions in that module have been exported, so this is impeding my ability to achieve this.
I came across the NPM package rewire, which worked beautifully, as it allowed me to mock any functions I wanted. The only problem is that code coverage doesn't acknowledge the lines tested via rewire and the devs in the Jest GitHub said they don't plan to support this.
So then I saw a couple of posts that people were successfully using babel-plugin-rewire to achieve the same thing as rewire, but that code coverage lines were successfully being acknowledged with this package. However, I cannot get this setup to work.
I think I understand the basics of Babel, as it pertains to browsers. It will take newer JS code and transpile/polyfill so that older JS code is generated that will work with older browsers of your choice.
But then I started seeing stuff about babel-jest and Babel being integrated into Jest and so on. Aside from the fact that I can't get babel-plugin-rewire to work, I feel like I don't even understand the basics of how Babel and Jest work together, or why Jest needs Babel.
What is the relationship between Babel and Jest?

Related

Jasmine/Jest type conflicts

I am relatively new here and would need some help.
Company I work for uses karma and jasmine for unit testing.
Now they would like to migrate some tests to jest.
I got a ticket assigned to me. We are using typescript btw...
I got jest installed, prefixed with .spec.jest.ts to separate jest test from karmas. And it works fine. Jest just picks up those files that are prefixed. But when i try to do some build things we do with our project I get the type declaration conflicts between jest and jasmine.
For example: node_modules/#types/jest/index.d.ts:32:1 - error TS6200: Definitions of the following identifiers conflict with those in another file: beforeAll, beforeEach, afterAll, afterEach, describe, fdescribe, xdescribe, it, fit, xit, expect, clock, DEFAULT_TIMEOUT_INTERVAL, CustomMatcherFact
ory, CustomEqualityTester
I am aware that jest is build on top of jasmine but is there some kind of workaround, we want to migrate our tests gradually. One at a time.
Is there some kind of workaround to namespace the types for jest or something like that so no conflicts occur?
Tnx in advance for your help :)
I tried to google some workaround but nothing that would solve the issue came across.
We excluded the karma types in the tsconfig and added jest types. The IDE now doesn't pick the karma types but it works for us since most of our tests are now in jest and very few left in karma and build process works now correctly since there are no more type conflicts.

How to use Intern with TypeScript and webpack?

Is it possible at all?
When I try to import Intern like this in my tests:
import * as registerSuite from 'intern!object'
and then I run webpack to transpile my *.ts tests, I get the following error:
TS2307: Cannot find module 'intern!object'.
Should I add some typings? I'm new to TypeScript, and webpack, and Intern — cannot understand what I'm missing.
Yes you need to add a reference to intern typings in include section of your tsconfig.json, as described in this tutorial.
But this will only get you past the compilation step. At runtime, you need a loader that understands module references like intern!object and handles them exactly in the way intern is expecting them to work.
I don't know if any such loader or plugin exists for webpack, I can only find one issue about it in the intern repo, and it does not seem to have any resolution.
In general, Intern 3 relies on dojo loader which is bundled with intern. People who try to use other loaders are on their own, and no common solution has emerged.
For intern 4 (which is a major update and not released yet), there is a plan to 'remove loader dependency from Intern', but I don't know if something specific will be done (or even needs to be done) for supporting webpack.
Actually, this is possible, as Dylan Schiemann said — the CEO at SitePen, the company that made Intern (if I'm not mistaken): https://www.sitepen.com/blog/2015/03/24/testing-typescript-with-intern/#comment-3280004472
The proof of this concept is the Dojo 2 project, which is also made by SitePen: https://github.com/dojo/widget-core/
It is authored in TypeScript, and uses webpack and Intern for testing.
But unfortunately there's no documentation tutorial on how to do this yet.
Dylan also said, in the next version of Intern (v4) the internal Intern's loader will be removed, and thus, this problem will go away, but Intern 4 is currently in pre-beta state yet, though relatively stable.

How to know if a package from Node Package Management (NPM) is tested

I am relatively new to Node.js and NPM, and I have a kind of naive question. I would like to know if there is a way to know if package published on NPM is tested, and if there is away could we automate that process, and if there is tool or framwork that tell me this packages is tested. Also, does NPM require developers to provide a test for their packages.
Thank you
NPM is just a package manager. As they say in their site,
It's a way to reuse code from other developers, and also a way to
share your code with them, and it makes it easy to manage the
different versions of code.
NPM does not require developers to provide a test for their packages.
Best to use a package that has more stars and downloads cos that vouch for the package.
P.S: Never forget that a developer can pull his/her code from npm anytime :)
There is no way to know absolutely for sure, but usually a good indicator is if the author/maintainer has a test script set in the module's package.json. npm does not require modules to have tests.
NPM doesn't require package developers to write tests for their code.
To understand if a specific package is tested, the best you can do is browse the source code of the package: does it have tests? Just unit tests or other types like integration tests and the like? Are these tests ready to run with straightforward commands? Do these tests offer good code coverage of the package? Do they actually test relevant cases?
To automate a process that tells you if a package has been tested, this process will have to make multiple checks within the source code of the package, as there are multiple conventions on how to write, name and structure tests within a Node.js codebase (not to mention the amount of available testing frameworks that can be used). My concern with this approach is how complicated (if possible) would it be to automatically determine if a package is well tested, without actually having a human look at the tests.

How Atom Editor Browser run ES6 Jasmine Tests

I'm using Atom editor, and it runs javascript test with Jasmine for ES6, with functionalities that are not read by any browser so far.
I'm trying to find out how it is done. I need to run the tests outside Atom. I will read Atom source code in order to find out, but maybe some one already know that?
Another alternative I was thinking was to use Babel to translate to ES5 and then run the tests, but Atom way seems better.
Any ideas?

How can i create JavaScript test cases

I am currently working on some test cases for my website. I've managed to test the back-end functionality using the simple-test framework.
I have some important javascript tools that requires some automatic testing. I know javascript is a client side language and it requires a browser, i am just wondering if its something i can do.
Notice the test must run from Linux CLI.
Thank you
You can install node.js with jasmine-node package to run your tests using cli:
Here's a brief intro to Jasmine:
http://net.tutsplus.com/tutorials/javascript-ajax/testing-your-javascript-with-jasmine/
And here's a tutorial on jasmine-node:
http://www.2ality.com/2011/10/jasmine.html
PS
In order to reference the code to be tested is useful to grasp how module.export works on Node.js ... here you'll find all the info you need to get started: http://jherdman.github.com/2010-04-05/understanding-nodejs-require.html (broken link) http://coppieters.blogspot.be/2013/03/tutorial-explaining-module-export.html
Jasmine is what I use for my Javascript testing. It can be found here :
https://github.com/jasmine/jasmine (UPDATED -- 2015)
I also use JSCoverage to test my code coverage. It can be found here : http://siliconforks.com/jscoverage/
There are several unit testing frameworks out there written for javascript.
You could try something like:
http://testcase.rubyforge.org/

Categories