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

Playwright tests #1832

Draft
wants to merge 88 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
4a5e69d
infra: generated mocks, msw setup
fhenrich33 Dec 4, 2024
a45d34e
infra: remove graphql-codegen package and plugins, setup playwright, …
fhenrich33 Dec 6, 2024
56b04e2
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Dec 6, 2024
0787be2
fix: fragment usage
fhenrich33 Dec 6, 2024
8d15663
infra: update graphql-gen script, fixtures
fhenrich33 Dec 6, 2024
f5c7ba6
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Dec 6, 2024
0733d47
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Dec 6, 2024
eea367a
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Dec 8, 2024
94c2629
refactor: move msw handlers import to source
fhenrich33 Dec 8, 2024
27d131f
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Dec 10, 2024
cb1c566
chore: file line end
fhenrich33 Dec 10, 2024
043488d
refactor: remove old test setup
fhenrich33 Dec 10, 2024
5934253
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Dec 10, 2024
09a8add
refactor: remove old test setup
fhenrich33 Dec 10, 2024
29fe187
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Dec 11, 2024
9ccc9f1
feat: update fixture objects and msw responses with mutations
fhenrich33 Dec 12, 2024
c5c9f64
feat: update fixture objects and msw responses with mutations
fhenrich33 Dec 12, 2024
512b5d9
feat: update fixture objects and msw responses with mutations
fhenrich33 Dec 12, 2024
5442c6e
feat: update fixture objects and msw responses with mutations
fhenrich33 Dec 12, 2024
c50b23c
feat: update fixture objects and msw responses with mutations
fhenrich33 Dec 12, 2024
2a14328
test: persist some box mutations in msw
fhenrich33 Dec 12, 2024
f1994f9
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Dec 12, 2024
1572eac
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Dec 12, 2024
b6ca365
infra: use workspace prettier config
fhenrich33 Dec 13, 2024
7e169c1
feat: msw handlers (except shipments, transfer agreements, box history)
fhenrich33 Dec 13, 2024
a043bc7
feat: msw handlers for shipments and transfer agreements queries
fhenrich33 Dec 13, 2024
2838fe8
test: qrCode fake cam feed
fhenrich33 Dec 13, 2024
16ce8be
feat: msw handler last mutations wip
fhenrich33 Dec 15, 2024
fdb83f8
feat: msw handler last mutations wip
fhenrich33 Dec 15, 2024
77547ff
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Dec 18, 2024
7b9069a
fix: tag labels on box edit in msw
fhenrich33 Dec 18, 2024
7f127a8
chore: add todo
fhenrich33 Dec 18, 2024
91b18eb
infra: enable gpg commit signing for vscode
fhenrich33 Dec 18, 2024
feed8db
chore: update msw todo
fhenrich33 Dec 18, 2024
ac98d5c
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Dec 19, 2024
c6d9c3a
docs: playwright adr draft
fhenrich33 Dec 19, 2024
093a1b4
test: infer operation name from graphql operation
fhenrich33 Dec 19, 2024
51d2129
test: wip/draft script to auto generate handlers and mocks
fhenrich33 Dec 19, 2024
1024d3f
test: wip/draft script to auto generate handlers and mocks
fhenrich33 Dec 19, 2024
ff81336
chore: remove draft handle and data generator
fhenrich33 Dec 20, 2024
668d32d
infra: WIP playwright CI job
fhenrich33 Dec 20, 2024
d7cfe4a
infra: WIP playwright CI job
fhenrich33 Dec 20, 2024
3e75a5f
infra: WIP playwright CI job
fhenrich33 Dec 20, 2024
8294d31
infra: WIP playwright CI job
fhenrich33 Dec 20, 2024
cb87d5e
infra: WIP playwright CI job
fhenrich33 Dec 20, 2024
3e81ab4
infra: WIP playwright CI job
fhenrich33 Dec 20, 2024
a7d3312
infra: WIP playwright CI job
fhenrich33 Dec 20, 2024
4149319
infra: WIP playwright CI job
fhenrich33 Dec 20, 2024
69e2371
infra: WIP playwright CI job
fhenrich33 Dec 20, 2024
bfc8560
infra: WIP playwright CI job
fhenrich33 Dec 20, 2024
b3e0181
infra: WIP playwright CI job
fhenrich33 Dec 20, 2024
71858b0
infra: WIP playwright CI job
fhenrich33 Dec 20, 2024
c0b9905
infra: WIP playwright CI job
fhenrich33 Dec 20, 2024
5f2fa26
infra: WIP playwright CI job
fhenrich33 Dec 21, 2024
d58d5d5
infra: playwright CI job
fhenrich33 Dec 21, 2024
d1d8e72
test: qrcode tests
fhenrich33 Dec 21, 2024
363d2d0
test: give bigger timeout for qrcode tests
fhenrich33 Dec 21, 2024
31b9f96
infra: add generated state.json to gitignore
fhenrich33 Dec 21, 2024
a0b6cfc
test: simplify CI test command, WIP fix qrcode in CI
fhenrich33 Dec 21, 2024
a744e21
infra: fix playwright server command
fhenrich33 Dec 21, 2024
510d713
infra: wip run playwright with node-executor
fhenrich33 Dec 21, 2024
30278d8
infra: wip run playwright with node-executor
fhenrich33 Dec 21, 2024
1a2c185
test: WIP fix qrcode in CI
fhenrich33 Dec 21, 2024
51dcf41
test: WIP fix qrcode in CI
fhenrich33 Dec 21, 2024
b30410e
infra: run playwright with node-executor
fhenrich33 Dec 21, 2024
e132d3d
test: WIP fix qrcode in CI
fhenrich33 Dec 21, 2024
516fcfd
test: local only tests for specs that don't work in CI
fhenrich33 Dec 21, 2024
f327be8
chore: remove unused if
fhenrich33 Dec 21, 2024
8dedd7f
chore: mutation TODOs
fhenrich33 Dec 23, 2024
e042935
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Dec 23, 2024
55d8b0d
docs: update testing section for the FE README
fhenrich33 Dec 24, 2024
9610e32
docs: update ADR
fhenrich33 Dec 24, 2024
3fc1483
docs: update ADR
fhenrich33 Dec 24, 2024
ca9bdd6
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Dec 24, 2024
b07441c
docs: update FE README
fhenrich33 Dec 24, 2024
11beaaa
Merge branch 'master' into playwright-tests
fhenrich33 Dec 27, 2024
cecad45
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Jan 2, 2025
e87ce1e
docs: add and fix missing context for ADR and FE README
fhenrich33 Jan 4, 2025
58298c1
Merge branch 'master' into playwright-tests
fhenrich33 Jan 4, 2025
18e7b75
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Jan 15, 2025
dd15fb4
Merge branch 'master' of https://github.com/boxwise/boxtribute into p…
fhenrich33 Jan 23, 2025
a11c89c
chore: remove unused stories files
fhenrich33 Jan 24, 2025
f895937
chore: remove unused files from previous test setup
fhenrich33 Jan 24, 2025
7502a3e
infra: remove unused packages
fhenrich33 Jan 24, 2025
d09ca08
fix: wrong font path
fhenrich33 Jan 31, 2025
c6dc12b
infra: wip multi auth setup, remove previous config, global load prop
fhenrich33 Jan 31, 2025
a76bde5
test: mock qr reader setup
fhenrich33 Jan 31, 2025
24544b2
test: adjust and add more specs
fhenrich33 Jan 31, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,37 +138,47 @@ jobs:
paths:
- front/dist

lint-front:
executor: node-executor
working_directory: ~/
resource_class: medium+
steps:
- attach_workspace:
at: ~/
- run:
name: Lint and type-check front app
command: |
pnpm check-types

test-front:
executor: node-executor
working_directory: ~/front
parallelism: 4
working_directory: ~/
resource_class: medium+
steps:
- attach_workspace:
at: ~/
- run:
name: Test front app and create reports
name: Install Playwright deps
command: |
DEBUG_PRINT_LIMIT=10000 pnpm test:coverage --test-timeout=40000
npx playwright install --with-deps chromium
- run:
name: Test shared-components and create reports
name: Test front app and create reports
environment:
FRONT_AUTH0_DOMAIN: boxtribute-dev.eu.auth0.com
FRONT_AUTH0_CLIENT_ID: ni9ZdcoIv3HU10kyc4t1qxOMxjVyxcbS
FRONT_USE_MSW: true
command: |
DEBUG_PRINT_LIMIT=10000 pnpm -C ../shared-components test:coverage --test-timeout=40000
- store_test_results:
path: coverage/junit.xml
- store_test_results:
path: ../shared-components/coverage/junit.xml
- store_artifacts:
path: coverage
destination: front-coverage
- store_artifacts:
path: ../shared-components/coverage
destination: shared-components-coverage
- codecov/upload:
flags: frontend
file: coverage/coverage-final.json
- codecov/upload:
flags: sharedComponents
file: ../shared-components/coverage/coverage-final.json
SHARD="$((${CIRCLE_NODE_INDEX}+1))"; ls tests && pnpm test -- --shard=${SHARD}/${CIRCLE_NODE_TOTAL}
# TODO: add coverage, report, record video on fail
# - store_test_results:
# path: coverage/junit.xml
# - store_artifacts:
# path: coverage
# destination: front-coverage
# - codecov/upload:
# flags: frontend
# file: coverage/coverage-final.json
- slack/notify-on-failure:
only_for_branches: master,production

Expand Down Expand Up @@ -440,6 +450,10 @@ workflows:
context: STAGING
requires:
- install-node-packages
- lint-front:
context: STAGING
requires:
- install-node-packages
- test-front:
context: STAGING
requires:
Expand Down
12 changes: 1 addition & 11 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,5 @@ module.exports = {
tsx: "never",
},
],
},
overrides: [
{
files: [
"**/?(__)tests?(__)/**/*.[jt]s?(x)",
"**/?(*.)+(spec|test).[jt]s?(x)",
"**/mocks/**/*.[jt]s?(x)",
],
extends: ["plugin:testing-library/react"],
},
],
}
};
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ docs/graphql-api/public
.VSCodeCounter
*.old.*
__pycache__/*
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
state.json
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"editor.formatOnPaste": false, // required
"editor.formatOnType": false, // required
"editor.formatOnSave": true,
"prettier.configPath": ".prettierrc",

// eslint
"eslint.workingDirectories": ["./", "./front", "./shared-components", "./statviz"],
Expand All @@ -29,5 +30,6 @@
},

"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
"typescript.enablePromptUseWorkspaceTsdk": true,
"git.enableCommitSigning": true
}
2 changes: 1 addition & 1 deletion back/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ to simulate a god user with ID 8 (for a regular user, set something like `id=1,
> [!IMPORTANT]
> To keep the front-end side up-to-date with the GraphQL schema, make sure that the pre-commit command for `*.graphql` files (`id: generate-graphql-ts-types`) is running properly.
>
> It should generate both `schema.graphql` (the introspected unified schema) and `graphql-env.d.ts` (the generated types to be ìnferred and consumed in the FE with `gql.tada`) inside `/graphql/generated/`.
> It should generate `schema.graphql` (the introspected unified schema), `graphql-env.d.ts` (the generated types to be ìnferred and consumed in the FE with `gql.tada`), `types.ts` (base generated types for msw handlers and mocks) and lastly `mocks.ts` (base mocks to make fixtures and use in tests) inside `/graphql/generated/`.

## Project structure

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ services:
NODE_ENV: development
HUSKY: 0
FRONT_ENVIRONMENT: ${ENVIRONMENT:-development}
FRONT_USE_MSW: ${USE_MSW}
FRONT_SENTRY_ENVIRONMENT: ${ENVIRONMENT:-development}
FRONT_SENTRY_FE_DSN: ${SENTRY_FE_DSN:-}
FRONT_SENTRY_TRACES_SAMPLE_RATE: ${SENTRY_TRACES_SAMPLE_RATE:-}
Expand Down
79 changes: 79 additions & 0 deletions docs/adr/adr_frontend_tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# ADR: Use Playwright for Application, Integration and Component Testing

Author: [Felipe](https://github.com/fhenrich33)

Reviewed by: [Roanna K](https://github.com/aerinsol)

## Status

Proposed, Implementing

## Context

The current testing setup for the Boxtribute front-end project uses React Testing Library and Vitest for unit and integration tests. While these tools are effective for testing individual components and their interactions, the tests usually aren't fully 1-1 representative of what the user will experience. E2E and more involved integration testing are crucial for ensuring that the application works correctly from the user's perspective, covering the entire workflow from start to finish.

Additionally, we use Mock Service Worker (MSW) to mock API requests during tests (and sometimes development) to avoid hitting the real development server and database during tests. This allows us to test the application in isolation and ensure consistent and fast test results.

## Decision Drivers

1. **Comprehensive Testing**: We need a testing framework that can simulate real user interactions, test the application as a whole, and handle integration tests with mocked API requests.
2. **Cross-Browser Testing**: The ability to test the application across different browsers to ensure compatibility.
3. **Ease of Use**: The testing framework should be easy to set up and use, with good documentation and community support.
4. **Performance**: The framework should be fast and efficient, minimizing the impact on the development workflow.
5. **Integration with CI/CD**: The framework should integrate well with our existing CI/CD pipeline.

## Considered Options

1. **React Testing Library + Vitest**

- Pros:
- Well-suited for unit and integration tests.
- Good community support and documentation.
- Easy to set up and use for isolated testing.
- Cons:
- Limited to testing individual components and their interactions.
- Does not provide comprehensive application testing capabilities (in comparison to Playwright).
- Runs against a mocked DOM structure (jsdom) instead of a real browser, which leads to flakiness and unreliability.
- No built-in support for cross-browser testing.

2. **Playwright**
- Pros:
Copy link
Member

Choose a reason for hiding this comment

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

I thought the main Pro argument is that you can easily generate mock data @fhenrich33 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would be the benefit of gql.tada, used with MSW request handlers. Playwright does have a very similar mocking capability as MSW, but we use the latter to allow us to mock yet-to-be-implemented schemas in the future.

- Comprehensive application, integration, and component testing capabilities.
- Tests against the real DOM, as even in headless mode the tests run inside a real browser.
- Supports cross-browser testing (Chromium, Firefox, WebKit).
- Easy to set up and use with excellent documentation. The API for test specs is very similar to React Testing Library.
- Ability to generate tests by using the app. See https://playwright.dev/docs/codegen-intro.
- Can still run isolated component tests. See https://playwright.dev/docs/test-components.
- Can run visual diffing. See https://playwright.dev/docs/test-snapshots.
- Fast and efficient, with parallel test execution. Might not match React Testing Library speed but gets close while testing in a real environment.
- Integrates well with CI/CD pipelines in headless mode.
- Cons:
- Additional learning curve for advanced use cases.
- Extra setup may be required for advanced use cases.
- Slightly slower than React Testing Library for sequential test runs (alleviated by parallel runs).

## Decision

We have decided to adopt Playwright for application, integration, and component testing in the Boxtribute front-end project. Playwright provides comprehensive testing capabilities, supports cross-browser testing, and integrates well with our existing CI/CD pipeline. Additionally, Playwright can be used with Mock Service Worker (MSW) to mock API requests, allowing us to test the application in isolation, while faithfully mimicking the real server since the mocks infer the data types from the GraphQL Schema. Vitest will still be used for individual TypeScript functions and modules.

## Consequences

- **Positive**:

- Improved test coverage with comprehensive application, integration, and component tests.
- Ability to test the application across different browsers.
- Fast testing with parallel test execution, while being more faithful to how a user will experience the app.
- Better integration with our CI/CD pipeline. Less prone to flakiness.
- Consistent test results by mocking API requests with MSW based on our GraphQL Schema.

- **Negative**:
- Additional learning curve for developers.
- Potential need for additional setup for advanced use cases.
- Heavier Development and CI/CD setup, as we install real browsers as dependencies.

## References

- [Playwright Documentation](https://playwright.dev/docs/intro)
- [React Testing Library Documentation](https://testing-library.com/docs/react-testing-library/intro)
- [Vitest Documentation](https://vitest.dev/)
- [Mock Service Worker Documentation](https://mswjs.io/docs/)
2 changes: 2 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ TEST_AUTH0_MANAGEMENT_API_CLIENT_ID=ZBDcEypTHMwn23ScgeaMqwzMxO5epguH
TEST_AUTH0_MANAGEMENT_API_CLIENT_SECRET=

ENVIRONMENT=development

USE_MSW=false
Loading