실행중인 GraphQL 백엔드 서버에 대해 내 릴레이 컨테이너의 통합 테스트를 구현하고 싶습니다. 나는 이것을 위해 Jest를 사용할 것이다. React 구성 요소의 단위 테스트가 Jest 설정으로 예상대로 잘 작동한다고 말하고 싶습니다. 다음은 내가 농담에 대한 package.json
에있는 내용은 다음과 같습니다작동중인 GraphQL 백엔드에 대해 Jest로 릴레이 컨테이너를 통합 테스트
"jest": {
"moduleFileExtensions": [
"js",
"jsx"
],
"moduleDirectories": [
"node_modules",
"src"
],
"moduleNameMapper": {
"^.+\\.(css|less)$": "<rootDir>/src/styleMock.js",
"^.+\\.(gif|ttf|eot|svg|png)$": "<rootDir>/src/fileMock.js"
},
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react/",
"<rootDir>/node_modules/react-dom/",
"<rootDir>/node_modules/react-addons-test-utils/",
"<rootDir>/node_modules/react-relay/"
]
}
여기 내가 사용하고있어 .babelrc
입니다 :
{
"presets": ["es2015", "react", "stage-0"],
"plugins": ["./babelRelayPlugin.js"]
}
여기 테스트 자체입니다. 'http://localhost:10000/q'GraphQL 끝점에 요청하여 현재 사용자 ('나')에 대한 정보를 나타내는 간단한 조각을 가져와야합니다.
jest.disableAutomock();
import React from 'react';
import Relay from 'react-relay';
import TestUtils from 'react-addons-test-utils';
import RelayNetworkDebug from 'react-relay/lib/RelayNetworkDebug';
RelayNetworkDebug.init();
Relay.injectNetworkLayer(
new Relay.DefaultNetworkLayer('http://localhost:10000/q')
);
describe('Me',() => {
it('can make request to /q anyway',() => {
class RootRoute extends Relay.Route {
static queries = {
root: (Component) => Relay.QL`
query {
root {
${Component.getFragment('root')}
}
}
`,
};
static routeName = 'RootRoute';
}
class AppRoot extends React.Component {
static propTypes = {
root: React.PropTypes.object,
};
render() {
expect(this.props.root).not.toBe(null);
expect(this.props.root.me).not.toBe(null);
expect(this.props.root.me.firstName).not.toBe(null);
expect(this.props.root.me.authorities[0]).not.toBe(null);
expect(this.props.root.me.authorities[0].authority).toEqual('ROLE_ANONYMOUS_AAA');
return (
<div>
{this.props.root.me.firstName}
</div>
);
}
}
const AppContainer = Relay.createContainer(AppRoot, {
fragments: {
root:() => Relay.QL`
fragment on Root {
me {
firstName
email
authorities {
authority
}
}
}
`,
},
});
const container = TestUtils.renderIntoDocument(
<div>
<Relay.RootContainer Component={AppContainer} route={new RootRoute()} />
</div>
);
expect(container).not.toBe(null);
});
});
문제는 테스트가 통과한다는 것입니다. 그러나 내 생각에 은 render()
expect(this.props.root.me.authorities[0].authority).toEqual('ROLE_ANONYMOUS_AAA');
안에이 줄에서을 실패해야합니다. render()
메서드가 전혀 실행되지 않는 것처럼 보입니다.
나는이 모든 모든 일을 생각합니까이
./node_modules/.bin/jest
처럼 농담을 실행하는거야?
감사합니다.
디버깅을 시도하여 어떤 컨테이너가 종료되는지 알아 냈습니까? 설명서에서는 반응을로드 할 때 사용할 수있는 DOM이 필요하다고 말하면서 DOM을 사용하고 있습니까? https://facebook.github.io/react/docs/test-utils.html#renderintodocument – brysgo
@brysgo Jest를 사용하고 있습니다. 그리고 내부적으로 jsdom을 사용합니다. 그것은 여기에 답변을 http://stackoverflow.com/questions/33383146/test-with-reactjs-renderintodocument-keep-failed-due-to-required-dom. 반복해서, 간단한 React 구성 요소의 테스트는 Jest의 튜토리얼에 따라 예상대로 작동합니다. 그러나 제가 질문 한 릴레이 테스트가 예상대로 작동하지 않습니다. – Grigory