简体   繁体   中英

Testing async methods using Mocha, Chai, node.js

I have a very simple code structure like this

TestWorks.ts

const axios = require('axios');

export class TestWorks{

    async getUsersList(param1:TestModel, userDetail:any){

    console.log("BEGIN -- ... ");

And then this is my test class

MyTest.ts

const testworks = require("../src/interfaces/TestService/TestWorks");

it('Get Users', async () => {
    var x = await testworks.getUsersList({}, {});
    expect(x).to.be.an("object");
});

but I am seeing the following error, unable to figure out what the issue could be. The paths are definitely right, not an issue with the file paths of where the files are

Get Users:
     TypeError: testworks.getUsersList is not a function
     at C:\Users\xxxxxx\Documents\xxxxx\test\test-server.test.ts:53:28

testworks refers to the module (or whatever TypeScript exports) because you use require() . You should use import for TypeScript modules.

import { TestWorks } from '../src/interfaces/TestService/TestWorks';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM