Jest: how can I import core-js automatically?

Created on 13 Apr 2015  路  5Comments  路  Source: facebook/jest

var result = categories.find(ct => ct.id() === categoryId);    

when I run jest, I got a error what - TypeError: Object # has no method 'find'

I know that I should import core-js to solve that error but he fact that I should import core-js every time using jest bothers me a lot.

What I want is to load core-js automatically when I run jest.
Is loading core-js automatically implemented already? or should I fork it and pull request.

Please let me know :)

Most helpful comment

For anyone that lands on this these days, the modern day fix is have a setupFiles setting in your package.json:

    "setupFiles": [
      "<rootDir>/internal/tests/browserMocks.js"
    ],

In the browserMocks.js file include this require statement at the top:

require('core-js');

All 5 comments

You can load polyfills into the environment by using config.setupEnvScriptFile and then require.requireActual (I think to make sure it's not mocked) from there.

See what we do in jstransform: https://github.com/facebook/jstransform/blob/master/package.json#L42 & https://github.com/facebook/jstransform/blob/master/jestEnvironment.js

@zpao thank you a lot

For anyone that lands on this these days, the modern day fix is have a setupFiles setting in your package.json:

    "setupFiles": [
      "<rootDir>/internal/tests/browserMocks.js"
    ],

In the browserMocks.js file include this require statement at the top:

require('core-js');

For anyone who's too lazy to create a browserMocks.js file, you can set core-js directly in your package.json:

    "setupFiles": [
      "<rootDir>/node_modules/core-js"
    ]

If you'd like to be even lazier you can use the name only, too:

"setupFiles": ["core-js"]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

StephanBijzitter picture StephanBijzitter  路  3Comments

nsand picture nsand  路  3Comments

kentor picture kentor  路  3Comments

stephenlautier picture stephenlautier  路  3Comments

ianp picture ianp  路  3Comments