Skip to main content

JavaScript

Versions

  • Node 8.x
  • Node 10.x
  • Node 12.x
  • Node 18.x

Test Frameworks

Starting from Node 10.x, Mocha is used instead of our custom test framework. Codewars' assertion methods under Test are still defined. However, those are wrappers around Chai assertions for backwards compatibility. Any new tests should not use them and existing tests should consider using Chai directly.

Starting from Node 12.x, Test is no longer defined. For backwards compatibility, you can import a package: const Test = require("@codewars/test-compat");. Any new tests should use Chai instead.

const chai = require("chai");
// const assert = chai.assert;
// const expect = chai.expect;
const chai = require("chai");
// const assert = chai.assert;
// const expect = chai.expect;

If the failure output for deep equality is truncated, chai.config.truncateThreshold can be adjusted. Setting this to 0 will disable the truncation.

If you're creating a kata, try inserting an example in the kata editor to get started.

Example for Node v10+

const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold=0;

describe("Example", function() {
  it("should test", function() {
    assert.strictEqual(1 + 1, 2);
    assert.deepEqual([2,2], [2,-(-2)]);
  });
});
const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold=0;

describe("Example", function() {
  it("should test", function() {
    assert.strictEqual(1 + 1, 2);
    assert.deepEqual([2,2], [2,-(-2)]);
  });
});

Each it represents a test case and stops at the first assertion failure.

If you want to have a test case for each input, you can write tests like the following:

// [input, expected]
const tests = [
  ["foo", "Foo"],
  ["bar", "Bar"],
];
describe("Example", function() {
  for (const [input, expected] of tests) {
    it(`input: ${JSON.stringify(input)}`, function() {
      assert.strictEqual(example(input), expected);
    });
  }
});
// [input, expected]
const tests = [
  ["foo", "Foo"],
  ["bar", "Bar"],
];
describe("Example", function() {
  for (const [input, expected] of tests) {
    it(`input: ${JSON.stringify(input)}`, function() {
      assert.strictEqual(example(input), expected);
    });
  }
});

When the input is too much for a test case name, you can output it inside a test case:

describe("Example", function() {
  for (let i = 0; i < tests.length; ++i) {
    it(`example test ${i + 1}`, function() {
      const [input, expected] = tests[i];
      console.log(`<LOG::-input>${JSON.stringify(input)}`);
      assert.strictEqual(example(input), expected);
    });
  }
});
describe("Example", function() {
  for (let i = 0; i < tests.length; ++i) {
    it(`example test ${i + 1}`, function() {
      const [input, expected] = tests[i];
      console.log(`<LOG::-input>${JSON.stringify(input)}`);
      assert.strictEqual(example(input), expected);
    });
  }
});

Prefixing with <LOG::-LABEL> will put the message inside a container with LABEL. The - makes it collapsed by default.

Timeout

12 seconds

Node Modules

NOTE: Module versions are locked but may be updated. If an update happens, existing kata may need to be manually updated.

  • bignumber.js
  • bluebird
  • brain
  • canvas
  • chai
    • chai-http
    • chai-spies
  • cheerio
  • enzyme
  • escape-html
  • expect
  • express
  • faker
  • jsdom
  • lodash
  • mocha
  • moment
  • mongodb
  • mongoose
  • pg
  • q
  • ramda
  • react
    • react-addons-test-utils
    • react-dom
    • react-redux
    • react-test-renderer
  • redis
  • redux
  • should
  • sinon
  • sqlite3
  • underscore
  • web3
    • ganache-core

Services

  • sqlite3
  • redis
  • mongodb

Language ID

javascript