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 mocha@3.1.2 chai@3.5.0 chai-http@3.0.0 --save-dev
npm install @types/mocha@2.2.32 @types/chai@3.4.34 @types/chai-http@0.0.29 --save-dev
npm install ts-node@3.3.0 --save-dev
npm install typescript@2.0.6 --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?
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.