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

Extend tests for --watch options #3915

Conversation

geigerzaehler
Copy link
Contributor

Requirements

Test coverage for the --watch option needs to be improved. We also need a solid base to be able to test the changes proposed in #3912.

Description of the Change

This PR adds two test cases to test the --watch option. We check that touching a test file reruns the tests and we test that touching file that has a correct extensions reruns the test.

With the support code introduced we also update the one existing test.

The change uses promises instead of callbacks. For example we change runMochaJSONRaw to runMochaJSONRawAsync. The use of promises improves the control flow and gives better error messages.

Benefits

  • Better test coverage
  • It is now easy to write more watcher tests.
  • The use of promises provides better control flow guarantees and error messages.

Possible Drawbacks

The tests introduce fs-extra as a dev dependency. We need this for the following reasons:

  • Node v6 does not support fs.copyFile
  • Nodes vanilla fs module has no method to remove a directory and its content.

The tests require quite a lot of non-trivial support code.

With the introduction of runMochaJSONRawAsync we mix promise based and callback based APIs. While this is not ideal the benefits of using promises (described above) warrant this inconsistency

Applicable issues

#3912

@jsf-clabot
Copy link

jsf-clabot commented May 14, 2019

CLA assistant check
All committers have signed the CLA.

@coveralls
Copy link

coveralls commented May 14, 2019

Coverage Status

Coverage increased (+0.6%) to 92.414% when pulling c66e6a4 on geigerzaehler:add-integration-tests-for-file-watching into 750c5f8 on mochajs:master.

@geigerzaehler geigerzaehler force-pushed the add-integration-tests-for-file-watching branch from bb66278 to 5a2124b Compare May 14, 2019 19:43
@juergba
Copy link
Member

juergba commented May 15, 2019

Yes, this is non-trivial stuff and I will need some time to digest this.

I have doubts about the change from callback to promises. I would have expexted the tests to crash, since our integration tests should also include browser and yes IE11 tests.
If you push your branch directly to our repos mochajs/mocha, then additional tests will run and your changes will probably fail. I'm not sure though...

runMochaJSONRaw: it has been required in just one single spec file?

@geigerzaehler
Copy link
Contributor Author

I would have expexted the tests to crash, since our integration tests should also include browser and yes IE11 tests.

As far as I can see from karma.conf.js only the unit tests (test/unit/*.spec.js) are run in the browser. It seems to me that running the integration tests in the browser does not make sense since they actually consist of running the the mocha executable.

runMochaJSONRaw: it has been required in just one single spec file?

Yes, only in the watcher spec. (I would actually prefer to move it to the spec file but did not want to make to many changes.)

@juergba
Copy link
Member

juergba commented May 15, 2019

Thanks @geigerzaehler
I asked @boneskull to help me out and have a look at this.

@juergba
Copy link
Member

juergba commented May 16, 2019

I pushed your branch to this repo and both additional tests have succeeded.
Note to myself: don't worry, be happy.

@boneskull
Copy link
Member

@juergba

I have doubts about the change from callback to promises. I would have expexted the tests to crash, since our integration tests should also include browser and yes IE11 tests.

This is untrue (unfortunately); only the stuff in test/unit and test/browser-specific run in a browser. I have an experimental branch where I'm trying to reconcile these and run some portion of the integration tests in a browser, but it's incomplete.

I was also hoping to switch to Promise-based test helpers, so this is a welcome change from my view.

Copy link
Member

@boneskull boneskull left a comment

Choose a reason for hiding this comment

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

this is a welcome addition to our test suites, thank you!

I have a couple requested changes, IMO nothing major. please resolve them or discuss.

.eslintrc.yml Outdated
@@ -25,6 +25,7 @@ overrides:
- bin/*
- lib/cli/**/*.js
- test/node-unit/**/*.js
- test/integration/**/*.js
Copy link
Member

Choose a reason for hiding this comment

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

would prefer if we can list individual files here; would rather not open the floodgates to ES6 in the integration tests, as there are tentative plans to run some portion of them in a browser

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -282,7 +287,7 @@ function resolveFixturePath(fixture) {
if (path.extname(fixture) !== '.js') {
fixture += '.fixture.js';
}
return path.join('test', 'integration', 'fixtures', fixture);
return path.resolve('test', 'integration', 'fixtures', fixture);
Copy link
Member

Choose a reason for hiding this comment

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

does this actually change anything?

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 was necessary before but I changed the signature of runMochaJSONRawAsync to not use a special fixturePath argument. This allows us to revert this change and make the code more understandable.

3f6372e

test/integration/options/watch.spec.js Outdated Show resolved Hide resolved
* Touch a file by appending a space to the end. Returns a promise that resolves
* when the file has been touched.
*/
function touchFile(file) {
Copy link
Member

Choose a reason for hiding this comment

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

this function doesn't return anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the documentation in 3948056

test/integration/options/watch.spec.js Show resolved Hide resolved
args = ['--watch'].concat(args);
const [mocha, mochaDone] = runMochaJSONRawAsync(fixture, args, spawnOpts);
const testResults = mochaDone.then(data => {
expect(data.code, 'to be', sigintExitCode);
Copy link
Member

Choose a reason for hiding this comment

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

I think we can skip this. these tests aren't asserting Mocha exits how we expect; they are --watch tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

test/integration/helpers.js Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
@geigerzaehler
Copy link
Contributor Author

Thanks for the review @boneskull. I hope I have addressed all your comments

@juergba
Copy link
Member

juergba commented May 19, 2019

@boneskull why do we test with "cross-spawn", when in real life Mocha runs with node's "child_process"?

@boneskull
Copy link
Member

I don’t think there’s a great reason. There are issues with child_process spawn on windows, but don’t know if those affect mocha in any meaningful way

@juergba juergba requested a review from craigtaub May 19, 2019 15:53
@juergba
Copy link
Member

juergba commented May 19, 2019

@craigtaub could you review this PR, please? I think you have been the author of the first watch test.

Copy link
Member

@juergba juergba left a comment

Choose a reason for hiding this comment

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

@geigerzaehler thanks!

Copy link
Contributor

@craigtaub craigtaub left a comment

Choose a reason for hiding this comment

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

Tiny comment. Nice work. LGTM

This commit adds two test cases to test the `--watch` option. We check
that touching a test file reruns the tests and we test that touching a
file that has a correct extensions reruns the test.

This commit adds `fs-extra` as a new dev dependency.
@geigerzaehler geigerzaehler force-pushed the add-integration-tests-for-file-watching branch from 2777874 to c66e6a4 Compare May 21, 2019 08:46
Copy link
Member

@boneskull boneskull left a comment

Choose a reason for hiding this comment

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

LGTM!

.eslintrc.yml Show resolved Hide resolved
@boneskull boneskull merged commit a758154 into mochajs:master May 21, 2019
@boneskull
Copy link
Member

@geigerzaehler thanks a ton!

@geigerzaehler
Copy link
Contributor Author

@geigerzaehler thanks a ton!

Thanks for the support

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

6 participants