Skip to content
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

Cannot mock ApplicationInsights in Jest tests #66

Closed
pviotti opened this issue Oct 22, 2018 · 4 comments
Closed

Cannot mock ApplicationInsights in Jest tests #66

pviotti opened this issue Oct 22, 2018 · 4 comments

Comments

@pviotti
Copy link

pviotti commented Oct 22, 2018

Hi,

I'm trying to use this plugin to write Jest mock tests for Application Insights, which does not support CommonJS. Unfortunately I can't manage to make it work: for some reason it throws a ReferenceError: define is not defined when importing Application Insights.

The code of my very small project is at this branch: https://github.com/pviotti/react-appinsights/tree/testing-babel-converter

Here's the relevant configuration (mostly taken from the example project in this repo):

jest.config.js

'use strict';

const webpackConfig = require('./webpack.config');

module.exports = {
    moduleNameMapper: webpackConfig.resolve.alias,
    transformIgnorePatterns: [
        "node_modules/(?!(applicationinsights-js)/)"
      ],
    setupTestFrameworkScriptFile:  "<rootDir>/test/setupTests.js"
}

webpack.config.js

'use strict';

const path = require('path');

module.exports = {
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'react-appinsights.bundle.js'
      },
    module: {
        rules: [
          {
            test: /.js$/,
            loader: 'babel-loader'
          }
        ]
    },
    resolve: {
      alias: {
        'applicationinsights-js': path.resolve(process.cwd(), './node_modules/applicationinsights-js/bundle/ai.module.js'),
      }
    }
};

.babelrc

{
  "presets": [ "@babel/preset-env", "@babel/preset-react"],
  "env": {
    "test": {
      "plugins": [
        "transform-amd-to-commonjs"
      ]
    }
}
}
@msrose
Copy link
Owner

msrose commented Oct 22, 2018

Hmm I'll take a closer look at this when I'm out of work (will probably have to inspect the source code of ApplicationInsights), but it's possible it will be addressed by #44, which has yet to be released. Take a look at the restrictToTopLevelDefine option as well and see if enabling it gives you a different error message. Once #65 is addressed I will cut a new release, so I'll work a bit faster to get that out if it turns out it's a blocker for you.

@msrose
Copy link
Owner

msrose commented Oct 22, 2018

Here's a link to the options for this plugin, with details about restrictToTopLevelDefine https://github.com/msrose/babel-plugin-transform-amd-to-commonjs#options

@msrose
Copy link
Owner

msrose commented Oct 23, 2018

I confirmed that the plugin is transforming the ApplicationInsights dist correctly, so you can disregard my comments from above. I figured it was something to do with your config, so I did a bit of digging and came across this issue jestjs/jest#6053 (comment).

I was able to get your tests passing by deleting the file in __mocks__(there are still some errors in there), renaming .babelrc to babel.config.js, and changing the babel.config.js contents so that it was exported:

module.exports={
  "presets": [ "@babel/preset-env", "@babel/preset-react"],
  "env": {
    "test": {
      "plugins": [
        "transform-amd-to-commonjs"
      ]
    }
}
}

Hope that helps; closing this issue because it isn't related to transforming AMD to commonjs.

@msrose msrose closed this as completed Oct 23, 2018
@pviotti
Copy link
Author

pviotti commented Oct 23, 2018

Awesome! Thank you so much for this and for the plugin! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants