A
A
Alexander2017-11-30 22:06:11
JavaScript
Alexander, 2017-11-30 22:06:11

Proper Javascript testing?

Hello! I wanted to learn more about testing in Javascript. I use mocha + chai. I'm running through gulp. And I would like to understand in more detail the types of tests:
Do unit tests assume dependence on other classes. That is, we will import a class into the test that will import other classes into itself? Is it possible to work with DOM in an imported class? Would this be considered a unit test? What then does an integration test include? Help, please, deal with the issue. Thank you all in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Konstantin Kitmanov, 2017-12-01
@k12th

That is, we will import a class into the test that will import other classes into itself?

You need to understand that a class does not import classes, import / export are modules. A class can instantiate other classes or accept instances as a constructor parameter, that's fine - in the second case, you just write mock: a stub that mimics the desired behavior in a predictable way. But if you export a class instance from a module, then there will be problems with testing. For example, this is bad:
// A.js
class A {}
export const a = new A();

// B.js
import { a } from './A';
export class B {
    constructor() {
        this.a = a;
    }

    doStuff() {}
};

Since class B implicitly depends on A, you can't stub A, and tests for B will test A's behavior.
This is better:
// A.js
export class A {};

// B.js
export class B {
    constructor(a) {
        this.a = a;
    }

    doStuff() {}
};

// index.js
import { A } from './A';
import { B } from './B';

const a = new A();
const b = new B(a);
b.doStuff();

If the test launcher supports it. For mocha, EMNIP, you need to fasten karma.
Unit-test, by definition, tests some kind of functional unit - a class, a module, a function, a method.

X
xfg, 2017-12-01
@xfg

If an object instantiates another object within itself, then such an object should be considered as a single unit.

import LineItem from './LineItem';

class Order {
  constructor(lineItems = []) {
    this.lineItems = lineItems;
  }
  addLineItem(name, cost, quantity) {
    this.lineItems.push(new LineItem(name, cost, quantity));
  }
}

In this case, you need to write a unit test for the Order class. There is no need to test the LineItem class, as it is part of the Order class. Such classes are called - Aggregate. When a car dealership invites you to test drive a car, this involves testing the Unit as a whole, that is, the Car. We will not test separately its component parts such as wheels / engine.
If an object accepts another object from outside, then such objects should be considered as separate units.
class Route {...}
class Ship {
  addRoute(route) {
   if (!(route instanceof Route)) {
     throw new Error('route must be instance of Route');
   }
    this.route = route;
  }
  ...
}

In this case, you need to write separate unit tests for Route and Ship. To write a unit test for the Ship class, it must be isolated from the Route class using a mock object.
The integration test implies that you will not be replacing Route with a mock object in order to test the Ship class. Instead, you must pass in a real implementation of the Route class. In this case, we will test the interaction between the two units Ship and Route.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question