Skip to content
This repository was archived by the owner on Sep 14, 2024. It is now read-only.

Add expect.extend #142

Merged

Conversation

benbrimeyer
Copy link
Collaborator

@benbrimeyer benbrimeyer commented Oct 1, 2020

Heavily influenced by https://jestjs.io/docs/en/expect#expectextendmatchers

Adds expect.extend which allow projects to add their own matchers to TestEZ.
Matchers are functions that should return an object with with two keys: boolean pass and a string message

-- spec.lua
describe("...", function()
  beforeAll(function()
    expect.extend({
      divisibleBy = function(receivedValue, expectedValue)
        local pass = receivedValue % expectedValue == 0
        if pass then
          return {
            pass = true,
            message = ("Expected %s to be divisible by %s"):format(receivedValue, expectedValue)
          }
        else
          return {
            pass = false,
            message = ("Expected %s not to be divisible by %s"):format(receivedValue, expectedValue)
          }
        end
      end,
    })
  end)

  it("should be an even number", function()
    expect(10).to.be.divisibleBy(2)
  end)
end)

This allows use cases to register their own, opinionated expectations that integrates into expect.

Cavets:

Since describe blocks are outside the usual test execution environment and exist primarily for planning, we do not have easy access to the test session's context, making supporting extend from describe blocks non-trival.
Realistically we may want to consider throwing a helpful error when users attempt to call expect from describe blocks, since these assertions should not occur during planning.

If a custom matcher is over written, we will throw. This is to help catch collisions in larger projects which would otherwise lead to subtle, confusing errors.

If a custom matcher tries to overwrite a default matcher or protected field of Expectation, it will throw.

@benbrimeyer benbrimeyer requested a review from MagiMaster October 1, 2020 05:33
@benbrimeyer benbrimeyer requested a review from MagiMaster October 1, 2020 22:50
@@ -76,7 +76,9 @@ function Expectation.new(value)
local self = {
value = value,
successCondition = true,
condition = false
condition = false,
matchers = {},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is matchers used? I don't see it anywhere else.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We use it once when we need to negate our expectation
https://github.com/Roblox/testez/pull/142/files#diff-c51d01b32afee27efe59f26103c1e704R129

We pass in the non-bound versions of the extend config.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, yep. There it is.

@benbrimeyer benbrimeyer merged commit f792e5b into Roblox:master Oct 1, 2020
@benbrimeyer benbrimeyer deleted the bbrimeyer/feature/extend-expectation branch October 1, 2020 23:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants