Import Requests for babel-jest ES2015

Has anyone written jasmine / jest tests using es2015 syntax? how much shimming / polyfill / gerrymandering does it require?

im failed to import functions correctly. I have one module: .... / utils / TweetUtils.js

'use strict'; export function getListOfTweetIds (tweets) { return Object.keys(tweets); }; 

and one test package:

.... / __ tests __ / TweetUtils-test.js

 'use strict'; jest.dontMock('../TweetUtils'); import * as TweetUtils from '../TweetUtils'; describe('Tweet utilities module', () => { it('has access to the TweetUtils methods', () => { let testObj = {a:'a',b:'b',c:'c'}; // Passes expect(TweetUtils.getListOfTweetIds).toBeDefined(); // Passes expect(typeof TweetUtils.getListOfTweetIds).toBe('function'); // Fails expect(TweetUtils.getListOfTweetIds(testObj)).toBeTruthy(); }); }); 

If I crack the console output into a package with something like this: expect('').toBe(TweetUtils);

Jasmine reports this:

 - Expected: '' toBe: { default: { getListOfTweetIds: Function }, getListOfTweetIds: Function } 

So, it looks like the import statement is doing something, but is not explicitly importing my methods honestly. I get the same results when importing using a syntax with a named function: import {getListOfTweetIds} from '../TweetUtils'; But if I use the default syntax: import getListOfTweetIds from '../TweetUtils'; The second spec doesn't work - its no longer a typeof function , but a typeof object // => {default: Function}

I combed documents and open problems. There have been related problems for several months, but known problems do not seem to be correct. Ive tried to import my jest.dontMock instructions to avoid getting up, around: https://github.com/babel/babel-jest/issues/16 , but not cubes.

Everything works if I modify TweetUtils.js to use module.exports = function… and transfer it to the set using const myFunction = require('../TweetUtils') , but it doesn’t feel like I'm imposing the true magic of ES2015. Are all dealing with erratic workarounds now while the ecosystem is catching up with new syntax?

+5
source share
1 answer

As you said, import statements are raised, and this causes problems with the joke auto-recognition function (the module is imported before , you say the joke has disabled it).

TweetUtils.getListOfTweetIds correctly imported, but it mocks, so every call returns undefined . That is why the third expectation fails.

Importing the jest.dontMock operator may work (I tested it), but it seems dirty to me (do you really want to create a "dontmock module" file for each test module?)

You must use the require syntax for the module under test. Replace

 import * as TweetUtils from '../TweetUtils'; 

 const TweetUtils = require('../TweetUtils'); 

It was the same in the jest example before I fixed it: jest # 379

+4
source

Source: https://habr.com/ru/post/1236656/


All Articles