-
Notifications
You must be signed in to change notification settings - Fork 29
Question: Any ideas on how to transpile only one file at a time? #1
Description
I'm using the compiler solution you've posted pretty much as is, it works great and stubs as expected. But, if you run two tests, one of which is stubbed and one of which is the actual test for what you may have stubbed out in previous test, then the actual component for the test has already been stubbed out globally, so the second test will fail.
For example, here's spec1 and spec2. spec1 tests the HeaderNav component, which includes the LoginStatus component. Its stubbed out properly, in the first test, but then when spec2 starts, LoginStatus is already stubbed, compiled and cached.
//mocha.opts
test/specs/*.js
--recursive
--compilers jsx:test/jsx-compiler.js
--reporter spec//spec1.js
describe('Spec 1', function() {
it('renders the HeaderNav class', function() {
(function(){
global.reactModulesToStub = ['LoginStatus.jsx'];
})();
var MyComponent = require('../../app/modules/HeaderNav.jsx'),
TestContext = require('./../lib/TestContext').getRouterComponent(MyComponent),
component = TestContext.component;
console.log(global.document.body.innerHTML);
TestUtils.findRenderedDOMComponentWithClass(
component, 'HeaderNav');
});
it('1 x 1', function() {
assert.equal(1, (1 * 1), '1 x 1 = 1');
});
});
//spec2.js
describe('Spec 2', function() {
it('renders LoginStatus', function() {
var MyComponent = require('../../app/modules/LoginStatus.jsx'),
TestContext = require('./../lib/TestContext').getRouterComponent(MyComponent),
component = TestContext.component;
console.log(global.document.body.innerHTML);
TestUtils.findRenderedDOMComponentWithClass(
component, 'LoginStatus');
});
it('1 x 1', function() {
assert.equal(1, (1 * 1), '1 x 1 = 1');
});
});
//HeaderNav.jsx
var React = require('react');
var Link = require('../modules/Link');
var paths = require('../helpers/paths');
var LoginStatus = require('./LoginStatus');
var SitemapNav = require('./SitemapNav');
var HeaderNav = React.createClass({
displayName: 'HeaderNav',
render: function() {
return (
<div className="HeaderNav">
<header className="header-wrapper">
<Link className="header-logo" to={paths.FrontPage} />
<nav className="header-nav hidden-sm">
<Link className="header-nav-link" to={paths.SearchPage}> This </Link>
<Link className="header-nav-link" to={paths.SearchPage}> That </Link>
<Link className="header-nav-link" to={paths.UserItemsPage}> Other </Link>
</nav>
<LoginStatus />
</header>
</div>
);
}
});
module.exports = HeaderNav;LoginStatus needs to be tested on its own, but stubbed out when included in HeaderNav. This is the most real world solution I have come across. So figuring it out would be huge for React testing with React-Router, Flux, etc. I didn't post LoginStatus, but its a JSX and outputs a simple component, but requires React-Router and Flux, hence the TestContext, which adds that stuff.
But the real problem is that I don't want to stub LoginStatus for my second test. I've added an afterEach to the mocha helper, but that does nothing, because using require.extensions loads all .jsx modules up front.
afterEach(function(){
global.reactModulesToStub = [];
});