DongThin / my-love-e2e-testing

e2e testing with cypress
2 stars 0 forks source link

Assertion with Chai (BDD) #23

Open alang-dev opened 3 months ago

alang-dev commented 3 months ago

Goal:

To gain familiarity with ChaiJS's assertion styles and API by refactoring existing test code that relies on Node.js's built-in assert module.

Instructions:

Example (Before Refactoring):

const assert = require('assert');

assert.strictEqual(1 + 1, 2);
assert.deepStrictEqual({ a: 1 }, { a: 1 });
assert.ok('hello'.length > 0, 'String should not be empty');

// ... more tests ...

Example assertions.js (After Refactoring):

const expect = require('chai').expect; // Choose your preferred style

expect(1 + 1).to.equal(2);
expect({ a: 1 }).to.deep.equal({ a: 1 });
expect('hello').to.have.length.above(0, 'String should not be empty');
alang-dev commented 1 month ago

BDD (Behavior-Driven Development) BDD is a software development process that encourages collaboration between developers, QA, and non-technical or business participants in a software project. It extends Test-Driven Development (TDD) by writing test cases in a natural language that non-programmers can read.

In the context of JavaScript, Mocha is a popular testing framework that supports BDD. When using Mocha with Chai, tests are written in a style that is very readable and expresses the expected behavior of the code.

alang-dev commented 3 weeks ago

Here's a tabulated comparison of BDD and TDD:

Aspect Test-Driven Development (TDD) Behavior-Driven Development (BDD)
Focus Code correctness and implementation Expected behavior from the user's perspective
Process 1. Write a failing test
2. Write code to pass the test
3. Refactor
4. Repeat
1. Define expected behavior in plain language
2. Write a failing test
3. Write code to pass the test
4. Refactor
5. Repeat
Participants Primarily developers Collaboration between developers, testers, and business stakeholders
Language Technical, specific to the implementation Plain language, often using Given-When-Then format
Key Principles Red, Green, Refactor
Emphasizes early and continuous testing
Ubiquitous Language
Collaboration
Focus on behavior
Output Verified code functionality Verified application behavior
Typical Workflow Developer-centric Involves multiple stakeholders
alang-dev commented 3 weeks ago

Here are examples of tools commonly used for TDD and BDD in the Node.js ecosystem:

Aspect Test-Driven Development (TDD) Behavior-Driven Development (BDD)
Testing Frameworks Mocha, Jasmine, Jest Cucumber.js, Yadda, Jasmine with Jasmine-Given
Assertion Libraries Chai, Node.js built-in assert module Chai (can be used with BDD syntax)
Mocking Libraries Sinon.js Sinon.js (can be used in BDD)
Test Runners Mocha, Jest Cucumber.js, Jest (can be adapted for BDD)
Utilities Supertest (for HTTP assertions), Rewire (for mocking) Chai (with BDD syntax), Sinon.js (for mocks/stubs/spies)
Scenario Definition - Cucumber.js (using Gherkin language), Yadda (for BDD scenarios)
Continuous Integration GitLab CI, Jenkins, Travis CI, CircleCI GitLab CI, Jenkins, Travis CI, CircleCI