- All the examples we've seen so far
have been using JavaScript,
but some developers, many developers actually,
prefer to use TypeScript.
TypeScript gives you greater type assurance.
When you declare a variable in TypeScript,
you'd say something like let X colon number.
And the compiler would then check
that you've used it as a number consistently.
So, you have more faith that your code is bug free.
Another advantage of TypeScript is
that it has language features like interfaces
and enums to help you be rigorous
in your type specification.
Okay so, what about tests in TypeScript?
Jest lets you test TypeScript code,
quite straightforward actually,
but you have to give it some configuration
to help it translate TypeScript into JavaScript,
and to perform the test end.
So, we are going to see how to do that in this section.
So, have a quick look, and we'll dig
into the code in a moment, but here's a simple function.
It's an a file called example.ts
in the UsingTypeScript folder.
I've written a simple add function. It takes two numbers.
A and B must be numbers, and the return value
is also a number. Okay?
So, in my TypeScript code here,
I'm specifying the types of the inputs
and the type of the return value.
And that's fine.
You can also use TypeScript in your tests.
So, have a look at this test here.
I've declared a variable expected,
and I specified it's going to be a number.
Okay? So, that's TypeScript code as well.
That the expected result is going to be 30.
And then when I call the add function,
I've imported the add function from that module,
when I call the add function, the result should be 30.
So, I've used TypeScript here in my test as well.
Right. Now, if you wanna use TypeScript in jest,
that's fine, but you have to, first of all,
install some libraries for the TypeScript transpilation.
So, in the package.json file
there are some devDependencies that you need to add in.
I don't know if you come across Babel.
Babel is a very useful transpiler.
It converts TypeScript into JavaScript.
So, here I have the Babel preset for TypeScript,
so that it kind of knows
that it is going to be looking at TypeScript.
I've added some type information, jest.
If you run it in TypeScript,
jest has to have some type information
to keep the TypeScript compiler happy.
And then, Babel jest is the third dependency
that I need to add in. Okay?
So, you need to basically add these dependencies,
these devDependencies, into your package fluctuation file,
and then you need to run an npm install or yarn install
in your folder to actually install those libraries
into your directory.
So, I'm going to do that first.
Here's my demo folder. In this demo folder,
I've got my package.json file.
So, I'm going to do an npm install
to install the libraries specified by the package.json file.
It always takes a little while for that to finish.
So, what we'll do is we'll just leave that
carry on in the background whilst we carry on
our discussions.
Okay? So, assuming that's finished,
what you then have to do, there's one more configuration,
you have to have a file called babel.config.json,
and in there, you have to set a Babel preset.
Babel is a remarkable technology.
It has presets to kind of configure it
in different common usage scenarios.
The TypeScript preset means that Babel will now be capable
of doing translation from TypeScript into JavaScript.
Okay? So, we've got that file as well in the demo folder.
This is what the code folder looks like,
and so, we can have a look at this.
Now, let's have a look.
First of all, we had our example code example.ts.
I've actually got four functions,
an add function, subtract, multiply, and divide,
and they're all in TypeScript.
They all specify the types of the inputs,
and the type of the return value.
And then, in my test code,
in my test code, I've got two suites.
I've got a suite that tests the additive functions,
and a suite that tests the multiplicative functions.
So, in the first suite, I'm testing the add function
using TypeScript type in here.
And, I'm also testing the subtract function.
And then, guess what?
In the multiplicative suite,
I'm testing that the multiply function works,
and that the divide function works.
Just one point to make here.
When I exported those functions,
I exported them in an object basically,
with four functions, add, subtract, multiply, divide.
When I imported that object into my test, I gave it a name.
So, funcs is the exported object.
Funcs.add is the add function in that object.
Okay? So, that's how you can export multiple functions
as an object.
And then, you kind of prefix it
with that name there, like funcs.subtract,
and funcs.multiply, and funcs.divide.
Okay? My package.json file had those devDependencies
which I mentioned. Npm install has installed those now.
And then, I had my Babel configuration
that basically told Babel,
"Yes, you do have to do TypeScript transpilation, please."
Okay? So, with all those files in place,
we're ready to run the tests.
So, to run the tests, the usual arrangement is
just npm run test. The TypeScript transpiler
will automatically kick in to convert TypeScript
into JavaScript in order to run my tests.
Npm run test.
So, just waiting for it to get its act together.
I've got four functions all together spread over two suites.
I've got my additive suite, with add and subtract.
I've got my multiplicative suite, with multiply and divide,
and they've all worked successfully.
So, feel free to write TypeScript code if you want.
There's absolutely, you know, no reason not to.
Just remember, the additional packages you've got to install
and the additional configuration that you need for a Babel,
so that it knows how to transpile the TypeScript
into JavaScript when it runs the tests.