JavaScript Testing for Pythonistas (Summary)

Kirsty’s pytest suite now has a JavaScript twin. fitting.test.js checks Morag’s skis, boots, and binding release setting (DIN), loads her booking from a file, and confirms that a reservation holds both skis and boots. It passes when you run it against the supplied fitting.js and fails when one of that file’s rules breaks, such as a height left as text or a weight band off by one.

Carry Each pytest Habit Across

Here’s each pytest habit next to the JavaScript you now write in its place:

pytest node:test and node:assert/strict
test_fitting.py, found by pytest fitting.test.js, found by node --test
assert a == b assert.strictEqual(a, b) for one value, assert.deepStrictEqual(a, b) for objects
with pytest.raises(FittingError, match=...) assert.throws(() => call(), { name, message: /.../ }), or FittingError in place of the object to check the class
@pytest.mark.parametrize a loop of test() calls inside describe()
a fixture parameter beforeEach() setting a let variable
tmp_path cleanup t.after() with rm(dir, { recursive: true })
async def test with pytest-asyncio an async test and await assert.rejects(promise, ...)
monkeypatch plus Mock t.mock.method(inventory, "hold", ...) and .mock.calls

Drop the Habits That Don’t Port

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.