has anyone been writing jasmine / jest tests using es2015 syntax? how much shimming / polyfill / gerrymandering does it require?
i’m having trouble importing functions correctly. i have one module: …./utils/TweetUtils.js
'use strict';
export function getListOfTweetIds (tweets) {
return Object.keys(tweets);
};
and one test suite:
…./__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 hack a console output into the suite with something like this:expect(‘’).toBe(TweetUtils);
Jasmine reports this:
- Expected: '' toBe: {
default: {
getListOfTweetIds: Function
},
getListOfTweetIds: Function
}
So it seems like the import statement is doing something, but it’s clearly not importing my methods honestly. I get the same results when I import using the named function syntax: import {getListOfTweetIds} from ‘../TweetUtils’;
But if I use the default syntax: import getListOfTweetIds from ‘../TweetUtils’;
The second spec fails - it’s no longer typeof function
, but typeof object // => {default: Function}
I’ve been combing the docs and open-issues. There’ve been related issues for a few months, but the known issues don’t seem right. I’ve tried importing my jest.dontMock statements to avoid hoisting, circa: https://github.com/babel/babel-jest/issues/16 but no dice.
Everything works if I modify TweetUtils.js to use module.exports = function…
and bring it into the suite using const myFunction = require(‘../TweetUtils’)
, but it doesn’t feel like I’m channeling the true ES2015 magic. Is everyone just dealing with wonky work-arounds right now while the ecosystem catches up to the new syntax?