2017-08-09 51 views
1

我有使用Jest with Enzyme進行打字稿測試。測試工作完美,但是當我添加一個組件,它給了我一個Unterminated regular expression錯誤Jest with Enzyme打字稿測試不識別組件

import {} from 'jasmine'; 
import {shallow} from 'enzyme'; 
import {Show, Hide} from '../components/show_hide'; 

describe('<Show /> Tests',() => { 
    it('Show should render once',() => { 
     const component = shallow(<Show />); 
     expect(component).toHaveLength(1); 
    }) 
}) 

當我運行yarn test控制檯上的結果是

FAIL __tests__/show_hide.ts 
    ● Test suite failed to run 

    /path/to/repo/__tests__/show_hide.ts: Unterminated regular expression (4:35) 
     2 | describe('<Show /> Tests',() => { 
     3 |  it('Show should render once',() => { 
     > 4 |   const component = shallow(/>);, expect(component).toHaveLength(1)); 
      |         ^
     5 |  }); 
     6 | }); 
     7 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2hvd19oaWRlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vX190ZXN0c19fL3Nob3dfaGlkZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFDQSxPQUFPLEVBQUMsT0FBTyxFQUFDLE1BQU0sUUFBUSxDQUFDO0FBRy9CLFFBQVEsQ0FBQyxnQkFBZ0IsRUFBRTtJQUMxQixFQUFFLENBQUMsbUJBQW1CLEVBQUU7UUFDdkIsTUFBTSxTQUFTLEdBQUcsT0FBTyxDQUFPLElBQUksRUFDcEMsTUFBTSxDQUFDLFNBQVMsQ0FBQyxDQUFDLFlBQVksQ0FBQyxDQUFDLENBQUMsQ0FBQSxDQUFDO0lBQ25DLENBQUMsQ0FBQyxDQUFBO0FBQ0gsQ0FBQyxDQUFDLENBQUEifQ== 

package.json的玩笑配置

"jest": { 
    "transform": { 
     "^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js" 
    }, 
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$", 
    "moduleFileExtensions": [ 
     "ts", 
     "tsx", 
     "js", 
     "json" 
    ], 
    }, 

對此有何看法?

回答

0

似乎忘記導入React導致了這個問題。

3

ts文件用於純打字稿代碼,tsx用於反應(如jsx)。 將文件重命名爲tsx