Hi, Devs!
Let's configure tests with mocha, chai and sinon on Typescript in a few steps.
Lets' start the project:
npm init -y
Now, let's install the packages. Here we also install the types (@types) of each library because we need to use them on Typescript. Usually we need to test some elements that returns one promise, so we need do install chai-as-promised.
npm i -D mocha @types/mocha chai @types/chai
npm i -D sinon @types/sinon
npm i -D chai-as-promised @types/chai-as-promised
We need to use ts-node library to run our tests, because using Typescript we have to transpile the code. So let's install typescript and ts-node.
npm i -D typescript ts-node
Now put on your package.json the necessary script to run the tests:
"scripts": {
...
"test": "mocha --require ts-node/register tests/**/*.spec.ts --exit"
}
Create one folder with the name tests, into a file with testfirst.spec.ts name and create a simple case:
import { describe } from "mocha";
import { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);
const expect = chai.expect;
describe('Testing the configuration', () => {
it('should pass', () => {
});
});
After you could run:
npm run test
That' all!
Contacts
Email: luizcalaca@gmail.com
Instagram: https://www.instagram.com/luizcalaca
Linkedin: https://www.linkedin.com/in/luizcalaca/
Twitter: https://twitter.com/luizcalaca
Top comments (2)
top man! ;) Parabens!
Funciona no yarm também ?
Funciona, sim!