Code Monkey home page Code Monkey logo

tdd_node.js's Introduction

Applying TDD (Test-driven Development)

  • Jest
  • REST API
  • Login with JWT

Separei a lógica do servidor (aplicação) da parte de 'ouvir' porta para quando rodar os testes não precisar alocar nenhuma porta

Dentro de src/; app.js => lógica do servidor (aplicação) server.js => alocar portas routes.js => rotas da aplicação


Tests with Jest

https://jestjs.io/ Node.js unit tests, mocks, coverage, etc.

  1. install jest yarn add jest -D

  2. Jest Init yarn jest --init

  3. configure your jest.config.js & create your test in tests/integration or _tests/unit

  4. run your tests yarn test

NOTE: you should create your .env file. example;

DB_HOST=127.0.0.1
DB_USER=***
DB_PASS=***
DB_NAME=***

tests coverage

run yarn jest and you can see the test coverage of your code. You can see each file and line that need coverage accessing __tests__/coverage/lcov-report/index.html


Testing routes with Supertest

  1. install; yarn add supertest -D

Environment Variables in Windows

https://www.edureka.co/community/77202/how-can-i-set-node-env-production-on-windows


Working with Sequelize

Instaling sequelize & other libs

yarn add sequelize pg (I'm using PostgreSQL) yarn add sequelize-cli -D

Sequelize init

yarn sequelize init create files config/config.json & models/index.js & the emptys folders migrations/ and seeders/;

Create migrations

yarn sequelize migration:create --name=create-users

Create a Database in PostgreSQL

If you are using postgreSQL & pgAdmin; Create a new database in PgAdmin NOTE: you can change the username & password in src/config/database.js and the database name must be the same name

running migrations

yarn sequelize db:migrate create tables in the database if they don't exist & update database after create your table you can access in pgAdmin -> Databases -> 'your_database' -> Schemas -> public -> Tables


Libraries

Jest

https://jestjs.io/ Node.js unit tests, mocks, coverage, etc.


Sequelize

É um ORM que facilita a gente lidar com os dados e tabelas do nosso Banco de Dados, utilizando sintaxe de Javascript ao invés de SQL.

pg

lib para o sequelize lidar com o Banco de Dados PostgreSQL

sequelize-cli;

ajuda a lidar com a criação das migrations, que são os arquivos que vão controlar a versão das nossas tabelas do BD


Tutorial link

https://www.youtube.com/watch?v=2G_mWfG0DZE&list=PLmX5_ydv5jg-WRtaCb72IbYiuYvIacooR&index=5&t=1240s

tdd_node.js's People

Contributors

eldersam avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.