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

assert.throws Doesn't Count Towards Assertions #41354

Open
SethFalco opened this issue Mar 4, 2021 · 7 comments
Open

assert.throws Doesn't Count Towards Assertions #41354

SethFalco opened this issue Mar 4, 2021 · 7 comments
Labels
help wanted Open for all. You do not need permission to work on these. scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory. type: bug Issues that need priority attention. Platform, Curriculum tests (if broken completely), etc.

Comments

@SethFalco
Copy link
Sponsor Member

Describe your problem and how to reproduce it:
I was working on the Sudoku Solver project and ran into an issue with my tests failing despite my unit tests passing locally.

Test case:

test('Invalid puzzle strings fail the solver', (done) => {
  const puzzleString = '115..2.84..63.12.7.2..5.....9..1....8.2.3674.3.7.2..9.47...8..1..16....926914.37.';
  assert.throws(() => solver.solve(puzzleString), 'Puzzle cannot be solved');
  done();
});  

Unit tests:
image

Network tab:
image

I can see in the tests for the project, it dictates all tests must have at least one assertion. I do have an assertion though, but it's not being returned.

Add a Link to the page with the problem:
https://www.freecodecamp.org/learn/quality-assurance/quality-assurance-projects/sudoku-solver

Tell us about your browser and operating system:

  • Browser Name: Firefox
  • Browser Version: 86.0 (64-bit)
  • Operating System: Ubuntu 20.04.2 LTS x86_64
@SethFalco SethFalco added scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory. status: waiting triage This issue needs help from moderators and users to reproduce and confirm its validity and fix. labels Mar 4, 2021
@sachinh19
Copy link

Have you tried doing something like this?

expect(() => solver.solve(puzzleString)).toThrow('Puzzle cannot be solved');

@SethFalco
Copy link
Sponsor Member Author

Have you tried doing something like this?

expect(() => solver.solve(puzzleString)).toThrow('Puzzle cannot be solved');

I've already passed the task so I don't need to work around it anymore.
However, I did try expect(() => solver.solve(puzzleString)).throw('Puzzle cannot be solved'); anyway.

This did not fix the issue. I'm betting it's probably something to do with the pattern not catching the () => syntax as a valid assertion, but haven't looked into it much yet.

@sachinh19
Copy link

We could probably close this issue since you have already finished the task.

@SethFalco
Copy link
Sponsor Member Author

SethFalco commented Mar 4, 2021

I think the problem lies with how the functions looks at runtime.

// assert.isFalse -> assert.isFalse.
function (done) { var puzzleString = '1.5..2.84..63.12.7.2..5.....9..1....8.2.3674.3.7.2..9.47...8..1..16....926914.37.'; var actual = solver.checkRegionPlacement(puzzleString, 0, 1, 2); assert.isFalse(actual); done(); }

// assert.throws -> assert["throws"]
function (done) { var puzzleString = '115..2.84..63.12.7.2..5.....9..1....8.2.3674.3.7.2..9.47...8..1..16....926914.37.'; assert["throws"](function () { return solver.solve(puzzleString); }, 'Puzzle cannot be solved'); done(); }

The regex can be updated to:

const cleanedBody = body.match(/(?:browser\s*\.\s*)?assert\s*(?:\.\s*\w*|\s*\[(["'])\w*\1\])\([\s\S]*\)/);

This will allow it to also work with the ["property"] syntax as well, for example:
image

Other parts will also still have to be updated, but unfortunately those methods are more complex, so I will review them later.
Just wanted to share some progress on it so far.


The following is a hack, but a quick and dirty fix instead is to update #assertionAnalyser to do the following to body at the start:

function assertionAnalyser(body) {
  if (!body)
    return "invalid assertion";

  body = body.replace(/assert\[(["'])(\w*)\1\]/, (match, p1, p2) => {
    return `assert.${p2}`;
  });

This will change assert["throws"] back to assert.throws and allow the rest to run as is, and the test cases will pass successfully! \(^-^)/
This is probably not an ideal solution, however. It'd be better to just parse the ["property"] syntax correctly.

@SethFalco
Copy link
Sponsor Member Author

We could probably close this issue since you have already finished the task.

No, because this is still a valid issue. I could work around it for now, however others will still encounter it when they shouldn't, and it still needs to be fixed.

@raisedadead raisedadead added help wanted Open for all. You do not need permission to work on these. type: bug Issues that need priority attention. Platform, Curriculum tests (if broken completely), etc. and removed status: waiting triage This issue needs help from moderators and users to reproduce and confirm its validity and fix. labels Apr 29, 2021
@NikIvan
Copy link
Contributor

NikIvan commented Aug 16, 2021

I've encountered same issue while was trying to use assert.throws() and assert.doesNotThrow() in some tests.

If adding this methods would be difficult, text of error in console should be fixed at least because now it's misleading. It says something like each test should contain at least one assert and obviously my tests contain asserts.

So maybe easy fix would be change description of this project (and maybe others which have same issue) and say something like Use at least one assert.equal in your tests to pass them

@Sboonny
Copy link
Member

Sboonny commented Jun 13, 2022

here is the file curriculum/challenges/english/06-quality-assurance/quality-assurance-projects/sudoku-solver.md

I am planning to solve the project, first. then I will attempt to solve this, because of fCC honest policy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Open for all. You do not need permission to work on these. scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory. type: bug Issues that need priority attention. Platform, Curriculum tests (if broken completely), etc.
Projects
None yet
Development

No branches or pull requests

6 participants