question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create tests for hasStore()

See original GitHub issue

Mission Control

The DAV project you are looking at is Mission Control. It is the brain in charge of orchestrating missions between DAV users and autonomous vehicles.

How you can help

The Issue

As a project that relies on a large community of contributors, it is very important for us to have good test coverage to make sure changes don’t break anything.

The file /server/lib/environment.js isn’t tested at all. Create a file /test/specs/lib.environment.spec.js that will test the function hasStore()

Test that hasStore() fulfills these conditions:

  • returns false if the DAV_ENV environment variable isn’t set to ‘simulated’

You might want to write the test so that it tests both conditions. You can get the environment variable in node using: process.env.DAV_ENV

You can see how this file should be structured based on the other files in the specs directory.

To run your tests, run npm test from the project’s root directory. All tests should pass.

tests

Contributing to Mission Control

  • Fork the repository from the Mission Control GitHub page.
  • Clone a copy to your local machine with $ git clone git@github.com:YOUR-GITHUB-USER-NAME/missioncontrol.git
  • Make sure you have node.js and npm installed on your machine. You can use this guide for help.
  • Install all of the project’s dependencies with npm. $ cd missioncontrol; npm install
  • Run npm test to run linting checks and all the automated tests and see that they pass.
  • Code! code! code!
  • Before committing your code, run npm test one last time and make sure no errors (including linting errors) are thrown.
  • Once you’ve made sure all your changes work correctly and committed all your changes, push your local changes back to github with $ git push -u origin master
  • Visit your fork on GitHub.com (https://github.com/YOUR-USER-NAME/missioncontrol) and create a pull request for your changes.
  • Make sure your pull request describes exactly what you changed and references this issue (include the issue number in the title like this: #28)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
TalAtercommented, Sep 16, 2017

First, a quick note. Since we want to make sure it returns false, I would use toBe(false) rather than toBeFalsy(). The latter would also work if it returns 0, ‘’, null, undefined, or NaN. See https://facebook.github.io/jest/docs/en/expect.html#tobefalsy

Second, we want to have each of our tests self-contained and just test a single condition. In other words, the tests that asserts that it returns false if the DAV_ENV environment variable isn't set to 'simulated', should not also test what happens when the environment is set to simulated (so, no else).

Now, regarding the test itself. It’s actually an interesting question of what to test here.

One obvious solution might be to expect(env !== 'simulation' && hasStore === false) (that’s pseudocode). But it feels to me like a more correct solution would be not to test for the environment variable as part of the assertion, and just test the thing we are testing.

In other words, I think this would be more correct:

test(`returns false when the DAV_ENV environment variable isn't set to 'simulated'`, () => {
  expect.assertions(1);
  if (process.env.DAV_ENV !== 'simulation') {
    expect(hasStore).toBe(false);
  }
});

It’s a subtle difference but it seems like a more correct one. The assertion just tests what we came here to test… and if in the future we change the environment that tests run in, it shouldn’t fail on returns false when the DAV_ENV environment variable isn't set to 'simulated' because hasStore didn’t break - it functioned correctly, it was the test that failed.

What would happen is that we would get an error saying Expected one assertion to be called but only received zero assertion calls. This would be a good chance for us to add tests for other environments.

This would also leave it up to us to implement a future test for the environment separately, and not tied into the test for hasStore.

This is just my view of the matter. You can expect myView.toBeTruthy(), but not necessarily myView.toBe(the only truth) 😉

P.S. I made a mistake in the task description. hasStore is not actually a function but a constant value. so there’s no need to invoke it. Leave the description of the test as is, as I will be refactoring it to a function once you are done adding the test.

1reaction
TalAtercommented, Sep 22, 2017

Looks great, @Rob-Rychs!

Thank you, and thank you for bearing with my idiosyncrasies 😄

Congrats on joining the community, and becoming an open-sourcerer!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing New Tests - H-Store - Brown University
For example, the following test case will automatically create the TPC-C project catalog for each test case. After the setUp() method is complete,...
Read more >
Testing Stores - Alt
This short tutorial demonstrates how to use ES6 modules in order to export both the alt created store as well as the store's...
Read more >
unittest — Unit testing framework — Python 3.11.1 ...
It checks for a specific response to a particular set of inputs. unittest provides a base class, TestCase , which may be used...
Read more >
Testing stores - Pinia
Stores will, by design, be used at many places and can make testing much harder ... This can be achieved with createTestingPinia() ,...
Read more >
Create tests | IntelliJ IDEA Documentation - JetBrains
The simplest way of creating a new test class in IntelliJ IDEA is by ... In the Create Test dialog, select the library...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found