3

I have a config.ts which returns an object:

// Config is an interface that I use to know which values are expected
export default function getConfig(): Config {
     return {amount: 50}
}

I have a class (../src/models/item.model) that has a dependency to the config.ts:

import getConfig from '../config/config';

class Item{
    _id: number;
    amount: number;

    constructor(_id: number) {
        this._id = _id;
        this.amount = getConfig().amount;
    }
}

export default Item

I would like to write some tests with a different amount value. The default value is 50 (set in config.ts), but in my item.test.ts I would like to use a value of 100. I'm trying to achieve this by using Proxyquire:

it('should use voxelsize of custom config', (done) => {
    const itemModel = proxyquire('../src/models/item.model', {
        '../config/config': function getConfig() {
            return {amount: 100};
        }
    }).default;

    const testItem = new itemModel(1)

    expect(testItem.amount).to.equal(100);
    done()
})

testItem.amount is in reality 50 (so it still uses the original configuration file). This should be 100.

How can I let the test pass?

1 Answer 1

2

You are using es6 export default function getConfig() {}, so you should assign the mocked getconfig() function to the default attribute of the ./config commonJS module.

E.g.

config.ts:

export default function getConfig() {
  return { amount: 50 };
}

item.model.ts:

import getConfig from './config';

class Item {
  _id: number;
  amount: number;

  constructor(_id: number) {
    this._id = _id;
    this.amount = getConfig().amount;
  }
}

export default Item;

item.model.test.ts:

import { expect } from 'chai';
import proxyquire from 'proxyquire';

describe('66691249', () => {
  it('should use voxelsize of custom config', () => {
    const itemModel = proxyquire('./item.model', {
      './config': {
        default: function getConfig() {
          return { amount: 100 };
        },
      },
    }).default;

    const testItem = new itemModel(1);
    expect(testItem.amount).to.equal(100);
  });
});

test result:

  66691249
    ✓ should use voxelsize of custom config (1742ms)


  1 passing (2s)

---------------|---------|----------|---------|---------|-------------------
File           | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------|---------|----------|---------|---------|-------------------
All files      |   83.33 |      100 |      50 |   83.33 |                   
 config.ts     |      50 |      100 |       0 |      50 | 2                 
 item.model.ts |     100 |      100 |     100 |     100 |                   
---------------|---------|----------|---------|---------|-------------------
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.