Code Monkey home page Code Monkey logo

phase-0-pac-3-what-is-a-test-lab's Introduction

Testing with JavaScript Lab

Learning Goals

  • Running tests
  • Reading test results

Running Tests

You understand how to read tests; now it's time to run the tests.

Recall that in the previous lesson, the tests were commented out. Be sure to fork and clone this lab into your local environment so you have the version of the test file that is not commented out. (Return to the previous lesson if you need a reminder of how to do this.)

Navigate into its directory in the terminal, then run code . to open the files in Visual Studio Code. (If you are using a different text editor, the command will be different.) Finally, run npm install to install the lab's dependencies.

What exactly do we mean by installing dependencies? Open the package.json file and scroll down to the bottom. You'll see a list of 'DevDependencies'. What's listed here are JavaScript packages: files or sets of files full of existing, reusable code. They are designed to be shared, allowing many developers to use the same code in their own projects. The packages you see listed in package.json make it possible to run the lab's tests. In order to use the packages, we have to install them; npm install does that for us.

If you take a look at index.js and indexTest.js, you should see the same code as in the previous lesson. The only difference is that the test code in indexTest.js is no longer commented out.

Important: You should never need to make changes to test files unless a lab's instructions specifically tell you to do so.

To run the tests, run npm test in the terminal. That's it!

The next step is learning how to read the results that the tests give you.

Reading Results of Tests

The first time you run npm test, you should see something that looks like this:

> [email protected] test
> mocha --timeout 5000 -R mocha-multi --reporter-options spec=-,json=.results.json


  what-is-a-test
    Name
      1) returns "Susan"
    Height
      2) is less than 40 and greater than 0
    Message
      ✓ gives the name and height


  1 passing (552ms)
  2 failing

  1) what-is-a-test
       Name
         returns "Susan":

      Error: Expected 'Joe' to equal 'Susan'
      + expected - actual

      -Joe
      +Susan

      at assert (node_modules/expect/lib/assert.js:29:9)
      at Expectation.toEqual (node_modules/expect/lib/Expectation.js:81:30)
      at Context.<anonymous> (test/indexTest.js:6:26)
      at processImmediate (internal/timers.js:461:21)

  2) what-is-a-test
       Height
         is less than 40 and greater than 0:
     Error: Expected 74 to be less than 40
      at assert (node_modules/expect/lib/assert.js:29:9)
      at Expectation.toBeLessThan (node_modules/expect/lib/Expectation.js:156:28)
      at Context.<anonymous> (test/indexTest.js:13:28)
      at processImmediate (internal/timers.js:461:21)

npm ERR! Test failed.  See above for more details.

Note: If you also get an error that ends with "unexpected character (after ) at line 1, column 1 [parse.c:769] (Oj::ParseError)", make sure you've cloned down the files for this lab, and are not running the tests on the files from the previous lesson.

Let's break this down a bit. If you look about a third of the way down in the output, you'll see a summary of how the tests went:

  1 passing (552ms)
  2 failing

Great! We've already got one test passing! Now let's see how we failed the other two tests.

  1) what-is-a-test
       Name
         returns "Susan":

      Error: Expected 'Joe' to equal 'Susan'
      + expected - actual

      -Joe
      +Susan

      at assert (node_modules/expect/lib/assert.js:29:9)
      at Expectation.toEqual (node_modules/expect/lib/Expectation.js:81:30)
      at Context.<anonymous> (test/indexTest.js:6:26)
      at processImmediate (internal/timers.js:461:21)

  2) what-is-a-test
       Height
         is less than 40 and greater than 0:
     Error: Expected 74 to be less than 40
      at assert (node_modules/expect/lib/assert.js:29:9)
      at Expectation.toBeLessThan (node_modules/expect/lib/Expectation.js:156:28)
      at Context.<anonymous> (test/indexTest.js:13:28)
      at processImmediate (internal/timers.js:461:21)

While there is no hard and fast rule, and there will be exceptions, it is most often best to address your test errors in order. So let's take a look at our first error:

1) what-is-a-test
       Name
         returns "Susan":

      Error: Expected 'Joe' to equal 'Susan'
      + expected - actual

      -Joe
      +Susan

      at assert (node_modules/expect/lib/assert.js:29:9)
      at Expectation.toEqual (node_modules/expect/lib/Expectation.js:81:30)
      at Context.<anonymous> (test/indexTest.js:6:26)
      at processImmediate (internal/timers.js:456:21)

Here is the specific error:

      Error: Expected 'Joe' to equal 'Susan'
      + expected - actual

      -Joe
      +Susan

It tells us what the test is expecting (Expected 'Joe' to equal 'Susan') and then gives us details about the expected and actual values. This shows you exactly how the value your code is returning (the actual value) differs from what the test is looking for. Make sure you understand what this is telling you — it will come in handy in later labs!

This error makes sense because we have the name variable set equal to "Joe" in our index.js file. Let's change that line of code to set name equal to "Susan" instead. Run the tests again by typing npm test in the terminal's command line (don't forget to save the file first!), and you should see that we are now passing 2 of the 3 tests!

  what-is-a-test
    Name
      ✓ returns "Susan"
    Height
      1) is less than 40 and greater than 0
    Message
      ✓ gives the name and height


  2 passing (736ms)
  1 failing

  1) what-is-a-test
       Height
         is less than 40 and greater than 0:
     Error: Expected 74 to be less than 40
      at assert (node_modules/expect/lib/assert.js:29:9)
      at Expectation.toBeLessThan (node_modules/expect/lib/Expectation.js:156:28)
      at Context.<anonymous> (test/indexTest.js:13:28)
      at processImmediate (internal/timers.js:461:21)

Woot! You passed another one. Now go ahead and try to pass the remaining test on your own.

Common Errors

While you are solving the other tests you may come across a few errors. Let's go over some common ones:

Variable Not Defined

ReferenceError: name is not defined

That one says that the name variable is not defined. That makes no sense! We initialized the name variable in index.js! What that actually means is that the test couldn't find the variable name. You'll get this error if the name of one of your variables is different than the test is expecting. Check to make sure you used the correct variable names and look carefully for typos.

Unexpected Identifier

/Users/lizburton_fs/Development/code/phase-0-pac-3-what-is-a-test-lab/index.js:1
cnst name = "Susan";
     ^^^^

SyntaxError: Unexpected identifier
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/Users/lizburton_fs/Development/code/phase-0-pac-3-what-is-a-test-lab/test/indexTest.js:1:15)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at /Users/lizburton_fs/Development/code/phase-0-pac-3-what-is-a-test-lab/node_modules/mocha/lib/mocha.js:311:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/Users/lizburton_fs/Development/code/phase-0-pac-3-what-is-a-test-lab/node_modules/mocha/lib/mocha.js:308:14)
    at Mocha.run (/Users/lizburton_fs/Development/code/phase-0-pac-3-what-is-a-test-lab/node_modules/mocha/lib/mocha.js:849:10)
    at Object.exports.singleRun (/Users/lizburton_fs/Development/code/phase-0-pac-3-what-is-a-test-lab/node_modules/mocha/lib/cli/run-helpers.js:108:16)
    at exports.runMocha (/Users/lizburton_fs/Development/code/phase-0-pac-3-what-is-a-test-lab/node_modules/mocha/lib/cli/run-helpers.js:143:13)
    at Object.exports.handler (/Users/lizburton_fs/Development/code/phase-0-pac-3-what-is-a-test-lab/node_modules/mocha/lib/cli/run.js:305:3)
    at Object.runCommand (/Users/lizburton_fs/Development/code/phase-0-pac-3-what-is-a-test-lab/node_modules/yargs/lib/command.js:242:26)
    at Object.parseArgs [as _parseArgs] (/Users/lizburton_fs/Development/code/phase-0-pac-3-what-is-a-test-lab/node_modules/yargs/yargs.js:1104:24)
    at Object.parse (/Users/lizburton_fs/Development/code/phase-0-pac-3-what-is-a-test-lab/node_modules/yargs/yargs.js:566:25)
    at Object.exports.main (/Users/lizburton_fs/Development/code/phase-0-pac-3-what-is-a-test-lab/node_modules/mocha/lib/cli/cli.js:68:6)
    at Object.<anonymous> (/Users/lizburton_fs/Development/code/phase-0-pac-3-what-is-a-test-lab/node_modules/mocha/bin/mocha:133:29)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
npm ERR! Test failed.  See above for more details.

Whoa! So many words that make no sense. Don't worry though. The most important line is the SyntaxError: Unexpected identifier line. What that means is you have some sort of typo or syntax mistake. It could be a HUGE variety of things but usually, JS will try and give you a hint. This time it's pointing to the cnst name = "Susan" line of code. Take a look and read very carefully: const is misspelled. Whoops! Once we fix that everything will work.

One note on this type of error is that it is sort of a catch-all. Tons and tons of problems end in that sort of error message. Whenever you see it, be sure to read over your code with a fine-toothed comb... and you'll find the problem!

Type Errors

On the second test, there is a chance you might see the following error:

1) what-is-a-test
       Height
         is less than 40 and greater than 0:
     Error: The "actual" argument in expect(actual).toBeLessThan() must be a number
      at assert (node_modules/expect/lib/assert.js:29:9)
      at Expectation.toBeLessThan (node_modules/expect/lib/Expectation.js:156:28)
      at Context.<anonymous> (test/indexTest.js:13:28)
      at processImmediate (internal/timers.js:456:21)

This error is slightly different than the last two. In this case, the test is giving us a unique message because it recognizes a problem. If we look at this test in test/indexTest.js, we see this:

describe("Height", () => {
    it("is less than 40 and greater than 0", () => {
      expect(height).toBeMoreThan(0)
      expect(height).toBeLessThan(40);
    });
  });

We can see that the word "actual" in this case is referring to the height variable. The error message is telling us that height must be a number. If you're seeing this, make sure that you have set the height variable to a number that's less than 40 (e.g. 39), not a string ("39"). The test will interpret the value as a string due to the quotation marks wrapping it.

Optional Mocha Configuration

In this lab, we only had three tests to pass, but as you continue through the curriculum you will encounter labs with many more tests. You can imagine that the test output could get very long, making it more difficult to focus in on how to fix a particular error.

To help with this issue, there is some very simple setup you can put in place in Mocha that will cause the tests to stop as soon as the first failing test is encountered.

To implement this, open up the package.json file and find the test script. It should look something like this:

"test": "mocha --timeout 5000 -R mocha-multi --reporter-options spec=-,json=.results.json"

Add the --bail flag to the end of the line, inside the quotes:

"test": "mocha --timeout 5000 -R mocha-multi --reporter-options spec=-,json=.results.json --bail"

That's it!

Submitting Your Work to Canvas

Once you've got all the tests passing, it's time to push your completed code up to GitHub and submit it to Canvas using CodeGrade. We'll do a quick review of how to do that below, but you may want to review the full process in the Complete Your First Software Engineering Assignment lesson. You'll be going through this process for every lab you do in this program!

Let's review the process. First, you need to "stage" your changes using the git add command:

$ git add index.js

or

$ git add .

Recall that the . shortcut will stage all files that have changes. In this case there's only one so either command will work.

Next, you need to "commit" your changes, which basically saves a record of the changes you've made. Don't forget to use the -m flag and include a commit message! Use the message shown below or choose your own:

$ git commit -m "complete lab"

Finally, push your changes up to your GitHub account (your fork of this lab):

$ git push

If you go back to your repo in GitHub and refresh the page, you should now see a new commit with your commit message.

The final step is to submit your work to Canvas:

  1. Scroll to the bottom of this lesson page in Canvas and click the button labeled "Load Review: Variables Lab in a new window".
  2. In the CodeGrade window that opens, click "Create Submission". You should now see a list of your repositories.
  3. Find the repo for this lab and click Connect.
  4. When you get the message that your repo has been connected, click on the embedded link, then the "AutoTest" tab to watch your progress. Once the tests have finished running, you should see the green checkmark in the "Pass" column, indicating that you've successfully completed the lab.

CodeGrade window showing tests have all been passed

Note about the git push command

You may recall from the Complete Your First Software Engineering Assignment lesson that a different form of the git push command was used:

$ git push <remote> <branch>

where remote is the "alias" for the repo's url on GitHub, and branch is the repo's default branch (generally main for newer repos and master for older ones). For this lab, therefore, the full command would be:

$ git push origin master

This command tells git to push the code in the master branch of the local repo to the master branch of the remote repo identified by the origin alias.

So why didn't you need to run that command?

When you use the git clone command to clone down a repo from GitHub, git automatically assigns the "origin" alias to the url you clone from, and uses the default branch for that repo.

As you work through the labs in this program, you should always:

  1. fork the lab's repo to your GitHub account, and
  2. clone that fork down to your local machine.

As long as you always fork before you clone, it should be safe to run git push without specifying the remote and branch.

If you want to verify that you're pushing to the right repo, you can use the git remote command and include the -v flag:

$ git remote -v
origin	[email protected]:your-github-username/phase-0-pac-3-what-is-a-test-lab.git (fetch)
origin	[email protected]:your-github-username/phase-0-pac-3-what-is-a-test-lab.git (push)

Here you can see that the origin alias points to your fork of the repo, so it's safe to run the shorter command, git push.

Conclusion

Now that you've gotten all your tests passing and submitted your work (and learned a bit more about git push), you're ready to move on. Congratulations! You've solved your first JavaScript tests!

phase-0-pac-3-what-is-a-test-lab's People

Contributors

danielseehausen avatar davidwolff218 avatar dependabot[bot] avatar graciemcguire avatar ihollander avatar jlboba avatar jmburges avatar kwebster2 avatar lizbur10 avatar maxwellbenton avatar rrcobb avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phase-0-pac-3-what-is-a-test-lab's Issues

error message when running test

mannywalker@Mannys-MacBook-Air phase-0-pac-3-what-is-a-test % learn test

[email protected] test
mocha --timeout 5000 -R mocha-multi --reporter-options spec=-,json=.results.json

Hello-
I am receiving this error message when I attempt to complete this test. Please advise. Thank you!

Manny

  1. "before all" hook in "{root}"

0 passing (178ms)
1 failing

  1. "before all" hook in "{root}":
    TypeError: require(...).env is not a function
    at Context. (node_modules/mocha-jsdom/index.js:52:22)
    at process.processImmediate (node:internal/timers:471:21)

mannywalker@Mannys-MacBook-Air phase-0-pac-3-what-is-a-test %

Forgotten Reminder

Canvas Link

https://learning.flatironschool.com/courses/5470/assignments/168088?module_item_id=363181

Concern

This page needs a reminder to delete the /* and / block commenting syntax in the index-test.js file before running learn test; this is briefly mentioned on the previous page, reading, "In the next lesson, we will remove the / and / so we can run the tests." However, it is not confirmed or mentioned again on this page I linked. I hesitated doing it myself particularly because the linked page read, "You should never need to make changes to test files unless a lab's instructions specifically tell you to do so." Because my learn test output was not what I expected, I eventually deleted the / and */ from index-test.js and tried again, getting the expected output. It was fairly easy for me to diagnose, but if students take a break between the previous page and this one, they may forget the note about index-test.js being commented out. I hope this makes sense!

Additional Context

No response

Suggested Changes

Early in this page, enter a note to remove the /* and */ from the file so we can run the tests.

what-is-a-test-lab issue

Canvas Link

https://learning.flatironschool.com/courses/6986/modules/items/570507

Concern

I'm getting a warning when I run the npm install command (see additional context section for details).

Also, it appears I am not getting the full code in index.js. This is all that is shown:

const name = "Joe";
const height = 74;
const message = ${name} is ${height} inches tall;

module.exports = {name, height, message};

Additional Context

john@DESKTOP_WIN_10:~/Development/code/se-prep/phase-0-pac-3-what-is-a-test-lab$ npm install
npm WARN deprecated [email protected]: this library is no longer supported
npm WARN deprecated [email protected]: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
npm WARN deprecated [email protected]: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
npm WARN deprecated [email protected]: request has been deprecated, see request/request#3142

added 184 packages, and audited 185 packages in 4s

59 packages are looking for funding
run npm fund for details

6 vulnerabilities (2 moderate, 1 high, 3 critical)

To address all issues (including breaking changes), run:
npm audit fix --force

Run npm audit for details.

Suggested Changes

No response

npm test producing incorrect results

Thanks for raising this issue! Future learners thank you for your diligence. In
order to help the curriculum team address the problem, please use this template
to submit your feedback. We'll work on addressing the issue as soon as we can.

Please fill out as much of the information below as you can (it's ok if you
don't fill out every section). The more context we have, the easier it will be
to fix your issue!

Note: you should only raise issues related to the contents of this lesson.
If you have questions about your code or need help troubleshooting, reach out to
an instructor/your peers.


Link to Canvas

Add a link to the assignment in Canvas here.

Describe the bug

instead of providing the expected output on first run, it produced this.

$ npm test

[email protected] test
mocha --timeout 5000 --reporter 'json' > .results.json & mocha

  1. "before all" hook

0 passing (3s)
1 failing

  1. "before all" hook:
    Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
    at Object.done (node_modules/mocha-jsdom/index.js:70:7)
    at /mnt/d/Programming/FlatironSchool/code/phase0/phase-0-pac-3-what-is-a-test-lab/node_modules/jsdom/lib/jsdom.js:325:18
    at processTicksAndRejections (node:internal/process/task_queues:78:11)

Even if I continued with the lab accordingly (although difficult since I couldnt use my own test to debug, I could not proceed.

Write a clear and concise description of what the bug is. Include the file and
line number(s) if possible.

What is the expected behavior?

Write a clear and concise description of what you expected to happen.

Screenshots

If applicable, attach screenshots to help explain your problem.

What OS are you using?

  • OS X
  • WSL
  • Linux

Any additional context?

Add any other context about the problem here.

Missing instructions lead to frustration

Canvas Link

https://learning.flatironschool.com/courses/5703/assignments/174656?module_item_id=382279

Concern

You specifically tell us:
"Important: You should never need to make changes to test files unless a lab's instructions specifically tell you to do so."

However, nowhere on that page does it tell you to remove the */ from the test file. It mentioned on the previous page that the code won't run if this is still on there but on this page it doesn't say to remove it, in fact, it says the exact opposite. This means when I run the test it returns almost nothing and is very confusing.

Additional Context

No response

Suggested Changes

Add the instruction to remove the */ from the test file

Height can not be valid and all tests pass

Canvas Link

https://learning.flatironschool.com/courses/6956/assignments/239275?module_item_id=562221

Concern

Nothing. No problems submitting or doing the assignment, but a known bug is discovered and is being reported with the project itself. index.js height variable set on line 2. indexTest.js test on line 14 over simplifies the issue and does not account for this bug.

Additional Context

I did not have a problem with the program, but I did want to note that it is possible to pass in a negative height and use it. And have your height is less than 40 inches test to pass. When height is clearly not valid. Have a look.

Suggested Changes

Make a test to check to make sure that height is at least zero. Now, unless the person is dead or did not exist in the first place, then 0 would also not be valid. But would still pass the height is less than 40 inches requirement.

Include "Save index.js file"

Canvas Link

https://learning.flatironschool.com/courses/6324/assignments/214210?module_item_id=496764

Concern

This error makes sense because we have the name variable set equal to "Joe" in our index.js file. Let's change that line of code to set name equal to "Susan" instead. Run the tests again by typing learn test in the terminal's command line, and you should see that we are now passing 2 of the 3 tests!

Additional Context

No response

Suggested Changes

A line should be added to thiz paragraph specifying that you should save the index.js file.

This error makes sense because we have the name variable set equal to "Joe" in our index.js file. Let's change that line of code to set name equal to "Susan" instead. (INSERT HERE "and save the index.js file again.") Run the tests again by typing learn test in the terminal's command line, and you should see that we are now passing 2 of the 3 tests!

Permission denied.

I'm getting this error as follows:
"ERROR: Permission to learn-co-curriculum/phase-0-pac-3-what-is-a-test-lab.git denied to bryantmlin.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists."

I've correctly forked and cloned the lab, and after verifying that I'm indeed using the correct repository with git remote -v, I see these as my outputs:

origin [email protected]:learn-co-curriculum/phase-0-pac-3-what-is-a-test-lab.git (fetch)
origin [email protected]:learn-co-curriculum/phase-0-pac-3-what-is-a-test-lab.git (push)

I'm not sure how to proceed, how do I delete the original repository that I cloned?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.