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

Reset defaults before tests #1212

Merged
merged 4 commits into from
Apr 10, 2018
Merged

Reset defaults before tests #1212

merged 4 commits into from
Apr 10, 2018

Conversation

UziTech
Copy link
Member

@UziTech UziTech commented Apr 10, 2018

Marked version: 0.3.19

Description

  • add function marked.getDefaults() that gets assigned to marked.defaults
  • resets the defaults before each test

Contributor

  • Test(s) exist to ensure functionality and minimize regression (if no tests added, list tests covering this PR); or,
  • no tests required for this PR.
  • If submitting new feature, it has been documented in the appropriate places.

Committer

In most cases, this should be a different person than the contributor.

  • Draft GitHub release notes have been updated.
  • CI is green (no forced merge required).
  • Merge PR

@UziTech UziTech mentioned this pull request Apr 10, 2018
5 tasks
Copy link
Member

@styfle styfle left a comment

Choose a reason for hiding this comment

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

This cloned object could have the same problem if it’s mutated. Instead, created a function marked.getDefaults() that returns a new object each time. That function can be used to assign the default object the first time.

@UziTech
Copy link
Member Author

UziTech commented Apr 10, 2018

good call

lib/marked.js Outdated
clone[opt] = defaults[opt];
}

return clone;
Copy link
Member

@styfle styfle Apr 10, 2018

Choose a reason for hiding this comment

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

Is the clone still necessary?
Can't you return defaults; here instead?
It's always returning a new object each call to the function so I can't think of a scenario where it would break.

Copy link
Member Author

Choose a reason for hiding this comment

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

I see what you are saying

@joshbruce
Copy link
Member

I ran into problems using setOptions, that's why we switched to making them part of the call. I was thinking we would want to do something like:

  beforeEach(function() {
    marked.defaults = {
      headerIds: false,
      xhtml: true
    }
  });

  afterEach(function() {
    marked.defaults = {
      headerIds: true,
      xhtml: false
    }
  });

The CommonMark spec doesn't recognize header IDs and uses XHTML for some of the tests.

lib/marked.js Outdated
xhtml: false
};

var clone = {};
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to do this as part of the test itself instead of the lib? Believe @styfle mentioned something like that before. Thinking it would be better to have the test use a blank object over the singleton pattern that seems preferred by Node (understandably in many use cases).

@UziTech
Copy link
Member Author

UziTech commented Apr 10, 2018

@joshbruce We could do something like that in the tests but then next time we add a new test file we will have to remember to do the same thing or we will fall into the same trap. This way we never have to think about it again.

@UziTech UziTech mentioned this pull request Apr 10, 2018
6 tasks
@joshbruce
Copy link
Member

@UziTech: Agreed. Think that's on us as reviewers. Not sure how often we'll be adding new test suites at this juncture, just seems the more logical place and, if we can do it in a single place, then we should be good.

@joshbruce
Copy link
Member

@UziTech: PS. good catch on the not returning thing. I mean to ask about that.

Copy link
Member

@styfle styfle left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@styfle
Copy link
Member

styfle commented Apr 10, 2018

@joshbruce I think this PR should land before the other PRs that involve the test harness.

Copy link
Member

@joshbruce joshbruce left a comment

Choose a reason for hiding this comment

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

Just want to verify the order of operations here. Before each, we set the options to defaults, the tests modify where needed. We are also making the defaults a getter only because we have the setter via setOptions.

@UziTech
Copy link
Member Author

UziTech commented Apr 10, 2018

Yes, marked.getDefaults() will always return our default options. marked.defaults is still overwritten by marked.setOptions (which I think we should change in 1.x)

We are resetting marked.defaults to marked.getDefaults() in a beforeEach that will run before any other beforeEach so we can still set options for tests in a beforeEach and it will work as expected.

@joshbruce joshbruce merged commit 9b2a49f into markedjs:master Apr 10, 2018
@UziTech UziTech deleted the reset-defaults branch April 10, 2018 15:07
zhenalexfan pushed a commit to zhenalexfan/MarkdownHan that referenced this pull request Nov 8, 2021
zhenalexfan pushed a commit to zhenalexfan/MarkdownHan that referenced this pull request Nov 8, 2021
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.

4 participants