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: add testing framework #2

Merged
merged 4 commits into from Aug 7, 2017
Merged

Conversation

niedzielski
Copy link
Owner

  • Move linting from test to a test prerequisite, the pretest script.

  • Add Mocha test and test:watch scripts:

    • Tests are permitted in test/ and src/ per ticket requirements.
      However, it's unclear why they'd be wanted in both. The test/
      directory was also added to format and lint scripts.
    • A throwaway test was added since the absence of all tests causes a
      failure and to show that the setup works.
  • Add Mocha, Sinon.JS, and Chokidar CLI to devDependencies:

    • Mocha is used because that's what Services and Readers'
      Infrastructure use and it's quite popular.
    • Sinon.JS is used because MobileFrontend uses it and Readers'
      Infrastructure is considering it (T151091*) and it's popular too.
    • Chokidar CLI (chokidar-cli) was chosen because it's popular, quick,
      and Chokidar is used by Live Server (live-server). watch and
      npm-watch were each tried but appeared to function sporadically and
      slowly on Ubuntu GNOME v17.04. npm test -- -w cannot be used
      because 1) continuous linting is also wanted, 2) [new files are not
      observed], 3) a maintainer indicated they were wanted to get rid of
      watch functionality in the same report, and 4) separating watch and
      testing responsibilities gives more flexibility in responding to
      file changes. No attempts were made to filter or throttle files
      observed.

    *The Services team template and most of the [Readers' Infrastructure
    repos] do not reference it.

    [Readers' Infrastructure repos]: as described on:

  • A Mocha ESLint environment was deliberately omitted to force tests to
    explicitly specify their dependencies even though the Mocha API is a
    global.

Bug: T172432

- Move linting from `test` to a test prerequisite, the `pretest` script.

- Add Mocha `test` and `test:watch` scripts:
  - Tests are permitted in test/ and src/ per ticket requirements.
    However, it's unclear why they'd be wanted in both. The test/
    directory was also added to format and lint scripts.
  - A throwaway test was added since the absence of all tests causes a
    failure and to show that the setup works.

- Add Mocha, Sinon.JS, and Chokidar CLI to `devDependencies`:
  - Mocha is used because that's what Services and Readers'
    Infrastructure use and it's quite popular.
  - Sinon.JS is used because MobileFrontend uses it and Readers'
    Infrastructure is considering it (T151091*) and it's popular too.
  - Chokidar CLI (chokidar-cli) was chosen because it's popular, quick,
    and Chokidar is used by Live Server (live-server). watch and
    npm-watch were each tried but appeared to function sporadically and
    slowly on Ubuntu GNOME v17.04. `npm test -- -w` cannot be used
    because 1) continuous linting is also wanted, 2) [new files are not
    observed], 3) a maintainer indicated they were wanted to get rid of
    watch functionality in the same report, and 4) separating watch and
    testing responsibilities gives more flexibility in responding to
    file changes. No attempts were made to filter or throttle files
    observed.

  *The [Services team template] and most of the [Readers' Infrastructure
  repos] do not reference it.

  [Services team template]: https://github.com/wikimedia/service-template-node/blob/3842da2/package.json
  [Readers' Infrastructure repos]: as described on:
    - https://www.mediawiki.org/wiki/Wikimedia_Reading_Infrastructure_team#Projects
    - https://www.mediawiki.org/wiki/Reading/Component_responsibility
    And discovered with:
    $ find -not \( \( -name .git -o -name node_modules -o -name i18n -o -name sinonjs -o -name bundle -o -name .bundle \) -prune \) -type f|
      xargs -rd\\n grep -El 'sinon|\.(calledOn|calledWith|spy)\('
    mediawiki/tests/qunit/...
    mediawiki/extensions/MobileFrontend/...
    mediawiki/extensions/MultimediaViewer/...
    mediawiki/extensions/TimedMediaHandler/...
  [new files are not observed]: mochajs/mocha#2176

- A Mocha ESLint environment was deliberately omitted to force tests to
  explicitly specify their dependencies even though the Mocha API is a
  global.

Bug: T172432
Copy link
Collaborator

@joakin joakin left a comment

Choose a reason for hiding this comment

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

We should only use a file watching utility.

Personally I've used a lot nodemon, but I know it uses chokidar, so I don't really care.

package.json Outdated
"lint": "eslint '{src,test}/**/*.js'",
"pretest": "npm run lint",
"test": "mocha '{src,test}/**/*.test.js'",
"test:watch": "chokidar '**' --silent -c 'npm test'",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure if you realized but I installed nodemon previously which wraps chokidar and some other niceties and does basically the same with a nice API. For example you can trigger restarts by typing rs in the watching process without having to touch a file.

I don't mind switching to raw chokidar but I strongly believe that we should be only using one file watching utility.

This command with nodemon I believe would be something like:

nodemon -w src -w test --exec "npm test"

Let me know what you think.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thanks for the tip! Initially filtered to src/ and test/ which means package, module, and other changes outside these directories will be undetected. File deletions do not appear to be observed.

@@ -0,0 +1,10 @@
const assert = require("assert");
const mocha = require("mocha");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you do here?:

const {describe, it} = require('mocha')

Great idea to not include the globals IMO 👍

Copy link
Owner Author

Choose a reason for hiding this comment

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

👍 Sure, done.

@joakin
Copy link
Collaborator

joakin commented Aug 7, 2017

I pushed some commits on the other branch so this one should probably be rebased. Sorry for the pain ☹️

@niedzielski niedzielski changed the base branch from T172429 to master August 7, 2017 14:27
Initially filtered to src/ and test/ which means package, module, and
other changes outside these directories will be undetected. File
deletions do not appear to be observed.
@niedzielski
Copy link
Owner Author

@joakin, not at all! I've merged master into this branch (as I was afraid we'd lost our comments). Please let me know if I've botched this or if you'd prefer I not stack PRs.

I have a little more feedback on this patch that will probably have to come after my meetings which start in a few minutes.

"lint:all": "npm run lint -- '{src,test}/**/*.js'",
"pretest": "npm run lint:all",
"test": "mocha '{src,test}/**/*.test.js'",
"test:watch": "nodemon -w src -w test -x 'npm test'",
"precommit": "lint-staged"
},
"lint-staged": {
Copy link
Owner Author

Choose a reason for hiding this comment

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

Should this be npm run lint:all now?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you think we should run the unit tests on precommit? We are doing it in popups and it hasn't been a problem since the unit tests have remained quite snappy. I'm fine just leaving npm test there.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Now that I see, I may have misunderstood what lint-staged is for. Should we run lint-staged && npm test on precommit?

Nothing to do with this patch tho.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I would like to run as many tests and checks as we can in the precommit hook. I wasn't sure if calling them from lint-staged was correct, however, since npm test are the unit tests (not linting) and npm run lint:all is executed as a prerequistite to tests (so it doesn't actually need to be called explicitly).

@joakin joakin merged commit 6e03dd3 into master Aug 7, 2017
@joakin joakin deleted the chore/T172432-test-framework branch August 7, 2017 15:57
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