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

fix: add tests for update-notifier and adjust code #301

Merged
merged 1 commit into from
Dec 10, 2018

Conversation

yuliabaron
Copy link
Contributor

  • Ready for review
  • Follows CONTRIBUTING rules
  • Reviewed by Snyk internal team

What does this PR do?

Adds tests for update-notifier API

test('verify updater', function (t) {
t.plan(1);
var pkg = {
"name": "snyk",
Copy link
Contributor

Choose a reason for hiding this comment

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

" -> '

Copy link
Contributor

Choose a reason for hiding this comment

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

"name" -> name

resolveStub.onSecondCall().returns('falseFile');

t.equal(updateCheck(), false, 'Notifier was not started on missing package.json');
resolveStub.restore();
Copy link
Contributor

Choose a reason for hiding this comment

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

All the .restore() stuff should be in t.teardown callback. Because if test fails unrestored stub can affect other tests.

const updateNotifier = require('update-notifier');

// Fake location of the package.json file and verify the code behaves well
test('missing package.json', function (t) {
Copy link
Contributor

Choose a reason for hiding this comment

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

function (t) -> (t) =>

import * as p from 'path';

export function updateCheck() {
const root = p.resolve(__dirname, p.normalize('../..'));
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need p.normalize here - it is not user input (already normalised).

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe we can do something like const pkgPath = p.join(root, '../../package.json'); instead of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tests also run on Windows env, so I do need p.normalize. And this file is not a test, it runs in CLI.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just check it path.win32.join('bla1', 'bla2', 'bla3', 'Bla4', '../package.json'); I believe join already doing normalize inside.

const updateNotifier = require('update-notifier');

// Fake location of the package.json file and verify the code behaves well
test('missing package.json', function (t) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add test which actually checks that notifier works (I mean produces some output)?

Copy link
Contributor Author

@yuliabaron yuliabaron Dec 9, 2018

Choose a reason for hiding this comment

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

I can't run notifier during the test as it spawns a child process and probably during testing this is not possible. Plus, I do not want to test external library. All I want is to make sure I'm able to init it successfully with the API they claimed. This way, if API changes I'll catch that during tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

But in the test "verify updater" you're exactly testing external library? At least maybe better to mock fs.readFileSync?


t.equal(updateCheck(), false, 'Notifier was not started on missing package.json');
resolveStub.restore();
t.end();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure you need t.plan(x); and t.end(); in this tests.

Copy link
Contributor Author

@yuliabaron yuliabaron Dec 9, 2018

Choose a reason for hiding this comment

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

tests fail without that. I can remove t.plan, but without t.end it fails as it thinks the test is unfinished

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's remove t.plan(x); at least.

@yuliabaron yuliabaron force-pushed the fix/test_updater branch 3 times, most recently from f8daae5 to 04a6b6d Compare December 9, 2018 12:18
@yuliabaron yuliabaron merged commit 56726e1 into master Dec 10, 2018
@yuliabaron yuliabaron deleted the fix/test_updater branch December 10, 2018 07:04
@snyksec
Copy link

snyksec commented Dec 10, 2018

🎉 This PR is included in version 1.116.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants