Skip to content

Codewars JavaScript Test Framework

farhan443 edited this page Feb 27, 2022 · 5 revisions

DEPRECATED Do not use this for new kata. Use Mocha + Chai instead. See JavaScript Authoring doc, and JavaScript page.

Basic Setup

describe("Example Tests", function() {
  it("Example Test Case", function() {
    Test.assertEquals(add(1, 1), 2, "optional message");
  });
});

Assertions

Test.assertEquals(actual, expected[, msg])

Checks that the actual value equals (===) the expected value.

Test.assertNotEquals(actual, unexpected[, msg])

Checks that the actual value does not equal (!==) the unexpected value.

Test.assertSimilar(actual, expected[, msg])

Checks that the actual value equals (===) the expected value.

Test.inspect is used to wrap the values being tested, allowing for similar values to be considered the same.

Test.assertNotSimilar(actual, unexpected[, msg])

Checks that the actual value does not equal (!==) the unexpected value.

Test.inspect is used to wrap the values being tested, allowing for similar values to be considered the same.

Test.assertDeepEquals(actual, expected[, msg])

Checks that the actual value equals the expected value by performing deep comparison.

Unlike Test.assertSimilar, values are not turned into strings.

Test.assertNotDeepEquals(actual, unexpected[, msg])

Checks that the actual value does not equal the unexpected value by performing deep comparison.

Unlike Test.assertNotSimilar, values are not turned into strings.

Test.assertApproxEquals(actual, expected[, msg])

Compares two floating point values and checks whether they are approximately equal to each other.

Test.assertNotApproxEquals(actual, expected[, msg])

Compares two floating point values and checks whether they are sufficiently different from each other.

Test.assertContains(actual, expected[, msg])

Checks that the actual value contains the expected element.

Test.assertNotContains(actual, unexpected[, msg])

Checks that the actual value does not contain the unexpected element.

Test.expectError([msg, ]fn)

Checks that fn throws.

Test.expectNoError([msg, ]fn)

Checks that fn does not throw.

Test.expect(passed[, msg])

Core assertion method testing if passed is truthy.

Utilities

Test.inspect(object)

Returns a string representation of the object.

Test.randomize(array)

Returns a shuffled array (new array).

Test.randomNumber()

Returns a random integer between 0-100 (inclusive).

Test.randomToken()

Returns a random string of characters.

  • Length range: between 1-9 (inclusive)
  • Character range: digits (0-9) and lowercase letters (a-z)

Test.sample(array)

Returns a single, randomly chosen item from an array.

Clone this wiki locally