2017-12-11 44 views
0

私はtypescriptでAPIテストを書こうとしています。'supertest'モジュールが見つかりません

私はこのように以下のパッケージをインストールした:

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 

私はこのようになりますtypescriptですコードがあります。

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); 
    }); 
}) 

をしかし、私が実行しようとすると、私はこのエラーを取得します:

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) 

なぜそれがsupertestを見つけることができないのか分かりません。ご覧のとおり、私は可能な限りあらゆる方法でsupertestをインストールしようとしました...また、私の 'node_modules'フォルダに 'supertest'というフォルダがあります。

思考?

答えて

1

バージョン3.0.0以前のSupertestは、ES6をサポートしていません。 GitHubには約issueがあります。

回避策もあります。

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 
} 

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

クレジットがfoxgaocnに行くの問題が消える場合には、私はそれをここに投稿します。

+0

ありがとうございます! インポートコマンドを「var supertest = require( "supertest");」に変更した後、 – user952342

関連する問題