Test Driven Development (TDD)

and Continuous Integration

Teste unitário no front-end

- Comece pelo teste

- Faça falhar

- Refatore

Exemplos

import { expect } from 'chai'
import { request } from 'supertest'

const mainUrl = 'https://painel-wifi.com/'

describe('Painel Admin Wi-fi', () => {
  it('Should /json returns sucess 200', (done) => {
    request(mainUrl)
      .get('/json')
      .expect(200)
      .end(function(err){
        done(err)
      })
  })
})

app.spec.js

Crie scripts no seu package.json para

fazer build e rodar testes localmente

"scripts": {
    "ng": "./node_modules/.bin/ng",
    "pm2": "./node_modules/.bin/pm2",
    "mocha": "./node_modules/.bin/mocha specs/**/*.spec.js",
    "serve": "npm run ng serve",
    "build": "rm -rf dist && npm run ng build",
    "test": "npm run mocha",
    "local:test": "npm run test --env=local",
    "local:start": "npm run pm2 --env=local start app.js --update-env",
    "local:stop": "npm run pm2 delete app.js",
    "prepush": "npm run build && npm run local:start
        && npm run local:test && npm run local:stop"
  }

package.json

import { expect } from 'chai'
import { API_URL } from '../source/config'

describe('Smoke test', () => {
  it('Should create an instace of myFunction', (done) => {
    const myVar = new myFunction({});
    expect(myVar).to.be.an.instanceOf(myFunction)
  })
})

app.spec.js

Exemplo de teste passando =)

Continuous Integration - Jenkins

FROM jenkins

USER root

RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - \
    && apt-get install -y nodejs

RUN npm install -g npm

USER jenkins

Dockerfile

Instalação e Configuração

Criando imagem Jenkins com o Docker

Instalação e Configuração

Criando imagem Jenkins com o Docker

docker image build -t myDenkinsImage:1.0.0 .

docker container run -p 8080:8080 myDenkinsImage:1.0.0

Terminal

https://localhost:8080

https://localhost:8080

https://localhost:8080

Jenkinsfile

Pipeline Jenkins

pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        deleteDir()
        checkout scm
      }
    }
    stage('Build') {
      steps {
        script {
          sh 'npm install'
          commitChanges()
        }
      }
    }
    stage('Unit Test') {
      steps {
        script {
          sh 'npm test'
        }
      }
    }
  }
}

- Instala as dependencias

- Executa o teste

Pipeline Jenkins

Isso é tudo pessoal!

Test Driven Development

By Bruno Macedo

Test Driven Development

  • 408