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

Is it possible to use jest without ejecting in my case? #7100

Closed
cam-narzt opened this issue May 23, 2019 · 3 comments
Closed

Is it possible to use jest without ejecting in my case? #7100

cam-narzt opened this issue May 23, 2019 · 3 comments

Comments

@cam-narzt
Copy link

I have an app created with create-react-app, and wanted to add some tests. One component I am trying to test requires that I import 'webrtc-adapter';. When I run npm test jest complains that Jest encountered an unexpected token with the details in the code block below. From jestjs/jest#2081, I believe this is because jest isn't transforming the es6 import within the webrtc-adapter module into something it understands. The recommended solution in the other issue, as well as the one in the output from jest require modifying the jest config or the babel config which in turn requires ejecting the app so far as I can tell, however I'd like to avoid that if possible. So I'm asking if there is a way to get jest to work, without ejecting the app.

/Users/me/Developer/webrtcapp/node_modules/webrtc-adapter/src/js/adapter_core5.js:12
    import {adapterFactory} from './adapter_factory.js';

SyntaxError: Unexpected token {

      1 | import React, { Component } from 'react';
    > 2 | import 'webrtc-adapter';
        | ^
@mrmckeb
Copy link
Contributor

mrmckeb commented May 27, 2019

Hi @cam-narzt, that's correct. Basically, the author of that package needs to ship it as a CommonJS/UMD bundle, have export listed in package.json under main, and then include the ES module in the module field.

That way you get the best of both worlds, as Node/Jest will pick up the CJS module, and Webpack and other modern build tools can use the ES module.

If you don't need this as a part of your tests, you can mock it too. Simple create an __mocks__ directory in root, and create a file called webrtc-adapter that mocks anything that gets called during testing.

@mrmckeb mrmckeb closed this as completed May 27, 2019
@mrmckeb mrmckeb self-assigned this May 27, 2019
@cam-narzt
Copy link
Author

It seems like they do what you describe with the package.json file though: https://github.com/webrtcHacks/adapter/blob/master/package.json

@mrmckeb
Copy link
Contributor

mrmckeb commented May 29, 2019

@lock lock bot locked and limited conversation to collaborators Jun 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants