Learn JavaScript Unit Testing Flashcards

1
Q

Write Good Tests With Mocha

before() Hooks

A

In a test file, the function before() will be executed first, regardless of it’s placement in the code block. before() is often used to set up code, like variables and values, for other function calls to use in their execution.

before(() => {
   path = './message.txt';
 });
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write Good Tests With Mocha

beforeEach() Hook

A

In a test file, the function beforeEach() will be executed before each test. beforeEach() is often used to set up or reset code, like variables and values, for other function calls to use in their execution.

beforeEach(() => {
   testCounter++;
 });
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write Good Tests With Mocha

after() Hooks

A

In a test file, the function after() will be executed last, regardless of its placement in the code block. after() is often used to print out results from the tests that were run in the suite or to reset variables and values.

after(() => {
   console.log(""number of tests: "" + testCounter);
 });
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write Good Tests With Mocha

afterEach() Hooks

A

In a test file, the function afterEach() will be executed after each test. afterEach() is often used to print out results from a particular test that was run in the suite or to reset variables and values.

afterEach(() => {
   path = './message.txt';
 });
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write Good Tests With Mocha

Test Frameworks

A

Test frameworks are used to organize and automate tests that provide useful feedback when errors occur.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write Good Tests With Mocha

describe() functions

A

In Mocha, the describe() function is used to group tests. It accepts a string to describe the group of tests and a callback function which contains it() tests. Calls to describe() are commonly nested to resemble the structure of the code being tested.

describe('group of tests', () => {
  //Write it functions here
  
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write Good Tests With Mocha

it() functions

A

In Mocha, the it() function is used to execute individual tests. It accepts a string to describe the test and a callback function to execute assertions. Calls to it() are commonly nested within describe() blocks.

describe('+', () => {
  it('returns the sum of its arguments', () => {
    // Write assertions here
    
  });
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write Good Tests With Mocha

The assert Library

A

The assert library is used to make assertions. It contains numerous functions that enable the tester to write easily readable assertions and throw AssertionErrors within a test.

describe('+', () => {
  it('returns the sum of its arguments', () => {
    // Write assertion here
    assert.ok(3 + 4 === 7)
  });
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write Good Tests With Mocha

assert.ok()

A

The assert.ok() function is be used to evaluate a boolean expression within a test. If the expression evaluates to false, an AssertionError is thrown.

describe('+', () => {
  it('returns the sum of its arguments', () => {
    // Write assertion here
    assert.ok(3 + 4 === 7)
  });
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write Good Tests With Mocha

Setup Phase

A

In testing, the Setup phase is where objects, variables, and set conditions that tests depend on are created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write Good Tests With Mocha

Exercise Phase

A

In testing, the Exercise phase is where the functionality under test is executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write Good Tests With Mocha

Verify Phase

A

In testing, the Verify phase is where expectations are checked against the result of the exercise phase. assert would be used here.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write Good Tests With Mocha

Teardown Phase

A

In testing, the Teardown phase is where the environment is reset before the next test runs. The teardown phase ensures that a test is isolated from other tests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write Good Tests With Mocha

Tests in Isolation

A

A project’s tests should run in isolation from one another. One test shouldn’t affect another. Tests should be able to run in any order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write Good Tests With Mocha

assert.equal()

A

assert.equal() verifies a loose equality (==) comparison. Using assert.equal() is more expressive, it’s more clear that it’s verifying equality than assert.ok().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write Good Tests With Mocha

assert.strictEqual()

A

assert.strictEqual() verifies a strict equality (===) comparison.

const a = 3;
const b = '3';
assert.equal(a, b);
assert.strictEqual(a, b);
17
Q

Write Good Tests With Mocha

assert.deepEqual()

A

assert.deepEqual() compares values within two objects. It will compare the values using loose (==) equality.

const a = {relation: 'twin', age: '17'};
const b = {relation: 'twin', age: '17'};

assert.strictEqual(a, b);
18
Q

Write Good Tests With Mocha

Why Test?

A

Testing can catch and identify issues with your implementation code before you deploy it to users