Skip to content

js: Add Jest testing#801

Merged
ix5 merged 5 commits intoisso-comments:masterfrom
ix5:js-jest-puppeteer
Mar 21, 2022
Merged

js: Add Jest testing#801
ix5 merged 5 commits intoisso-comments:masterfrom
ix5:js-jest-puppeteer

Conversation

@ix5
Copy link
Copy Markdown
Member

@ix5 ix5 commented Feb 19, 2022

As discussed in #754, add some basic Jest testing config and examples to facilitate finally writing some frontend code tests.


Note: End-to-end tests using puppeteer were split off and moved into #807


Terminology

  • Jest is a Javascript testing framework. It is mainly used here for unit tests.

Running

Install the needed packages:

npm install

Then run:

  • npm run test-unit for Jest unit tests

Highly WIP at the moment, but could be useful to someone else working on the same issues. Please communicate and collaborate!

Depends on #800. I have temporarily changed the base to that branch to make it easier to review this change.

@ix5
Copy link
Copy Markdown
Member Author

ix5 commented Feb 19, 2022

Also pinging @stefangehn, @blatinier and @Lucas-C here. Let me know if you find this useful and please share tips/improvements/general ideas.

I'd love for someone more knowledgeable than me to write some proper tests and I hope this is a good starting-off point.

A way to simply have some docker containers (one for Isso, one for JS e2e puppeteer tests) interconnected and running them with a simple command would be much appreciated!

@ix5 ix5 added client (Javascript) client code and CSS feature labels Feb 19, 2022
@ix5 ix5 added this to the 0.13 milestone Feb 19, 2022
@ix5 ix5 changed the base branch from master to js-webpack February 19, 2022 16:59
Copy link
Copy Markdown
Contributor

@Lucas-C Lucas-C left a comment

Choose a reason for hiding this comment

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

Nice bootstrapping! Good job @ix5 :)
Can I commit stuff on this branch if I find the time to write some tests?

Comment thread isso/js/jest-puppeteer.config.js Outdated
Comment thread isso/js/tests/integration/puppet.test.js Outdated
Comment thread isso/js/tests/mocks/fileMock.js
Comment thread isso/js/tests/unit/utils.test.js Outdated
Comment thread package.json
"build-prod": "webpack --config isso/js/webpack.config.js --merge --config-name dev --config-name prod"
},
"devDependencies": {
"jest": "^27.5.0",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"jest-puppeteer" is missing ;)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Adding jest-puppeteer to devDependencies will automatically pull in a 400Mb+ download of headless chrome, as explained earlier. That's why I'm a bit hesitant to add it here.

Imo it should be up to the user whether they need to run the end-to-end tests.

Maybe there's an option to configure the package before installing to not pull in the chrome download automatically?

Comment thread isso/js/jest-puppeteer.config.js Outdated
@@ -0,0 +1,6 @@
module.exports = {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This file should be at the root of the repository.
It is currently ignored by Jest when running npm test

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'd like to keep all javascript-related stuff inside isso/js/, so I'll have to figure out a way for Jest to read this file.
See https://github.com/smooth-code/jest-puppeteer#configure-puppeteer

(remains TODO)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Point of reference: argos-ci/jest-puppeteer#160 (comment)

process.env.JEST_PUPPETEER_CONFIG = require.resolve('./jest-puppeteer.config.js');

Comment thread isso/js/tests/unit/template.test.js Outdated
@ix5
Copy link
Copy Markdown
Member Author

ix5 commented Mar 3, 2022

Hey @Lucas-C, thank you so much for reviewing and verifying!

I probably should have split this PR into two PRs, one for Jest and one for the addition of puppeteer.

Re: virtualenv standard location and bringing up the Isso dev server, my wish for that would be to have some kind of standardization in testing.
Either docker containers with docker-compose for client and server (too heavyweight imo plus dependency on docker+compose) or an entry in the Makefile for creating the virtualenv and then starting the server from inside Node? Or maybe requiring the user to start the server?

Instrumentation is hard, and I'm open to suggestions here as I have no experience and no idea what accepted standard practices are.

@ix5
Copy link
Copy Markdown
Member Author

ix5 commented Mar 3, 2022

Nice bootstrapping! Good job @ix5 :) Can I commit stuff on this branch if I find the time to write some tests?

Let me split up the puppeteer addition into another PR first, then commit away ;)

Edit: Done, new PR is at #807

@ix5 ix5 force-pushed the js-jest-puppeteer branch from a3e8580 to b147c72 Compare March 3, 2022 21:39
@ix5 ix5 changed the title [WIP] js: Add Jest testing with puppeteer end-to-end integration test [WIP] js: Add Jest testing Mar 3, 2022
@ix5 ix5 force-pushed the js-jest-puppeteer branch 2 times, most recently from fe23d2c to fbaac0b Compare March 7, 2022 15:23
ix5 added 5 commits March 21, 2022 21:39
Provide a base config and npm commands to run unit tests
using the `Jest` tool.

Recommendations have been lifted from
https://mailchimp.com/developer/open-commerce/docs/testing-requirements/

`jsdom` "emulates" a browser environment in a light way so
that access to `window`, `document` etc. is possible.
Just a simple test as a starting point for further ones.
Test expected languages when none are set in the browser.
Could be expanded further.
@ix5 ix5 force-pushed the js-jest-puppeteer branch from fbaac0b to 1ad1a05 Compare March 21, 2022 20:39
@ix5 ix5 changed the title [WIP] js: Add Jest testing js: Add Jest testing Mar 21, 2022
@ix5 ix5 changed the base branch from js-webpack to master March 21, 2022 20:41
@ix5 ix5 merged commit 7a1ae20 into isso-comments:master Mar 21, 2022
@ix5 ix5 deleted the js-jest-puppeteer branch March 21, 2022 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

client (Javascript) client code and CSS feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants