Skip to content

Instantly share code, notes, and snippets.

@skelet00r
Created April 6, 2022 06:11
Show Gist options
  • Select an option

  • Save skelet00r/e7cc9874b661b14843ed33c962b8cdd7 to your computer and use it in GitHub Desktop.

Select an option

Save skelet00r/e7cc9874b661b14843ed33c962b8cdd7 to your computer and use it in GitHub Desktop.

Revisions

  1. James Delibas created this gist Apr 6, 2022.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # proxyquire with typescript

    Exmaple on how to get proxyquire working with typescript
    53 changes: 53 additions & 0 deletions adapter.test.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    import anyTest, { TestInterface } from 'ava';
    import sinon, { SinonStub } from 'sinon';
    import proxyquire from 'proxyquire';

    type AxiosStub = {
    get: SinonStub;
    }

    type Context = {
    axios: AxiosStub;
    fn: Function;
    };

    const test = anyTest as TestInterface<Context>;

    test.before((t) => {
    t.context.axios = {
    get: sinon.stub(),
    }

    t.context.fn = proxyquire('./adapter', {
    'axios': t.context.axios
    }).default;
    });

    test('should correctly mock axios', async (t) => {
    // Arrange
    const expected = { some: 'data '}
    t.context.axios.get.resolves({ data: expected });

    // Act
    const result = await t.context.fn();

    // Assert
    t.deepEqual(result, expected);
    });

    test('should correctly handle error', async (t) => {
    // Arrange
    const expected = {
    instanceOf: Error,
    message: 'Something failed here',
    };

    t.context.axios.get.rejects({ data: expected });

    // Assert
    await t.throwsAsync(
    // Act
    t.context.fn(),
    expected,
    );
    });
    13 changes: 13 additions & 0 deletions adapter.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    import axios from 'axios';

    const adapter = async () => {
    try {
    const { data } = await axios.get('/')

    return data
    } catch(err) {
    throw new Error('Something failed here')
    }
    }

    export default adapter;
    32 changes: 32 additions & 0 deletions index.test.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    import anyTest, { TestInterface } from 'ava';
    import sinon, { SinonStub } from 'sinon';
    import proxyquire from 'proxyquire';

    type Context = {
    adapterStub: SinonStub;
    fn: Function;
    };

    const test = anyTest as TestInterface<Context>;

    test.before((t) => {
    t.context.adapterStub = sinon.stub()

    t.context.fn = proxyquire('./index', {
    './adapter': {
    default: t.context.adapterStub
    },
    }).default;
    });

    test('should correctly mock the adapter call', async (t) => {
    // Arrange
    const expected = { some: 'data '}
    t.context.adapterStub.resolves({ data: expected });

    // Act
    const result = await t.context.fn();

    // Assert
    t.deepEqual(result, expected);
    });
    8 changes: 8 additions & 0 deletions index.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    import adapter from './adapter';

    const demo = async () => {
    const { data } = await adapter();
    return data;
    }

    export default demo;
    28 changes: 28 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    {
    "name": "proxyquire-typescript",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
    "test": "tsc && nyc ava"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "devDependencies": {
    "@types/node": "14.14.22",
    "@types/proxyquire": "1.3.28",
    "@types/sinon": "10.0.2",
    "ava": "3.5.0",
    "nock": "13.0.5",
    "nyc": "15.1.0",
    "proxyquire": "2.1.3",
    "sinon": "11.1.1",
    "ts-loader": "8.0.14",
    "ts-node": "9.1.1",
    "typescript": "4.1.3"
    },
    "dependencies": {
    "axios": "0.26.1"
    }
    }