Ahoy, mate!
This topic may be useful for test automation engineers who just started to learn TypeScript. It also extends my previous post:
TypeScript is a JavaScript superset in Node.js, which brings static types and other useful features to your project.
Under the hood, it transforms the code you written to the one that your platform understands (ES5 standard).
Source code of project from this article on GitHub: https://github.com/bormando/mochapi/tree/future
Setup
You may clone a GitHub repo with few example tests from here: https://github.com/bormando/mochapi/tree/main
After cloning, you must execute this command to download all required dependencies:
npm i
To check, if cloned project is working, you may run:
npm run test
It should display something like this in the end:
Install TypeScript dependencies
These are standard TypeScript dependencies that we'll need to download:
typescript
-
ts-node
(to transform code from TypeScript to EcmaScript 5 standard on run)
Also, keep in mind that some Node.js packages like mocha
& chai
doesn't come with TypeScript, so you'll have to install them separately as well:
@types/mocha
@types/chai
To install them all, you'll need to execute in terminal:
npm i -D typescript ts-node @types/mocha @types/chai
TypeScript configuration
Now, as we've finished dependencies installation, we need to configure TypeScript.
Simply execute npx tsc --init
in your project root which will create tsconfig.json
.
I recommend these options for new projects:
Don't forget to save changes.
ES5 vs TypeScript
You may read about base differences between ES5 and TypeScript here:
TypeScript vs. JavaScript. Why was TypeScript developed when we… | by Avelon Pang | Geek Culture | Medium
Avelon Pang ・ ・
Medium
In our current project, all you'll have to do is:
- Rename
markets.test.js
tomarkets.test.ts
. - Change imports in
markets.test.ts
file...
From this:
const axios = require('axios');
const assert = require('chai').assert;
To this:
import axios from 'axios';
import { assert } from 'chai';
These are the small changes but this is all we need to make sure that our TypeScript setup works fine (since this is the point of current article).
Reconfigure shortcut
Shortcut for running Mocha tests in ES5 looks like this:
"test": "npx mocha src/specs"
You may find it in scripts
section inside of package.json
file.
To convert code TypeScript code to JavaScript on-run, you need to change this shortcut on something like this:
"test": "npx mocha --require ts-node/register src/specs/**/*.test.ts"
ts-node/register
works as a converter in this case.
Also notice src/specs/**/*.test.ts
in the end - now we have to specify test files pattern, as Mocha looks for .js
files by default.
Running tests
We're good to go by now, so all you have to do is execute in terminal:
npm test
or
npm run test
And then you must see report in your terminal output:
Thanks for reading, hope you've learned something new.
Top comments (2)
Glad to help!
Type declaration isn't mandatory - it'll automatically be
any
.If I'm correct, this behavior is configurable in
tsconfig.json
, so it's not necessary to do that in the project presented in the article.