-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rewire does not work with babel #62
Comments
Same as jhnns/rewire-webpack#12 This is not an ES6 issue, but caused by the way how babel emulates the new module system. |
Further insights: babel/babel#1337 Unfortunately, it's not easy to solve, because it requires a major rewrite. And tbh: I'm not very motivated, because babel's change, which is causing the problem, was driven by the misconception |
👍 for being honest and saying it's not motivating. |
Definitely 👍 The tests, however, should stay the same (and they should still run afterwards). Since it would be a great rewrite, I'd appreciate if you could do this in close consultation. @speedskater has already contacted me. |
Is anybody working on this? |
It looks like somebody did: https://github.com/speedskater/babel-plugin-rewire |
Just landed here after headbutting my code for a while and then looking for information on this. Wondering if it would be a good idea to have a paragraph about this in the readme to avoid people trying to do this with rewire in the first place. I can submit a PR :) |
@trodrigues you can do that. However, it should not sound like it's impossible to use rewire with ES2015 modules. I just have to figure out a good way how to hook-in rewire. I'll probably also have to refactor the public API so that rewire "ports" like rewireify are easier to integrate (also see #78) |
Right. But I think the main issue here is not ES6 modules in themselves but transpiled ES6 modules. I think that's always going to be a case specific to babel users. |
I think I might be having a similar issue.
This seems to happen only when I webpack my test files and their es6 deps and run them in mocha / node. I don't see any issues when I run the same tests thru my karma config and run them in the browser. The funny thing is that the webpack config should be about the same for both karma and mocha. I'll check for differences, but any suggestions would be greatly appreciated. Thanks! |
Just FYI, my approach to 'fix' this is: import _config from '../config';
let config = _config; Then rewire as normal: const thing = rewire('./path/to/thing');
thing.__set__('config', testConfig); It's a little dirty as I don't like changing production code for test purposes. I normally add a comment like |
@jamlen Thank you - that really solves my problem for now! |
@jamlen - pardon my ignorance, but is the rewire('./path/to/thing') the workaround for rewire('../config')? |
@pilphil the So if you have:
and you are writing the test file |
Is this the right place/issue to ask about support for mocking out calls to |
@robrtmain you could check out this answer of mine if stackoverflow... |
@jamlen that's fine but it looks like you're using I know it works with |
@robertmain I have some examples somewhere... let me dig them out :) it does work (although I wasn't using TypeScript) |
My class looks like this: import { EventEmitter } from 'events';
import _logger from './logger';
import { stock as mappers } from './mappers';
import { stock as _StockStore} from './stores';
let StockStore = _StockStore;
let logger = _logger;
let store, _debug;
/** StockMigrator */
module.exports = class StockMigrator extends EventEmitter {
/** Create a StockMigrator. */
constructor () {
super();
store = new StockStore();
store.on('write_data', (type, key, duration) => {
this.emit('progress', 'write', key, duration);
});
}
// other methods here...
} My tests look like this: import rewire from 'rewire';
import deride from 'deride';
const Migrator = rewire('../../../src/stockMigrator');
import { stock as mappers } from '../../../src//mappers';
import data from '../../data/stock';
import config from '../../config';
describe('Unit tests', () => {
describe('Stock Migration', () => {
let migrator, store;
describe('stockMigrator', () => {
before(() => {
store = deride.stub([ 'writeDocuments' ]);
store.setup.writeDocuments.toDoThis(() => {
store.emit('status', 'write_data', 'doc123');
return Promise.resolve();
});
Migrator.__set__({
StockStore: () => store,
logger: deride.stub(['debug', 'info', 'warn', 'error', 'audit'])
});
migrator = new Migrator();
});
// describes here... |
@jamlen so you got |
@robertmain Yes, but with the caveat that you need to add a |
ah! |
@jamlen The problem I'm running up against at the moment with this in TypeScript is that it seems I have to load the thing I'm testing with rewire....which doesn't return the correct kind of object (it has no constructor) and then the ts language server gets upset. |
@robertmain at a guess I'd say this is a TS specific issue and probably raise a new issue for it. |
Using
|
ES6 modules (compiled with babel) don't work with rewire.
The text was updated successfully, but these errors were encountered: