2017-12-02 3 views
1

Ich studiere jetzt ReactJS zum ersten Mal.Schreibtest in facebookinkubator/create-react-app

Vom https://egghead.io/lessons/react-redux-writing-a-counter-reducer-with-tests

Kesselblech: facebookinculator/create-react-app
-Test unter Verwendung von: npm test

Der Hauptgrund, warum ich beschlossen, nicht auf die Ebene Online-Website verwenden praktizierenden zu tun ist, weil ich vertraut wollen mit echte Werkzeuge so viel wie ich kann. Hier ist mein Code.

export const counter = (state = 0, action) => { 
    switch (action.type) { 
    case 'INCREMENT': 
     return state + 1; 
    case 'DECREMENT': 
     return state - 1; 
    default: 
     return state; 
    } 
} 

Und mein utils.test.js

import React from 'react'; 
import counter from '../utils/utils'; 

it('expect 1 to be return from reducer',() => { 
    expect(counter(0, {type: 'INCREMENT'})).toEqual(1) 
}); 

Fehler:

FAIL src/utils/utils.test.js 
    ● expect 1 to be return from reducer 

    TypeError: (0 , _utils2.default) is not a function 

     at Object.<anonymous>.it (src/utils/utils.test.js:5:30) 
      at new Promise (<anonymous>) 
     at Promise.resolve.then.el (node_modules/p-map/index.js:46:16) 
      at <anonymous> 
     at process._tickCallback (internal/process/next_tick.js:188:7) 

PASS src/App.test.js 

Test Suites: 1 failed, 1 passed, 2 total 
Tests:  1 failed, 1 passed, 2 total 
Snapshots: 0 total 
Time:  0.062s, estimated 1s 
Ran all test suites. 

Watch Usage: Press w to show more. 

Frage:
Wo bin ich falsch? Ich habe schon mit Docs überprüfen. Auch ich hatte versucht expect(1).toEqual(1) zu bestätigen, dass meine Konfiguration korrekt ist.

Referenzen:
https://facebook.github.io/jest/docs/en/expect.html#toequalvalue

Antwort

1

Es sieht aus wie Ihre counter ein Import benannt ist zu einem Import Standard gegenüber.

Versuchen Sie, diese mit counter importieren:

import { counter } from '../utils/utils'; 

Die Syntax, die Sie zur Zeit nur verwenden funktioniert, wenn Sie Ihren counter als Standard für das utils Modul exportieren sind.

+1

Bitte warten Sie 3 Minuten. Vielen Dank für Ihre schnelle Antwort. – Sarit