speedskater / babel-plugin-rewire

A babel plugin adding the ability to rewire module dependencies. This enables to mock modules for testing purposes.
843 stars 90 forks source link

Everything is undefined #158

Closed screendriver closed 7 years ago

screendriver commented 7 years ago

I am using Webpack and Babel for my project. For my unit tests I created a dedicated webpack.test.config.js that runs my unit tests with Mocha.

Unfortunately I don't get it. All rewire properties are undefined in my tests.

package.json ``` json { "name": "me", "version": "1.0.0", "description": "", "main": "src/index.js", "scripts": { "devtest": "webpack-dev-server --config webpack.test.config.js", "dev": "webpack-dev-server --config webpack.config.js" }, "author": "", "license": "ISC", "devDependencies": { "babel-core": "^6.13.2", "babel-loader": "^6.2.4", "babel-plugin-rewire": "^1.0.0-rc-6", "babel-preset-es2015": "^6.13.2", "babel-preset-react": "^6.11.1", "babel-register": "^6.11.6", "chai": "^3.5.0", "css-loader": "^0.23.1", "file-loader": "^0.9.0", "image-webpack-loader": "^2.0.0", "mocha": "^2.5.3", "mocha-loader": "^0.7.1", "node-sass": "^3.8.0", "null-loader": "^0.1.1", "rewire": "^2.5.2", "rewire-webpack": "^1.0.1", "sass-loader": "^4.0.0", "sinon": "^1.17.5", "sinon-chai": "^2.8.0", "style-loader": "^0.13.1", "webpack": "^1.13.1" }, "dependencies": { "react": "^15.3.0" } } ```
webpack.test.config.js ``` js const path = require('path'); module.exports = { entry: 'mocha!./test/index.js', output: { path: path.resolve(__dirname, 'test'), publicPath: '/test/', filename: 'test.bundle.js' }, module: { noParse: [ /\/sinon\.js/, ], loaders: [{ test: /\.js$/, exclude: /node_modules/, loader: 'babel?plugins=babel-plugin-rewire', }] }, resolve: { alias: { sinon: 'sinon/pkg/sinon', } } }; ```
.babelrc ``` { "presets": ["es2015", "react"], "plugins": ["rewire"] } ```
src/index.js ``` js import { dummyFunc } from './mockme.js'; export function doIt() { return dummyFunc(); } ```
src/mockme.js ``` js export function dummyFunc() { return 'hello'; } ```
test/index.js ``` js // This will search for files ending in .test.js and require them // so that they are added to the webpack bundle const context = require.context('.', true, /.+\.test\.js?$/); context.keys().forEach(context); module.exports = context; ```
test/index.test.js ``` js import chai, { expect } from 'chai'; import { dummyFunc, __RewireAPI__, __Rewire__ } from '../src/mockme.js'; import { doIt } from '../src/index.js'; describe('index', () => { before(() => { // Properties are undefined console.log(dummyFunc.__RewireAPI__, dummyFunc.__Rewire__); }); it('should do anything', () => { }); }); ```

I could provide a sample project here on GitHub if you want.

speedskater commented 7 years ago

@screendriver Providing a sample project on Github woulgbe perfect. But I think i won't be able to get to tackle the issue before monday.

screendriver commented 7 years ago

All right. See here for more information https://github.com/speedskater/babel-plugin-rewire/issues/44#issuecomment-240663243

massimo-pacher-tw commented 7 years ago

@screendriver Hey, I'm using a similar configuration but running the test through mocka-webpack. I occurred in the same issue you have, but looking at the generated bundle, you can use the __Rewire__ import (rename it via as for more clarity).

So try __Rewire__('dummyFunc', () => {console.log('invoked')});

Hope it helps :)

speedskater commented 7 years ago

@screendriver thanks for the sample project. and @massimo-pacher-tw thanks for helping out. @screendriver I think I found your issue. In your sample project you tried to use the rewiring api on an es6 module which did not have anything to rewire (variables, functions, const variables, imports or classes defined at module top level and used somewhere in the same module). I provided a PR for your sample project which should result in a working sample. Could you please let me know if this solves your issue. If not please do not hesitate to reopen this issue.