Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find module 'supertest'

I am trying to write API tests in typescript.

I've installed the following packages like this:

npm install -g mocha
npm install chai
npm install supertest
npm install supertest --save-dev
npm install supertest -g
npm install [email protected] [email protected] [email protected] --save-dev
npm install @types/[email protected] @types/[email protected] @types/[email protected] --save-dev
npm install [email protected] --save-dev
npm install [email protected] --save-dev

I have typescript code that looks like this:

import { expect } from 'chai';
import * as supertest from 'supertest';
import 'mocha';

var api = supertest('http://petstore.swagger.io');

describe('API test', () => {
  it('should return a 200 response from petstore', function (done) {
    api.get('/v2/store/inventory')
      .set('Accept', 'application/json')
      .expect(200, done);
  });
})

However, when I try to run, I get this error:

mocha -r ts-node/register src/**/branches.ts  
C:\project\node_modules\ts-node\src\index.ts:307
        throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset))
              ^
TSError: ⨯ Unable to compile TypeScript
src\API\branches.ts (5,28): Cannot find module 'supertest'. (2307)

I don't understand why it cannot find supertest. As you can see, I tried installing supertest in every way possible... Also, I do have a folder in my 'node_modules' folder called 'supertest'.

Thoughts?

like image 920
user952342 Avatar asked Oct 18 '25 16:10

user952342


1 Answers

Version 3.0.0 and earlier of Supertest doesn't support ES6. There is an issue about that on GitHub.

There is also a workaround posted there. I'll post it here in case the issue disappears:

test_helper.js:

import sinon from 'sinon'
import chai from 'chai'
import sinonChai from 'sinon-chai'
var expect = chai.expect
chai.use(sinonChai)

var supertest = require("supertest");

module.exports = {
  sinon: sinon,
  chai: chai,
  expect: expect,
  supertest: supertest
} 

In your testing file:

import {sinon, chai, expect, supertest} from '../test_helper'

Credit goes to foxgaocn.

like image 185
Mika Sundland Avatar answered Oct 20 '25 08:10

Mika Sundland