node.jsunit-testingnestjstap

test .ts class by node-tap


I have a class : company-class.ts

export class CompanyRequest {
        async getCompanies(): Promise<any> {
        const result= await this.request(this.reqBody);

        return result;
    }

}

I use node-tap for testing mytest.ts

import { CompanyRequest } from "../src/Requests/company-requests";
const tap = require('tap')
tap.test('get compmies', async t => {  
  const req = new CompanyRequest();
  const res = await req.getCompanies();
  t.equal(0,0);
})

Finally, I get this error SyntaxError: Cannot use import statement outside a module

If not use Company Request test is true, like this

const tap = require('tap')
tap.test('get compmies', async t => {  
  t.equal(0,0);
})

Solution

  • install tap globally: npm i -g tap after that, put this code on package.json

    "test": "tap --node-arg=--require=ts-node/register",
    

    run => npm run test