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

Chore/testing infrastructure #78

Merged
merged 4 commits into from
Oct 21, 2021

Conversation

cjpillsbury
Copy link
Collaborator

This PR adds the basic infrastructure for unit/integration testing of web components/custom elements as well as JS modules, including:

  • basic dependencies and scripts for running and writing unit+integration tests
  • a non-exhaustive example of writing tests for a web component/custom element (media-play-button) for a POC/reference usage
  • a non-exhaustive example of testing an ESM util module (captions) for a POC/reference usage.

NOTES:

  • This PR is not meant to be exhaustive, but rather to provide the baseline infrastructure so other efforts may now include tests. This will also allow for iterations on our testing infrastructure, including baseline example(s) of integration-level tests, multi-browser testing, using with the CI, and git hooks integration.
  • For the test runner, we are using Web Test Runner, the recommended test runner for web components and @open-wc/testing for web component/custom element-specific testing utils (e.g. fixtures), with all other test dependencies (chai+plugins for assertions, sinon for mocks/spies/stubs) following from these.
    • Ideally, we would be able to use jest plus testing-library for better assertion & querying semantics, especially for the DOM & web components/custom elements. However, jest currently has some node/JSDOM assumptions that require some hack solutions to work with Web Testing Library, and testing-library currently has no plans for shadowDOM support. If we prefer these semantics, we can iterate to come up with a viable solution.

…sage for unit testing media chrome custom elements. Update gitignore to ignore coverage reports
@vercel
Copy link

vercel bot commented Oct 20, 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/mux/media-chrome/31fsaJJtgsmAwSiQRXx36EWuJ7de
✅ Preview: https://media-chrome-git-fork-cjpillsbury-chore-testing-infr-747958-mux.vercel.app

Copy link
Collaborator

@heff heff left a comment

Choose a reason for hiding this comment

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

Cool stuff. I approve, but someone with a better understanding of modern testing should probably also weigh in.

@@ -0,0 +1,145 @@
import { spy } from 'sinon';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why all the underscores in __tests__?

Copy link
Collaborator

Choose a reason for hiding this comment

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

__tests__ is a default for Jest, pretty common: jestjs/jest#637

describe('<media-play-button/>', () => {
it('passes the a11y audit', async () => {
const el = await fixture(`<media-play-button></media-play-button>`);
await expect(el).shadowDom.to.be.accessible();
Copy link
Collaborator

Choose a reason for hiding this comment

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

wtf, you can just do to.be.accessible()? Wow.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah. it's not perfect, but it's a good audit to have. It's built on axe core (https://www.npmjs.com/package/chai-a11y-axe)

@cjpillsbury
Copy link
Collaborator Author

Hack example of Testing Library with shadow dom/custom elements support. Adding for documentation/reference in case we decide to revisit: https://github.com/MatthiasKainer/dom-testing-tools-web-components-todo-list

Copy link
Collaborator

@dylanjha dylanjha left a comment

Choose a reason for hiding this comment

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

Some small suggestions, but overall really happy with this 🙌🏼

I generally default to Jest, and I see you tried that first, but we're hamstrung by your comment in the notes, so this all looks like the right setup.

If Jest does play nicer with web components world in the future, then it would be nice to transition at some point down the road, only because Jest + testing-library feels like the more "modern" setup. Practically speaking though, this all feels like a great way to have unit test coverage.

@@ -0,0 +1,145 @@
import { spy } from 'sinon';
Copy link
Collaborator

Choose a reason for hiding this comment

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

__tests__ is a default for Jest, pretty common: jestjs/jest#637

/** @TODO Implement me (CJP) */
expect(false).to.be.true;
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe just remove the ones that are skipped rn? I generally feel like having @TODO littered around source + test files is an anti-pattern because they're not very visible and often times never get done.

If it's truly a TODO -- file an issue, if not, then just remove the clutter.

That's my opinion, anyway.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm cool with removing the @TODOs but I'd recommend we leave the skips, as that's a common practice for tests for visibility and "red"/"green" testing (whether or not we're doing TDD/BDD).

{ label: 'English', kind: 'subtitles', language: 'en-US' },
]);
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Having these tests makes understanding the source wayyyy easier, and now I'd feel much more comfortable updating/refactoring any of that source code 👍🏼

@@ -13,7 +13,7 @@
"build": "snowpack build --config snowpack.prod.config.js",
"dev": "snowpack dev --config snowpack.dev.config.js",
"start": "yarn dev",
"test": "echo \"Error: no test specified\" && exit 1",
"test": "web-test-runner --coverage",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a .github/workflows/ci.yml file with something like

name: CI

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - run: yarn install --frozen-lockfile
      - run: yarn test
      - run: yarn build

Something like that -- goal would just be to make sure tests pass and the build succeeds on every push

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd recommend we only require passing tests for pull_requests to avoid adding friction with pairing/sharing scenarios (or just "save your work" best practices).

Copy link
Collaborator

Choose a reason for hiding this comment

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

sounds good 👍🏼

@cjpillsbury
Copy link
Collaborator Author

If Jest does play nicer with web components world in the future, then it would be nice to transition at some point down the road, only because Jest + testing-library feels like the more "modern" setup. Practically speaking though, this all feels like a great way to have unit test coverage.

💯 agree.

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

3 participants