Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nova abordagem para testes #82

Merged
merged 5 commits into from
Jul 24, 2021
Merged

Nova abordagem para testes #82

merged 5 commits into from
Jul 24, 2021

Conversation

filipedeschamps
Copy link
Owner

@filipedeschamps filipedeschamps commented Jul 23, 2021

Seguindo as sugestões do @huogerac nessa thread, eu tirei a responsabilidade de levantar e derrubar os containers de dentro dos testes e passei isso para a infraestrutura. Ficou muito melhor e muito mais rápido, principalmente quando você está desenvolvendo e testando ao mesmo tempo com npm run test:watch.

Então agora o foco dos testes locais não é garantir a integridade, e sim acelerar o desenvolvimento junto aos testes, porque quem deve fazer a garantia final é o CI (no nosso caso as Actions do Github). E ao rodar nas Actions, ao invés de usar o ambiente de desenvolvimento do Next.js, é feito o build e start, como em um ambiente de produção. Não é igual ao ambiente de produção, pois está rodando nos containers do Github, mas é um passo a mais do que só testar no ambiente de desenvolvimento.

Outra coisa que fiz é remover das Actions o push e deixar somente para rodar nos pull_requests. Isso deve acelerar um pouco as coisas também.

Bom, é um grande refactor, não sei o que vai acontecer aqui no Github Actions, vamos ver se vai passar 👍

[edit]

Passou 🎉

Então os comandos para desenvolver ou testar continuam os mesmos, e eles automaticamente se responsabilizam por levantar ou derrubar tudo o que precisa:

npm run dev
npm run test
npm run test:watch

@vercel
Copy link

vercel bot commented Jul 23, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/tabnews/tabnews/3MBN39SS6rzvaaB7ZoaEkFrJpFXg
✅ Preview: https://tabnews-git-tests-new-approach-tabnews.vercel.app

@filipedeschamps filipedeschamps changed the title [WIP] Nova abordagem para testes Nova abordagem para testes Jul 23, 2021
Copy link

@huogerac huogerac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Fiz uma revisão rápida (tenho mais tempo a noite)
  • Gostei das alterações do package json (scripts)
  • yarn test funciona, os tests passam, mas sai com exit 1 por algum motivo
  • a aplicação está funcionando
  • o act não da erro, mas não executa mais (voltei o rc para original), com a imagem que voce passou, mas ainda assim nada acontece, sem erro, sem output..(preciso investigar mais)
➜  tabnews.com.br git:(tests-new-approach) ✗ yarn test
yarn run v1.22.10
$ npm run services:up && concurrently -s -k -n next,jest 'npm run next' 'jest --runInBand' && npm run services:stop
npm WARN lifecycle The node binary used for scripts is /tmp/yarn--1627065061622-0.835042471651541/node but npm is using /home/roger/.nvm/versions/node/v12.22.1/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.

> tabnews.com.br@1.0.0 services:up /home/roger/workspace/workon/tabnews/tabnews.com.br
> docker-compose -f infra/docker-compose.development.yml up -d

postgres-dev is up-to-date
[next] npm WARN lifecycle The node binary used for scripts is /tmp/yarn--1627065061622-0.835042471651541/node but npm is using /home/roger/.nvm/versions/node/v12.22.1/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.
[next] 
[next] > tabnews.com.br@1.0.0 next /home/roger/workspace/workon/tabnews/tabnews.com.br
[next] > next dev
[next] 
[next] ready - started server on 0.0.0.0:3000, url: http://localhost:3000
[next] info  - Loaded env from /home/roger/workspace/workon/tabnews/tabnews.com.br/.env
[next] info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
[next] 
[next] warn - You have enabled the JIT engine which is currently in preview.
[next] warn - Preview features are not covered by semver, may introduce breaking changes, and can change at any time.
[next] event - compiled successfully
[next] event - build page: /api/v1/migrations
[next] wait  - compiling...
[next] event - compiled successfully
[next] No migrations to run!
[next] > Migrating files:
[next] > - 1599609889070_init
[next] ### MIGRATION 1599609889070_init (UP) ###
[next] > Migrating files:
[next] > - 1599609889070_init
[next] ### MIGRATION 1599609889070_init (UP) ###
[next] No migrations to run!
[next] No migrations to run!
[jest]  PASS  pages/api/v1/migrations/index.test.js
[jest]   [e2e] First GET to /api/v1/migrations
[jest]     ✓ should list all pending migrations (34 ms)
[jest]   [e2e] First POST to /api/v1/migrations
[jest]     ✓ should list all migrated migrations (24 ms)
[jest]   [e2e] Second POST to /api/v1/migrations
[jest]     ✓ should list zero migrated migrations (22 ms)
[jest]   [e2e] Second GET to /api/v1/migrations
[jest]     ✓ should list all zero pending (21 ms)
[jest]   [e2e] PUT to /api/v1/migrations
[jest]     ✓ should return 404 (4 ms)
[jest] 
[jest] Test Suites: 1 passed, 1 total
[jest] Tests:       5 passed, 5 total
[jest] Snapshots:   0 total
[jest] Time:        9.673 s
[jest] Ran all test suites.
[jest] jest --runInBand exited with code 0
--> Sending SIGTERM to other processes..
[next] npm run next exited with code SIGTERM
error Command failed with exit code 1.

@filipedeschamps
Copy link
Owner Author

filipedeschamps commented Jul 23, 2021

@huogerac show!!!!! Eu não to usando o yarn no projeto, sugiro usar o npm e testar com ele no ambiente local, veja se isso altera o resultado 🤝

[edit]

Veja meu output:

➜ tabnews.com.br git:(tests-new-approach) npm test

> tabnews.com.br@1.0.0 test
> npm run services:up && concurrently -s -k -n next,jest 'npm run next' 'jest --runInBand' && npm run services:stop


> tabnews.com.br@1.0.0 services:up
> docker-compose -f infra/docker-compose.development.yml up -d

Creating network "infra_default" with the default driver
Creating postgres-dev ... done
[next] 
[next] > tabnews.com.br@1.0.0 next
[next] > next dev
[next] 
[next] ready - started server on 0.0.0.0:3000, url: http://localhost:3000
[next] info  - Loaded env from /Users/filipedeschamps/local/tabnews.com.br/.env
[next] info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
[next] event - compiled successfully
[next] event - build page: /api/v1/migrations
[next] wait  - compiling...
[next] event - compiled successfully
[next] > Migrating files:
[next] > - 1599609889070_init
[next] ### MIGRATION 1599609889070_init (UP) ###
[next] > Migrating files:
[next] > - 1599609889070_init
[next] ### MIGRATION 1599609889070_init (UP) ###
[next] > Migrating files:
[next] > - 1599609889070_init
[next] ### MIGRATION 1599609889070_init (UP) ###
[next] No migrations to run!
[next] No migrations to run!
[jest]  PASS  pages/api/v1/migrations/index.test.js
[jest]   [e2e] First GET to /api/v1/migrations
[jest]     ✓ should list all pending migrations (32 ms)
[jest]   [e2e] First POST to /api/v1/migrations
[jest]     ✓ should list all migrated migrations (36 ms)
[jest]   [e2e] Second POST to /api/v1/migrations
[jest]     ✓ should list zero migrated migrations (32 ms)
[jest]   [e2e] Second GET to /api/v1/migrations
[jest]     ✓ should list all zero pending (38 ms)
[jest]   [e2e] PUT to /api/v1/migrations
[jest]     ✓ should return 404 (3 ms)
[jest] 
[jest] Test Suites: 1 passed, 1 total
[jest] Tests:       5 passed, 5 total
[jest] Snapshots:   0 total
[jest] Time:        0.913 s, estimated 1 s
[jest] Ran all test suites.
[jest] jest --runInBand exited with code 0
--> Sending SIGTERM to other processes..
[next] npm run next exited with code 0

> tabnews.com.br@1.0.0 services:stop
> docker-compose -f infra/docker-compose.development.yml stop

Stopping postgres-dev ... done

@filipedeschamps
Copy link
Owner Author

@huogerac descobri porque o seu act não retorna mais nada, porque eu setei a action para rodar somente como pull_request e se eu não me engano ele roda por padrão somente push.

Então para rodar o job de tests, execute esse comando:

act -j tests

But remove it if it's localhost, development or CI
Copy link

@huogerac huogerac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bom Filipe,
Não tenho conhecimento em nodejs e next para "aprovar", mas tudo que testei e vi, está funcionando. (tests, dev, services:up/down act, lint)

FYI:

  • act funcionou com o parâmetro: act -j tests! massa, cada vez gosto mais deste act
  • o npm run test ou yarn test não estavam funcionando, pq eu estava usando o node v12, fiz update para 14 e tambem não funcionou, dai ao usar a última versão (v16.5.0), tudo funcionou
  • onde podemos colocar no projeto a versão do node do projeto? (já vi alguns projetos com um .node_version e outros como uma entrada no package.json. Alguem sabe?
  • sabe qual node tem na vercel, dai talvez poderia ser interessante usar no projeto a mesma versão da vercel (já vi cada erro causado por versão diferente do node)
  • eu estava usando um docker-compose bem antigo (2019) e achava q o problema era com ele, mas no final não era nada relacionado com docker, mas de qualquer forma é bom ter o docker e docker-compose mais novos instalado!

[edit]
o curioso é depois vi que no tests.yml do gh actions, lá tem referencia para o node-version 14. Dai fiquei na duvida pq tive que utilizar o node 16 para funcionar...

@filipedeschamps
Copy link
Owner Author

Sensacional @huogerac 🎉👏

Sobre os seus pontos:

  1. Sim, o act pra mim foi a grande descoberta da Milestone 1. É simplesmente sensacional e to louco pra falar sobre isso na Live de Encerramento!
  2. Estranho, eu testei aqui na versão 12.x e funcionou. Será que é algum cache na sua máquina? Por exemplo, depois que você muda de versão é preciso reinstalar as dependências do projeto, então sugiro remover o node_modules por completo e instalar. Vi no seu edit que você viu que a versão do Node.js nas Actions é a 14.x 👍
  3. A versão usada na Vercel é sempre a LTS do Node.js, você pode escolher qual major version quer e que no nosso caso está casando com o CI, olha só:
    image
  4. Interessante! O que acha de agora com o docker atualizado, você voltar para a versão 14.x, remover os módulos, instalar e testar novamente?

@filipedeschamps filipedeschamps merged commit 5dde95f into main Jul 24, 2021
@filipedeschamps filipedeschamps deleted the tests-new-approach branch July 24, 2021 00:19
@filipedeschamps
Copy link
Owner Author

Merged! Let's gooooo!!!! 👍

@filipedeschamps
Copy link
Owner Author

Eu notei que pulei um dos seus pontos @huogerac

onde podemos colocar no projeto a versão do node do projeto? (já vi alguns projetos com um .node_version e outros como uma entrada no package.json. Alguem sabe?

Talvez usar a chave engines.node, pois inclusive vai sobrescrever o que é definido na interface e isso é ótimo:

image

https://vercel.com/docs/runtimes#official-runtimes/node-js/node-js-version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants