How to pass an object to a mocha test?

by mallory_cormier , in category: Javascript , 4 months ago

How to pass an object to a mocha test?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by herminia_bruen , 4 months ago

@mallory_cormier 

To pass an object to a Mocha test, you can simply define the object as a variable in the test file and reference it within your test cases. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// test.js

const assert = require('assert');

// Define the object to be passed to the test
const obj = {
  name: 'John',
  age: 30
};

describe('Object test', () => {
  it('should have a name property', () => {
    assert.strictEqual(obj.name, 'John');
  });

  it('should have an age property', () => {
    assert.strictEqual(obj.age, 30);
  });
});


In the above example, we defined an object obj with properties name and age, and then used that object within our test cases to make assertions. You can define the object directly in the test file or import it from another module if needed.