unit test

X-BUILD uses Jest as a unit testing framework, and it also enables code coverage testing.

Instructions for use

Test file path and naming convention

  • All test files should be placed in tests/ or __tests__ directory.Tests/is automatically generated by default.
  • The test file names are [filename].spec.[js|jsx|ts|tsx].

Case

Create a file reverse.js under src/script/ to reverse integers.

// reverse.js
function reverse (x) {
  let result = (x> 0? 1: -1) * String (x) .split (''). filter (item => item! == '-'). reverse (). join ('');
  return result;
}
export default reverse;

Create a reverse.spec.js test file under the tests/ folder.

// reverse.spec.js
import reverse from '@ / scripts / reverse.js'

describe ('Integer inversion', () => {
  test ('reverse 321 to 123', () => {
    expect (reverse (321)). toBe (123);
  });
  test ('reverse -321 to -123', () => {
    expect (reverse (-321)). toBe (-123);
  });
});

Run the command npm run test to get the unit test and code coverage test results: